SpringCloud Config(配置中心)实现配置自动刷新(十六)

Easter79
• 阅读 466

一、实现原理

1、ConfigServer(配置中心服务端)从远端git拉取配置文件并在本地git一份,ConfigClient(微服务)从ConfigServer端获取自己对应 配置文件;

2、当远端git仓库配置文件发生改变,ConfigServer如何通知到ConfigClient端,即ConfigClient如何感知到配置发生更新?

Spring Cloud Bus会向外提供一个http接口,即图中的/bus/refresh。我们将这个接口配置到远程的git的webhook上,当git上的文件内容发生变动时,就会自动调用/bus-refresh接口。Bus就会通知config-server,config-server会发布更新消息到消息总线的消息队列中,其他服务订阅到该消息就会信息刷新,从而实现整个微服务进行自动刷新。

二:实现方式

实现方式一:某个微服务承担配置刷新的职责

SpringCloud Config(配置中心)实现配置自动刷新(十六)

1、提交配置触发post调用客户端A的bus/refresh接口

2、客户端A接收到请求从Server端更新配置并且发送给Spring Cloud Bus总线

3、Spring Cloud bus接到消息并通知给其它连接在总线上的客户端,所有总线上的客户端均能收到消息

4、其它客户端接收到通知,请求Server端获取最新配置

5、全部客户端均获取到最新的配置

存在问题:

1、打破了微服务的职责单一性。微服务本身是业务模块,它本不应该承担配置刷新的职责。2、破坏了微服务各节点的对等性。3、有一定的局限性。WebHook的配置随着承担刷新配置的微服务节点发生改变。

改进如下方式二:配置中心Server端承担起配置刷新的职责,原理图如下:

SpringCloud Config(配置中心)实现配置自动刷新(十六)

1、提交配置触发post请求给server端的bus/refresh接口

2、server端接收到请求并发送给Spring Cloud Bus总线

3、Spring Cloud bus接到消息并通知给其它连接到总线的客户端

4、其它客户端接收到通知,请求Server端获取最新配置

5、全部客户端均获取到最新的配置

SpringCloud Config(配置中心)实现配置自动刷新(十六)

第一种配置:

启动rabbitmq

rabbitmq: docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:management
  • product-service

    • pom.xml

      <dependency>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-actuator</artifactId>
                      </dependency>
      ​
                      <dependency>
                          <groupId>org.springframework.cloud</groupId>
                          <artifactId>spring-cloud-starter-bus-amqp</artifactId>
                      </dependency>
      
    • bootstrap.yml

      spring:
                        rabbitmq:
                          host: 192.168.180.112
                          port: 5672
                          username: guest
                          password: guest
                  
                      #暴露全部的监控信息
                      management:
                        endpoints:
                          web:
                            exposure:
                              include: "*"
      
    • web层

      @RestController
      @RequestScope
      @RequestMapping("/api/v1/product")
      public class ProductController {
      ​
      ​
      ​
          @Value("${server.port}")
          private String port;
      ​
          @Autowired
          private ProductService productService;
      ​
          /**
           * 获取所有商品列表
           * @return
           */
          @RequestMapping("list")
          public Object list(){
              return productService.listProduct();
          }
      ​
      ​
          /**
           * 根据id查找商品详情
           * @param id
           * @return
           */
          @RequestMapping("find")
          public Object findById(int id){
      ​
      //        try {
      //            TimeUnit.SECONDS.sleep(2);
      //        } catch (InterruptedException e) {
      //            e.printStackTrace();
      //        }
              Product product = productService.findById(id);
      ​
              Product result = new Product();
              BeanUtils.copyProperties(product,result);
              result.setName( result.getName() + " data from port="+port );
              return result;
          }
      ​
      }
      ​
      
    • 测试,要手动发送POST http://localhost:8773/actuator/bus-refresh

      就会发现控制台会重新加载配置信息

SpringCloud Config(配置中心)实现配置自动刷新(十六)

第二种配置:

其实也是把config-server连到Bus和mq中,然后去请求它,其他服务才进行重新加载。

  • config-server

    • pom.xml

       1  <dependency>
       2             <groupId>org.springframework.boot</groupId>
       3             <artifactId>spring-boot-starter-actuator</artifactId>
       4         </dependency>
       5 ​
       6         <dependency>
       7             <groupId>org.springframework.cloud</groupId>
       8             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
       9         </dependency>
      10       <dependency>
      11             <groupId>org.springframework.cloud</groupId>
      12             <artifactId>spring-cloud-config-server</artifactId>
      13         </dependency>
      
    • application.yml

      #服务名称
      spring:
        application:
          name: config-server
        cloud:
          config:
            server:
              git:
                uri: http://192.168.180.112/root/test.git
                username: root
                password: ***********
                default-label: master
        rabbitmq:
          host: 192.168.180.112
          username: guest
          password: guest
          port: 5672
      management:
        endpoints:
          web:
            exposure:
              include: "*"
      ​
      ​
      #服务的端口号
      server:
        port: 9100
      ​
      ​
      #指定注册中心地址
      eureka:
        client:
          serviceUrl:
            defaultZone: http://localhost:8761/eureka/
      
  • 发送请求:http://192.168.137.1:9100/actuator/bus-refresh,会发现所有的服务都会拉取信息。

SpringCloud Config(配置中心)实现配置自动刷新(十六)

都会加进队列中。

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
Nacos Config源代码分析(一)
NacosConfig提供了配置管理的功能,它允许用户在nacos上配置keyvalue对,并在客户端订阅需要的配置。当配置发生变更时,订阅的客户端会获得通知,随后拉取最新的keyvalue对。ConfigServer为了最大程度保证可用性采用了一种三层的存储架构设计,mysql本地文件内存缓存:!(https://oscim
Stella981 Stella981
2年前
Spring Cloud 系列之 Config 配置中心(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:SpringCloud系列之Config配置中心(一)(https://my.oschina.net/u/4126211/blog/4274304)本篇文章讲解Config如何实现配置中心自动刷新。配置中心自动刷新  点击链接观看:配置中心自动刷新视频(http
Stella981 Stella981
2年前
Docker部署freeswitch
1\.clone配置文件到本地服务器gitclonehttps://github.com/BetterVoice/freeswitchcontainer.git相关Dockerfile如下:Jenkins.FROMubuntu:16.04MAINTAINERThomasQuintan
Stella981 Stella981
2年前
Linux配置本地Yum仓库方法
Linux配置本地Yum仓库方法一、服务端:显示光盘的内容,挂载光驱设备root@WXR~mount/dev/cdrom/mnt/二、客户端:书写客户端配置文件,指定服务端位置
Stella981 Stella981
2年前
Spring Cloud Config
nvironmentRepository的默认实现使用Git后端,这对于管理升级和物理环境以及审核更改非常方便。要更改存储库的位置,可以在ConfigServer中设置“spring.cloud.config.server.git.uri”配置属性(例如application.yml)。如果您使用file:前缀进行设置,则应从本地存储库中工作,
Stella981 Stella981
2年前
Consul Config 使用Git做版本控制的实现
SpringCloudConfig原理!image(http://b.pigx.top/springcloudconfig.png)我们通过git把配置文件推送到远程仓库做版本控制,当版本发生变化的时候,远程仓库通过webhook机制推送消息给ConfigServer,ConfigS
Stella981 Stella981
2年前
Spring Cloud Alibaba Nacos Config 的使用
一、需求主要实现nacos作为配置中心的一些使用方法。二、实现功能1、加载productproviderdev.yaml配置文件2、实现配置的自动刷新3、实现加载多个配置文件
Easter79 Easter79
2年前
SpringCloud配置中心——ConfigServer搭建
_在实际应用中,一个系统的每个微服务都会有相同的配置,如数据库配置信息等等。为了将每个微服务的公共配置可以抽取出来。SpringCloud提供了Config配置中心的配置,作为配置中心(ConfigServer),提供给微服务(ConfigClient)读取并且加载配置。使用git仓库存放配置文件,SpringCloudConfig读取到之后会自
Easter79 Easter79
2年前
SpringCloud配置中心高可用搭建
本文通过configserver连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行。引入依赖<dependencies<dependency<groupIdorg.springframework.cloud</groupId<artifactIdsp
Easter79 Easter79
2年前
SpringCloud全家桶学习之消息总线
一、概述  ConfigClient(微服务)从ConfigServer端获取自己对应的配置文件,但是目前的问题是:当远程git仓库配置文件发生改变时,每次都是需要重启ConfigCient(微服务),如果有上百上千个微服务呢?我想我们不会一个个去重启每个微服务,也就是说如何让ConfigServer端通知到ConfigClient端?即Con
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k