分布式之配置中心config

一、配置中心服务端
  1. 依赖

    	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-config-server</artifactId>
    	</dependency>
    
  2. 新建一个配置中心项目,启动类加注解@EnableConfigServer

    @SpringBootApplication
    @EnableConfigServer
    public class CloudConfigCenter3344Application {
    
       public static void main(String[] args) {
       	SpringApplication.run(CloudConfigCenter3344Application.class, args);
       }
    
    }
    
  3. 配置文件
    
    server:
      port: 3344
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              # git仓库地址
              # 也可以直接用克隆地址:git@gitee.com:******/springcloud-config.git,但是要带用户名密码,私有仓库也是
              #username:*****
              #password:*****
              uri: https://gitee.com/*******/springcloud-config    
              search-paths:
                - springcloud-config  # 目录
          label: master    # 分支
    
    
二、客户端
  1. 依赖
    	<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    
  2. 配置文件
    server:
      port: 3355
    
    spring:
      application:
        name: cloud-config-client
      cloud:
        config:
          uri: http://localhost:3344  # 配置中心地址
          label: master  # 分支名称
          name: config   # 配置文件名称
          profile: dev  # 后缀名称  结合在一起就是:master分支上的config-dev.yml配置文件  http://localhost:3344/master/config-dev.yml
    
    # 暴露监控端点,为了实现动态更新
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
  3. 动态更新,当git上配置文件更新时,在不重启项目的情况下,获取最新的配置信息
    除了配置文件中暴露监控端点(需要依赖spring-boot-starter-actuator),还需要在获取配置信息的类上加注解@RefreshScope
    比如,现在git上配置文件中有一个config.info,当git上的config.info值更新时,需要发送一个post请求:http://localhost:3355/actuator/refresh,此时客户端就更获取到最新的配置信息
    @RefreshScope   //刷新
    @RestController
    public class ConfigController {
    
        @Value("${config.info}")
        private String config;
    
        @GetMapping("/getConfigInfo")
        public String getConfigInfo(){
            return config;
        }
    }
    
  4. 但是上述的动态更新还是太麻烦,当存在多个配置客户端时,需要对每个客户端发送post刷新请求,所以还可以利用bus总线进行广播通知进行全局刷新。
    4.1 首先需要使用RabbitMQ消息队列,给配置中心和客户端添加依赖
    <!-- 对rabbitMQ支持 -->
    	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-starter-bus-amqp</artifactId>
    	</dependency>
    
    4.2 修改配置文件
    配置中心:
    server:
      port: 3344
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/ning_1106/springcloud-config    # git仓库地址
              search-paths:
                - springcloud-config  # 目录
          label: master    # 分支
      #rabbitmq配置
      rabbitmq:
        host: 111.229.173.132
        port: 5672
        username: guest
        password: guest
    #rabbitmq相关配置,暴露bus刷新配置的端点
    management:
      endpoints:
        web:
          exposure:
            include: 'bus-refresh'  #bus-refresh对应后续的post请求
    # 服务注册到eureka
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    
    客户端:
    server:
      port: 3355
    
    spring:
      application:
        name: cloud-config-client
      cloud:
        config:
          uri: http://localhost:3344  # 配置中心地址
          label: master  # 分支名称
          name: config   # 配置文件名称
          profile: dev  # 后缀名称  结合在一起就是:master分支上的config-dev.yml配置文件  http://localhost:3344/master/config-dev.yml
      #rabbitmq配置
      rabbitmq:
        host: 111.229.173.132
        port: 5672
        username: guest
        password: 369789
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
    
    # 暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    4.3 此时如果对gitee上的配置文件进行修改,只需要对配置中心发送一个post请求 (http://localhost:3344/actuator/bus-refresh) 即可刷新所有的客户端的配置,而不需要对每个客户端发送
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值