美文网首页
09基于Config的配置中心

09基于Config的配置中心

作者: 攻城老狮 | 来源:发表于2021-01-05 19:24 被阅读0次

    一 Spring Cloud Config的理解

    对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护。Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。server提供配置文件的存储、以接口的形式将配置文件的内容提供出去。client通过接口获取数据、并依据此数据初始化自己的应用。

    二 Spring Cloud Config的配置部署

    2.1 在GitHub上创建config的配置文件仓库,上传配置文件

    文件命名规则:

    • {application}-{profile}.yml
    • {application}-{profile}.properties

    application为应用名称 profile指的开发环境(用于区分开发环境,测试环境、生产环境等)

    image-20210103152720464.png

    2.2 搭建服务端程序

    1. 引入依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
    1. 修改配置文件
    server:
      port: 10001 #服务端口
    spring:
      application:
        name: config-server #指定服务名
      cloud:
        config:
          server:
            git:
              uri: https://github.com/yaokuku123/springcloud-config.git # 配置的地址
    
    1. 启动类开启配置中心注解
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class,args);
        }
    }
    
    1. 测试配置中心是否可以获得远端数据
    http://localhost:10001/product-dev.yml
    

    2.3 修改客户端配置

    1. 引入依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
    1. 删除原来的application.yml(从配置中心获取)
    2. 添加boostrap.yml

    使用加载级别更高的 bootstrap.yml 文件进行配置。启动应用时会检查此配置文件,在此文件中指定 配置中心的服务地址。会自动的拉取所有应用配置并启用

    spring:
     cloud:
       config:
         name: product
         profile: dev
         label: master
         uri: http://localhost:10001
    
    1. 手动刷新

    但当我们修改GitHub上面的值时,服务端(Config Server) 能实时获取最新的值,但客户端(Config Client)读的是缓存,无法实时获取最新值。SpringCloud已经为我们解决了这个问题,那就是客户端使用post去触发refresh,获取最新数据,需要添加依赖springboot-starter-actuator

    • 添加依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    • 在需要被刷新的类上添加注解
    @RefreshScope
    public class ProductController {}
    
    • 在bootstrap中配置开放刷新端口
    management:
     endpoints:
       web:
         exposure:
           include: refresh
    
    • 使用postman发送post请求到该地址,刷新缓存
    http://localhost:9001/actuator/refresh
    

    三 配置中心高可用

    1. 在配置中心引入eureka的依赖
    <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>
    
    1. 服务中心的配置文件添加eureka的相关配置
    server:
      port: 10001 #服务端口
    spring:
      application:
        name: config-server #指定服务名
      cloud:
        config:
          server:
            git:
              uri: https://github.com/yaokuku123/springcloud-config.git
    eureka:
      client:
        service-url: 
          defaultZone: http://localhost:7001/eureka/
      instance:
        prefer-ip-address: true
    
    1. 修改客户端bootstrap的配置文件,将从配置中心直接拉取改为从eureka的微服务中拉取配置
    spring:
      cloud:
        config:
          name: product
          profile: dev
          label: master
          #uri: http://localhost:10001
          discovery:
            enabled: true # 开启从eureka的服务发现
            service-id: config-server # 配置中心的服务名称
    management:
      endpoints:
        web:
          exposure:
            include: refresh
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka/
      instance:
        prefer-ip-address: true
    

    四 通过消息总线更新配置

    4.1 引入说明

    由于若每次去手动刷新各微服务较为繁琐,通过引入消息总线的方式,仅刷新配置中心即可。配置中心会将该消息推送到消息总线,各个微服务从消息总线中接收消息,刷新各自的缓存,并从配置中心获取配置,使得更新配置更为便利。

    4.2 消息总线整合配置中心

    1. 引入消息总线的依赖
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-bus</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    </dependency>
    
    1. 配置中心的配置
    server:
      port: 10001
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: https://github.com/yaokuku123/springcloud-config.git
      rabbitmq: # 配置消息中间件
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka/
      instance:
        prefer-ip-address: true
    management: # 开放刷新端口
      endpoints:
        web:
          exposure:
            include: bus-refresh
    
    1. 在客户端需要在github上加入消息中间件的配置
    rabbitmq: # 配置消息中间件
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
    
    1. 修改github上的配置,使用post请求配置中心触发消息总线
    http://localhost:10001/actuator/bus-refresh
    

    相关文章

      网友评论

          本文标题:09基于Config的配置中心

          本文链接:https://www.haomeiwen.com/subject/eenvoktx.html