1. Config

1. Config分布式配置中心介绍

分布式系统面临的配置问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理.……

是什么

官网介绍
七、服务配置 - 图1

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置,这个中心化的外部配置中心能够通过Git从远程获取配置信息

SpringCloud Config分为服务端和客户端两部分

  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

作用

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露 - post/crul访问刷新即可…

与GitHub整合配置

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式。

2. Config配置总控中心搭建

1. 使用Git从GitHub上获取配置文件

这里直接使用的使周阳老师的Git仓库地址https://github.com/zzyybs/springcloud-config.git
或者使用自己的账户在GitHub上新建一个名为SpringCloud的新Repository
也可用使用Gitee

在本地硬盘将仓库克隆下来,随后 git add .,git commit -m “sth” 等一系列上传操作上传到springcloud-config的新Repository
查看配置文件信息:

  • config-dev.yml

    1. config:
    2. info: "master branch,springcloud-config/config-dev.yml version=7"
  • config-prod.yml

    config:
    info: "master branch,springcloud-config/config-prod.yml version=1"
    
  • config-test.yml

    config:
    info: "master branch,springcloud-config/config-test.yml version=1"
    

2. 创建配置中心

创建Module模块cloud-config-center-3344,它即为Cloud的配置中心模块CloudConfig Center

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">
    <parent>
        <artifactId>SpringCloud</artifactId>
        <groupId>org.gmf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center-3344</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--Config配置中心依赖-->
        <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>
</project>

application.yaml
server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/theres-nothing-funny/spring-cloud-config.git #Gitee上面的git仓库名字
          #搜索目录
          search-paths:
            - SpringCloud-config
      #读取分支
      label: master

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

主启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class, args);
    }
}


windows下修改hosts文件,增加映射
127.0.0.1 config-3344.com

3. 测试通过Config微服务是否可以从GitHub上获取配置内容

启动Eureka7001、ConfigCenter3344
浏览器防问 - http://config-3344.com:3344/master/config-dev.yml
image.png
结果:成功响应配置文件数据,实现了用SpringCloud Config通过GitHub获取配置信息

4. 配置读取规则

官网文档
The HTTP service has resources in the following form:
image.png
/{label}/{application}-{profile}.yml(推荐)

/{application}-{profile}.yml(label分支从配置文件中查找)

/{application}/{profile}[/{label}]

重要配置细节总结
/{name}-{profiles}.yml
/{label}-{name}-{profiles}.yml

  • label:分支(branch)
  • name:服务名
  • profiles:环境(dev/test/prod)

注意:GitHub中的配置文件命名严格遵守Spring官方命名规则

3. Config客户端配置与测试

1. 新建cloud-config-client-3355


2. 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">
    <parent>
        <artifactId>SpringCloud</artifactId>
        <groupId>org.gmf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3355</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <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>
</project>

3. bootstrap.yaml

  • applicaiton.yml:用户级的资源配置项
  • bootstrap.yml:系统级的,优先级更加高

Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。
初始化的时候,BootstrapContext负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的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://config-3344.com:3344  #配置中心地址

#将服务注册到Eureka注册中心
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

4. 主启动类

@SpringBootApplication
@EnableEurekaClient
public class ConfigCenterMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3355.class, args);
    }
}

5. 业务类Controller

@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return configInfo;
    }
}

6. 测试

启动cloud-config-client-3355
浏览器访问:http://localhost:3355/configInfo
image.png
结果:成功从配置中心中获取到了 master 分支的 config-dev.yml 配置内容

7. 面临的问题——分布式配置的动态刷新问题

  • Linux运维修改GitHub上的配置文件内容做调整
  • 刷新3344,发现ConfigServer配置中心立刻响应
  • 刷新3355,发现ConfigClient客户端没有任何响应
  • 3355没有变化除非自己重启或者重新加载
  • 难到每次运维修改配置文件,客户端都需要重启?

演示:修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version
image.png
服务配置中心访问:http://config-3344.com:3344/master/config-dev.yml 发现内容发生变动
image.png
配置客户端访问:http://localhost:3355/configInfo 发现内容依旧不变
image.png

4. Config动态刷新之手动版

在上一章节最后提到了配置客户端动态刷新的问题,下面的方式能够手动刷新避免每次改动配置都要重启配置客户端服务

手动动态刷新步骤:

1. 在pom.xml中引入actuator监控依赖

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

2. 修改yaml

#暴露所有监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3. 业务类Controll上添加@RefreshScope注解

@RestController
@RefreshScope  //动态刷新配置
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo()
    {
        return configInfo;
    }
}

4. 测试

此时修改github配置文件内容 -> 访问3344 -> 访问3355
浏览器访问:http://localhost:3355/configInfo 结果依旧没有发生变化??

原因:需要运维人员手动发生POST请求刷新配置客户端
image.png
浏览器再次访问:http://localhost:3355/configInfo 得到的结果发送改变

5. 仍然存在的问题

  • 假如有多个微服务客户端3355/3366/3377
  • 每个微服务都要执行—次post请求,手动刷新?
  • 可否广播,一次通知,处处生效?
  • 我们想大范围的自动刷新,求方法