springcloud-服务配置Config(八)

概述

  • 目前分布式面临的问题
  1. 微服务意味着将单体应用中的业务拆分为一个个子服务, 每个服务的粒度相对较小, 因此系统中出现大量的服务, 由于每个服务都需要必要的配置信息才能运行, 所以一套集中式的, 动态的配置管理设备是不可缺少的.
    • 我们每一个微服务自己带着application.yml, 上百个的话管理起来会非常麻烦......
  2. SpringCloud提供了ConfigServer来解决这个问题.

  • 是什么
  1. SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持, 配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置.
  2. Config分为服务端和客户端两部分.
  3. 服务端也称为分布式配置中心, 它是一个独立的微服务应用, 用来连接配置服务器并为客户端提供获取配置信息, 加密/解密信息等访问接口.
  4. 客户端则是通过指定的配置中心来管理应用资源, 以及业务相关的配置内容, 并在启动的时候从配置中心获取和加载配置信息.
  5. 配置服务器默认采用git来存储配置信息, 这样就有助于对环境配置进行版本管理, 并可以通过git客户端工具来方便的管理和访问配置内容.
  • 作用
  1. 集中管理配置文件
  2. 不同环境不同配置, 动态化的配置更新, 分环境部署.
  3. 运行期间动态调整配置, 不再需要在每个服务部署的机器上编写配置文件, 服务会向配置中心统一拉取配置自己的信息.
  4. 当配置发生变动时, 服务不需要重启即可感知到配置的变化并应用新的配置.
  5. 将配置信息以REST接口的形式暴露 - (post, curl访问刷新均可)...

Config服务端配置

  1.  用Github新建一个Repository: springcloud-config
    • 新建README.md,  config-dev.yml,  config-prod.yml,  config-test.yml
  2. Git操作
    • 拉取到本地仓库
      git push 地址
    • 若要修改, 修改完后如下操作
      git add
      git commit -m "init yml"
      git push origin master
  3. 新建Module: cloud-config-center-3344作为Cloud的配置中心模块
    • pom
      <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>springcloud</groupId>
                  <artifactId>cloud-api-commons</artifactId>
                  <version>${project.version}</version>
              </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
        cloud:
          config:
            server:
              git:
                uri: git@github.com:xxx/springcloud-config.git #github上的Git仓库地址
                search-paths: #搜索目录
                  - springcloud-config
            label: master #读取分支
      
      eureka:
        client:
          service-url:
            defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    • 主启动类: @EnableConfigServer
      @EnableConfigServer
      @SpringBootApplication
      public class ConfitCenterMain3344 {
      
          public static void main(String[] args) {
              SpringApplication.run(ConfitCenterMain3344.class, args);
          }
      }
  4. 在windows下修改hosts文件, 增加映射 - C:WindowsSystem32driversetc
    127.0.0.1 config-3344.com
  5. 测试通过Config微服务是否能从Github上获取配置内容

  •  配置的读取规则
  1.  /{label}/{application}-{profile}.yml(最推荐使用这种方式)
    • http://config-3344.com:3344/master/config-dev.yml
    • http://config-3344.com:3344/master/config-test.yml
    • http://config-3344.com:3344/master/config-prod.yml
  2.  /{application}-{profile}.yml
    • 默认master
  3. /{application}-{profile}[/{label}]

Config客户端配置

  1. 新建cloud-config-client-3355
  2. pom
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
  3. bootstrap.yml
    • 是什么
      • application.yml是用户级资源配置项, 而bootstrap.yml是系统级的, 优先级更高.
      • SpringCloud会创建一个"Bootstrap Context", 作为Spring应用的"Application Context"的父上下文, 初始化的时候, "Bootstrap Context"负责从外部源加载配置属性并解析配置, 这两个上下文共享一个从外部获取的"Environment".
      • Bootstrap属性有高优先级, 默认情况下, 它们不会被本地配置覆盖, "Bootstrap context"和"Application Context"有着不同的约定, 所以新增bootstrap.yml, 保证"Bootstrap Context"和"Application Context"配置的分离.
    • 内容
      server:
        port: 3355
      
      spring:
        application:
          name: config-client
        cloud:
          #Config客户端配置
         config:
            label: master #分支名称
            name: config #配置文件名称
            profile: dev #读取后缀名称 [config-dev.yml]
            uri: http://localhost:3344 #配置中心地址
      
      eureka:
        client:
          service-url:
            defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  4. 主启动类
    @EnableEurekaClient
    @SpringBootApplication
    public class ConfigClient3355 {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigClient3355.class, args);
        }
    }
  5. 业务类
    • 我们的github中有如下内容
    • controller
      @RestController
      public class ConfigClientController {
      
          @Value("${config.info}")
          private String configInfo;
      
          @GetMapping("/configInfo")
          public String getConfigInfo() {
              return configInfo;
          }
      }
  6. 测试
    • 启动7001, 7002, 3344
    • 启动3355作为Client准备访问: http://localhost:3355/configInfo
      • 可成功实现客户端3355访问SpringCloud Config3344通过GitHub获取配置信息.
  7. 问题
    • 我们在github上对配置文件内容做调整.
    • 刷新3344, 发现ConfigServer配置中心立刻响应.
    • 但刷新3355, Config客户端没有任何响应.
    • 3355没有变化, 除非重启. 每次运维人员修改配置文件, 客户端都得重启? 这简直是噩梦.

Config客户端动态刷新

  1. 我们必须避免每次更新配置都要重启客户端微服务3355.
  2. 动态刷新
    • pom中必须引入acctuator监控
      <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-actuator</artifactId>
          </dependency>
    • 修改yml, 暴露监控端口
      #暴露监控端点
      management:
        endpoints:
          web:
            exposure:
              include: "*"
    • 在业务类上添加@RefreshScope注解
      @RefreshScope
      @RestController
      public class ConfigClientController {
      
          @Value("${config.info}")
          private String configInfo;
      
          @GetMapping("/configInfo")
          public String getConfigInfo() {
              return configInfo;
          }
      }
    • 此时我们修改完配置文件后再去访问3355, 发现还没有变化.
  3. 需要我们的运维人员在每次修改完配置文件, 发送Post请求刷新3355
    • curl -X POST "http://localhost:3355/actuator/refresh"
    • 此时即修改成功. 避免了服务的重启
  4. 问题
    • 假如有上百台微服务客户端, 每个微服务都要执行一次post请求, 手动刷新?
    • 假如我们要这100台中的特定的72台刷新, 又该怎么办?
    • 下一章Bus消息总线解决上述问题!!!
原文地址:https://www.cnblogs.com/binwenhome/p/13229713.html