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

一、实现原理

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会发布更新消息到消息总线的消息队列中,其他服务订阅到该消息就会信息刷新,从而实现整个微服务进行自动刷新。

二:实现方式

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

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

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

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

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

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

存在问题:

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

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

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

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

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

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

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

第一种配置:

启动rabbitmq

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

    • pom.xml

      1<dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-actuator</artifactId> 4 </dependency> 56 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-bus-amqp</artifactId> 9 </dependency>
    • bootstrap.yml

      1spring: 2 rabbitmq: 3 host: 192.168.180.112 4 port: 5672 5 username: guest 6 password: guest 7 8 #暴露全部的监控信息 9 management: 10 endpoints: 11 web: 12 exposure: 13 include: "*"
    • web层

      1@RestController 2@RequestScope 3@RequestMapping("/api/v1/product") 4public class ProductController { 5678 @Value("${server.port}") 9 private String port; 1011 @Autowired 12 private ProductService productService; 1314 /** 15 * 获取所有商品列表 16 * @return 17 */ 18 @RequestMapping("list") 19 public Object list(){ 20 return productService.listProduct(); 21 } 222324 /** 25 * 根据id查找商品详情 26 * @param id 27 * @return 28 */ 29 @RequestMapping("find") 30 public Object findById(int id){ 3132// try { 33// TimeUnit.SECONDS.sleep(2); 34// } catch (InterruptedException e) { 35// e.printStackTrace(); 36// } 37 Product product = productService.findById(id); 3839 Product result = new Product(); 40 BeanUtils.copyProperties(product,result); 41 result.setName( result.getName() + " data from port="+port ); 42 return result; 43 } 4445} 46
    • 测试,要手动发送POST http://localhost:8773/actuator/bus-refresh

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

第二种配置:

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

  • config-server

    • pom.xml

      1 1 <dependency> 2 2 <groupId>org.springframework.boot</groupId> 3 3 <artifactId>spring-boot-starter-actuator</artifactId> 4 4 </dependency> 5 56 6 <dependency> 7 7 <groupId>org.springframework.cloud</groupId> 8 8 <artifactId>spring-cloud-starter-bus-amqp</artifactId> 9 9 </dependency> 1010 <dependency> 1111 <groupId>org.springframework.cloud</groupId> 1212 <artifactId>spring-cloud-config-server</artifactId> 1313 </dependency>
    • application.yml

      1#服务名称 2spring: 3 application: 4 name: config-server 5 cloud: 6 config: 7 server: 8 git: 9 uri: http://192.168.180.112/root/test.git 10 username: root 11 password: *********** 12 default-label: master 13 rabbitmq: 14 host: 192.168.180.112 15 username: guest 16 password: guest 17 port: 5672 18management: 19 endpoints: 20 web: 21 exposure: 22 include: "*" 232425#服务的端口号 26server: 27 port: 9100 282930#指定注册中心地址 31eureka: 32 client: 33 serviceUrl: 34 defaultZone: http://localhost:8761/eureka/
  • 发送请求:http://192.168.137.1:9100/actuator/bus-refresh,会发现所有的服务都会拉取信息。

都会加进队列中。

点赞
收藏

评论区

加载中...

相关推荐

Nacos Config源代码分析(一)

NacosConfig提供了配置管理的功能,它允许用户在nacos上配置keyvalue对,并在客户端订阅需要的配置。当配置发生变更时,订阅的客户端会获得通知,随后拉取最新的keyvalue对。ConfigServer为了最大程度保证可用性采用了一种三层的存储架构设计,mysql本地文件内存缓存:!(https://oscim

Spring Cloud 系列之 Config 配置中心(二)

本篇文章为系列文章,未读第一集的同学请猛戳这里:SpringCloud系列之Config配置中心(一)(https://my.oschina.net/u/4126211/blog/4274304)本篇文章讲解Config如何实现配置中心自动刷新。配置中心自动刷新  点击链接观看:配置中心自动刷新视频(http

Docker部署freeswitch

1\.clone配置文件到本地服务器gitclonehttps://github.com/BetterVoice/freeswitchcontainer.git相关Dockerfile如下:Jenkins.FROMubuntu:16.04MAINTAINERThomasQuintan

Linux配置本地Yum仓库方法

Linux配置本地Yum仓库方法一、服务端:显示光盘的内容,挂载光驱设备root@WXR~mount/dev/cdrom/mnt/二、客户端:书写客户端配置文件,指定服务端位置

Spring Cloud Config

nvironmentRepository的默认实现使用Git后端,这对于管理升级和物理环境以及审核更改非常方便。要更改存储库的位置,可以在ConfigServer中设置“spring.cloud.config.server.git.uri”配置属性(例如application.yml)。如果您使用file:前缀进行设置,则应从本地存储库中工作,

Consul Config 使用Git做版本控制的实现

SpringCloudConfig原理!image(http://b.pigx.top/springcloudconfig.png)我们通过git把配置文件推送到远程仓库做版本控制,当版本发生变化的时候,远程仓库通过webhook机制推送消息给ConfigServer,ConfigS