0
点赞
收藏
分享

微信扫一扫

Spring Cloud 学习笔记之——09 Config 分布式配置中心

得一道人 2022-01-30 阅读 90

        微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以—套集中式的、动态的配置管理设施是必不可少的。
        SpringCloud 提供了ConfigServer 来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理.....

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持(Git / Github)、配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

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

主要功能:

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

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

SpringCloud 官网链接:Spring Cloud Config

Config 服务配置与测试

1、在 github / giee 上创建一个名为 springcloud-config 的仓库,仓库中有两个分支一个是 master,一个是 dev 分支,两个分支都有 config-dev.yml 、config-test.yml 和 config-prod.yml文件

远程仓库中文件名称如下图所示:

2、创建 cloud-config-center-3344 模块作为配置中心模块

2.1、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>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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

2.2、YML 配置文件

server:
  port: 3344

spring:
  application:
    name: cloud-config-center  # 注册进 Eureka 的服务器微服务名称

  # 服务配置中心从 Git 读取数据时遇到中文会出现乱码,这里设置编码格式为 UTF-8
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true

  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/ling-swift/springcloud-config.git   # Github 上面的 git 仓库名称
          ## 搜索目录
          search-paths:
            - springcloud-config

      # 读取主分支
      label: master

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

2.3、主启动类:com.atyixuan.springcloud.MainAppConfigCenter3344  

注意添加 @EnableConfigServer 注解

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

2.4、修改 hosts 文件添加映射:hosts 文件路径 C:\Windows\System32\drivers\etc\hosts,在该文件末尾添加如下内容

######### Spring Cloud Config ##########
127.0.0.1 config-3344.com

3、测试:先后启动微服务 cloud-eureka-server7001 和 cloud-config-center3344

浏览器输入:http://config-3344.com:3344/master/config-dev.yml

配置读取规则

        依据 Spring Cloud Config 官网的内容,HTTP服务中有如下形式的资源: ​​​​

推荐使用 /{label}/{application}-{profile}.yml 方式==> /{分支名称}/{服务名称}-{环境}.yml

对于 master 分支下的文件:

http://config-3344.com:3344/master/config-dev.ymlhttp://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

对于 dev 分支下的文件:

http://config-3344.com:3344/dev/config-dev.yml

http://config-3344.com:3344/dev/config-test.yml

http://config-3344.com:3344/dev/config-prod.yml

还有如下两种方式:/{application}-{profile}.yml  ==> /{服务名称}-{环境}.yml

http://config-3344.com:3344/config-dev.yml

http://config-3344.com:3344/config-test.yml

http://config-3344.com:3344/config-prod.yml

/{application}-{profile}/{label} ==> /{服务名称}-{环境}/{分支名称}.yml

http://config-3344.com:3344/config/dev/master

http://config-3344.com:3344/config/test/master

http://config-3344.com:3344/config/prod/master

label ==> 服务名称, label ==> 分支(branch),   profofile ==> 环境(dev/test/prod)

Config 客户端配置与测试

新建cloud-config-client-3355 模块

1、POM 文件

<dependencies>
        <!--Config 的客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>com.atyixuan</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </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>

2、bootstrap.yml 配置文件

boostrap.yml 文件内容:

server:
  port: 3355

spring:
  application:
    name: config-client

  cloud:
    config:
      label: master     # 分支名称
      name: config      # 配置文件名称,因为配置文件名称是 config-dev.yml, 这里写 短横线前面的即可
      profile: dev      # 配置文件名称是 config-dev.yml 中 短横线之后的内容即为 dev
      uri: http://localhost:3344   # 配置中心地址

      # 综合上述四项配置: http://localhost:3344/master/config-dev.yml
      # 由于在 hosts 文件中做了修改
      ######### Spring Cloud Config ##########
      #127.0.0.1 config-3344.com
      #上面的 localhost <==> 127.0.0.1 <==> config-3344.com
      # 综上所述:请求发起的地址是 http://config-3344.com:3344/master/config-dev.yml

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

3、主启动类:com.atyixuan.springcloud.ConfigClientMain3355

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

 4、业务类:com.atyixuan.springcloud.controller.ConfigClientController

@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;
    
    @GetMapping(value = "/configinfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

上面 @Value("${config.info}") 就是获取配置文件中的内容,在远程仓库中,config-dev.yml 配置文件中就是config.info

5、测试:先后启动  cloud-eureka-server7001、cloud-config-center3344 和 cloud-config-client-3355

浏览器输入:http://config-3344.com:3344/master/config-dev.yml

 http://localhost:3355/configInfo

同步刷新问题

         Linux运维修改GitHub上的配置文件内容做调整,将远程库中的 verision 信息修改为 2     

 

         刷新cloud-config-center3344,发现ConfigServer配置中心立刻响应 

         刷新 cloud-config-client-3355, 发现ConfigServer客户端没有任何响应

         cloud-config-client-3355 没有变化除非自己重启 或者重新加载,重启 cloud-config-client-3355 再次刷新   http://localhost:3355/configInfo 链接 链接

Config 客户端之动态刷新

1、在 cloud-config-client-3355 的 POM 文件中引入

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

2、修改其 YML 配置文件:

server:
  port: 3355

spring:
  application:
    name: config-client

  cloud:
    config:
      label: master     # 分支名称
      name: config      # 配置文件名称,因为配置文件名称是 config-dev.yml, 这里写 短横线前面的即可
      profile: dev      # 配置文件名称是 config-dev.yml 中 短横线之后的内容即为 dev
      uri: http://localhost:3344   # 配置中心地址

      # 综合上述四项配置: http://localhost:3344/master/config-dev.yml
      # 由于在 hosts 文件中做了修改
      ######### Spring Cloud Config ##########
      #127.0.0.1 config-3344.com
      #上面的 localhost <==> 127.0.0.1 <==> config-3344.com
      # 综上所述:请求发起的地址是 http://config-3344.com:3344/master/config-dev.yml

eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka  # 服务注册中心地址

# 添加 management 内容
management:
  endpoints:
    web:
      exposure:
        include: "*"

3、在业务类 com.atyixuan.springcloud.controller.ConfigClientController 上添加
@RefreshScope注解

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

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

4、测试,再次启动  cloud-eureka-server7001、cloud-config-center3344 和 cloud-config-client-3355

浏览器中输入:http://config-3344.com:3344/master/config-dev.yml


http://localhost:3355/configInfo

 再次修改远程仓库中的文件内容:修改其版本号为 version=3 

刷新 http://config-3344.com:3344/master/config-dev.yml  远程库中的修改立即生效

刷新  http://localhost:3355/configInfo 版本号没有变化,依旧是2

 为了让版本号的改变可以同步到 cloud-config-client-3355,必须向 cloud-config-client-3355 发送 Post 请求刷新 cloud-config-client-3355 

此时刷新 

 http://localhost:3355/configInfo,版本号 version = 3

举报

相关推荐

0 条评论