目录
一、概述
二、Config服务端配置与测试
配置读取规则
三、Config客户端配置与测试
bootstrasp.yml
四、Config客户端之动态刷新
一、概述
官网:Spring Cloud Config
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
  
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端和客户端两部分。
  
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
SpringCloud Config功能
- 可以集中管理配置文件;
- 不同环境不同配置,动态化的更新配置,分环境部署比如dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露,可以以Post方式通过curl命令行手动更新配置
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式
二、Config服务端配置与测试
服务端的ConfigServer是直接向GitHub拉取配置文件的
在GitHub新建一个仓库springcloud-config用于测试,并创建三份不同环境下的配置文件

新建Module模块cloud-config-center-3344,它即为Cloud的配置中心模块cloudConfig Center
pom.xml
<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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
yml
server:
  port: 3344
spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: git@github.com:zzyybs/springcloud-config.git #GitHub上面的git仓库名字
        ####搜索目录
          search-paths:
            - springcloud-config
      ####读取分支
      label: master
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
 
主启动类 @EnableConfigServer
测试

配置读取规则

其中,label为标签, profile为环境(dev/test/prod),application可以为服务名,所以我们在Github上的配置文件要采取上面的命名格式
三、Config客户端配置与测试
客户端的Config是向服务端ConfigServer拉取配置信息
新建cloud-config-client-3355
pom.xml(注意,客户端导入的依赖是spring-cloud-starter-config,服务端是spding-cloud-config-server)
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>bootstrasp.yml
applicaiton.yml是用户级的资源配置项
 bootstrap.yml是系统级的,优先级更加高
即application.yml是当前微服务自己的配置文件,bootstrap是拉取中心配置的文件,但是如果有同名的配置优先采用bootstrap(中心配置)。
  
 Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。
  
`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。 `Bootstrap context`和`Application Context`有着不同的约定,所以新增了一个`bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置的分离。
  
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
 因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml
  
  
server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
主启动
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355
{
    public static void main(String[] args)
    {
        SpringApplication.run(ConfigClientMain3355.class,args);
    }
}
a业务类,configServer会将配置信息以REST接口的形式暴露
@RestController
public class 9
{
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo() 
    {
        return configInfo;
    }
}测试

这里出现了一个问题,当我们修改github上的配置文件时,服务端直接拉取可以动态改变,但是客户端config并没有发生改变,所以我们需要做进一步配置开启动态刷新配置
四、Config客户端之动态刷新
避免每次更新配置都要重启客户端Config微服务
pom.xml中我们需要引入actuator
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>修改YML
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"业务类加上@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}但还需要加上一步,需要运维人员发送Post请求刷新3355,可以通过curl命令的方式刷新
curl -X POST "http://localhost:3355/actuator/refresh" 
 
但如果有很多个客户端都需要手动刷新的话也很麻烦,可否广播,一次通知,处处生效?
这就开启了下一个篇章,SpringCloud Bus消息总线


















