一、关于

Spring Cloud Config为分布式系统中的配置提供了外部化的服务器和客户端支持。可以通过配置服务器,来管理环境中应用程序的外部属性。

本文代码仓库地址:https://github.com/lazyrabb1t/rabb-springcloud-demo

二、Config服务端

1、在github上创建一个配置文件的仓库,并添加配置文件(rabb.yml)
spring:
  profiles:
    active: dev
---
server:
  port: 16001
spring:
  profiles: dev
  application:
    name: rabb-dev
---
server:
  port: 16002
spring:
  profiles: test
  application:
    name: rabb-test
---
server:
  port: 16003
spring:
  profiles: prod
  application:
    name: rabb-prod
2、创建config-server模块
3、引入依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
4、配置
server:
  port: 16000
spring:
  application:
    name: rabb-config-server
  cloud:
    config:
      server:
        git:
          # 这里为1、中创建的仓库地址
          uri: https://github.com/lazyrabb1t/test-springcloud-config.git
          # 我这里是公开的仓库所以不需要用户名密码
#          username: username@xx.xx
#          password: xxxxxx
          clone-on-start: true
5、启动类添加注解@EnableConfigServer开启配置中心服务
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}
6、启动程序进行测试

访问 http://localhost:16000/rabb/prod ,可以看到配置的内容

7、服务端获取配置的路径

可以按照以下几种格式来获取仓库中的配置:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

其中application 是配置文件的名称,profile是环境,label为git的分支。

三、Config客户端

1、创建config-client模块
2、引入依赖

我所使用的cloud版本为2020.0.1,需要手动引入spring-cloud-starter-bootstrap依赖,否则无法加载bootstrap.yml文件

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
3、resources目录下添加bootstrap.yml文件
server:
  port: 17000
spring:
  application:
    name: rabb-config-client
  cloud:
    config:
      uri: http://localhost:16000
      profile: dev
      label: master
      name: rabb
4、添加获取配置文件内容接口,启动项目测试
@SpringBootApplication
@RestController
public class ConfigClientApplication {

    @Value("${spring.application.name}")
    String name;

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

    @GetMapping
    public String url() {
        return name;
    }
}
5、添加刷新配置

通过SpringBoot Actuator的refresh端点可以刷新客户端配置信息。

在pom.xml中添加Actuator的依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在bootstrap.yml中开启refresh端点:

management:
  endpoints:
    web:
      exposure:
        include: 'refresh'

添加@RefreshScope注解用于刷新配置

@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {

    @Value("${spring.application.name}")
    String name;

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

    @GetMapping
    public String url() {
        return name;
    }
}

重新启动config-client后,调用refresh端点进行配置刷新:

curl -X POST http://localhost:16001/actuator/refresh

四、使用Eureka创建Config集群

1、首先服务端与客户端都程序都需要引入eureka依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
2、Config服务端模块添加eureka注册配置
server:
  port: 16000
spring:
  application:
    name: rabb-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lazyrabb1t/test-springcloud-config.git
#          username: username@xx.xx
#          password: xxxxxx
          clone-on-start: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10002/eureka/
3、Config客户端添加eureka配置并修改config配置
server:
  port: 17000
spring:
  application:
    name: rabb-config-client
  cloud:
    config:
#      uri: http://localhost:16000
      profile: dev
      label: master
      name: rabb
      # 开启服务发现并添加配置中心服务名称
      discovery:
        enabled: true
        service-id: RABB-CONFIG-SERVER
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10002/eureka/
4、测试

使用不同端口启动多个config服务端模块,然后启动eureka服务端和config客户端模块。

五、其他

1、添加用户认证

https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_security

2、配置刷新

https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_push_notifications_and_spring_cloud_bus

参考

https://docs.spring.io/spring-cloud-config/docs/current/reference/html/

https://stackoverflow.com/questions/38532683/spring-cloud-config-client-not-loading-the-value-from-config-server