学习Spring Cloud Config 的时候,很容易遇到各种各样的问题,这里我就把我遇到的:Client 端读取不到Server端的配置文件中的属性总结一下。

首先我搭建了一个Eureka 注册中心,这里就不着重介绍了,不知道的小伙伴可以
网上查资料!

1.搭建Config 配置中心

POM 文件:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sg.config</groupId>
    <artifactId>config-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

这里面Config用到的依赖是 spring-cloud-config-server ,其他自己看着引入即可

2.application.yml 的配置

server:
  port: 8095
spring:
  application:
    name: config-demo
  cloud:
    config:
      server:
        git:
          uri: https://github.com/****/spring-cloud-demo.git
          username:
          password: 
          search-paths: spring-cloud-demo-config
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8090/eureka/
    register-with-eureka: true
    fetch-registry: true

Config Server 的配置主要是spring.cloud.config.server.git 下面的东西,其他按需配置即可。

3.Config Server 启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigDemoApplication {

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

}

至此 Config Server 端就配置完成了。

4.配置Config Client 端

首先引入依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sg.eureka.client</groupId>
    <artifactId>eureka-client-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sg.common</groupId>
            <artifactId>spring-cloud-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </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>

主要有关的依赖是 spring-cloud-starter-config ,其他的按需引入即可

5.application.yml 配置文件
server:
  port: 8091
spring:
  application:
    name: eureka-client-demo
  cloud:
    config:
      profile: test
      uri: http://127.0.0.1:8095/
      label: master
      discovery:
        enabled: true
        service-id: config-demo

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8090/eureka/

### 端点控制
management:
  endpoints:
    web:
      exposure:
        # 开启指定端点
        include: hystrix.stream

project:
  name: hahha

启动类上什么配置也不用添加,就可以了,重要的一点:
1:如果用了Eureka ,则需要配置 spring.cloud.config.discovery.enable: true 和 spring.cloud.config.discovery.service-id: config-demo 这两个属性

6.读取Server中配置的属性 
package com.sg.eureka.client.controller;

import com.sg.common.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

/**
 * @author liuhailong
 * @date 2019-08-08
 */
@RestController
@RequestMapping(value = "users")
@Configuration
public class UserController {

    @Autowired
    private Environment environment;

    @Value("${project.name}")
    private String name;

    @GetMapping(value = "/config")
    public String getConfig(@RequestParam("key")String key){
        return environment.getProperty(key);
    }

    @GetMapping(value = "/projectName")
    public String projectName(){
        return name;
    }

}

我这里用了 两种方式去读取配置文件中的内容
1:使用Spring core中的Environment 类 中的getProperty 可以取到
2:使用Spring 的 @Value("${project.name}") 注解

6.接下来验证一下:访问 http://localhost:8091/users/config?key=project.name 结果发现获取不到Config Server中配置的参数。

主要原因:

Spring Cloud 会首先加载bootstrap.yml 和bootstrap.properties 配置文件,然后再去加载spplication.properties 配置文件,所以在Config client 中的配置文件名称要修改为 bootstrap.yml 。然后在读取配置中心Config Server 中的 eureka-client-demo-test 的配置文件,这样就可以读取到了。