spring cloud Config client 和项目公共配置

Posted 冬天不眠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud Config client 和项目公共配置相关的知识,希望对你有一定的参考价值。

1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>Spring-Cloud-Config-Client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Spring-Cloud-Config-Client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency> -->
        <dependency>  用这个包  不要用上面的否则启动报错
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2 启动类

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigClientApplication.class, args);
    }
}

3 配置文件、

application.properties

server.port=7023
spring.application.name=test
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/

在resources文件夹下新建bootstrap.properties文件

 

 

spring.cloud.config.uri=http://localhost:7066
spring.cloud.config.name=test
spring.cloud.config.profile=dev
#spring.cloud.config.discovery.enabled=true     //这个暂时没调试通,先通过uri的方式吧
#spring.cloud.config.discovery.service-id=CONFIG-SERVER
#spring.cloud.config.label=master

 

 配置仓库中的application.properties为所有服务的公共配置,

config lient 首先会查找application下的配置然后在找项目下的配置,如果application和项目下都有相同的配置,则项目配置会覆盖application下的配置

 

4 编写测试类

@RestController
public class TestController {
    @Value("${version}")
    private String version;
    
    @Value("${all.data.url}")
    private String dataUrl;
    
    @GetMapping("/")
    public String getMessage() {
        return version+dataUrl;
    }
}

5 启动项目访问

test-dev  和 application-dev 

 

 

 成功!

 

以上是关于spring cloud Config client 和项目公共配置的主要内容,如果未能解决你的问题,请参考以下文章

无法通过 spring.cloud.config.enabled:false 禁用 Spring Cloud Config

0701-spring cloud config-简介Config Server开发Config Client开发

问题 spring-cloud-config 和 spring-cloud-bus

spring-cloud-config——Quick Start

spring cloud config 8888端口可以变么

spring-cloud-config-server——Environment Repository