Spring Cloud Config + Spring Cloud Bus 实现配置文件动态刷新(版本 Finchley.RC1)

Spring Cloud 应用篇 之 Spring Cloud Config(配置中心)一文中介绍了 Spring Cloud Config 的使用,已经实现了配置文件的统一管理(git 仓库),但是,每次修改配置文件后,还需要重新启动应用才能加载到修改后的配置文件,这还没有达到我们的目的,我们最终想要的是,修改完配置文件后,不需要重启我们的应用,就可以重新加载到修改后的配置文件,其实 Spring Cloud 已经为我们提供了这样的支持,那就是 Spring Cloud Bus 组件。

(一)简介

下面是官方文档介绍,我们可以使用消息中间件 RabbitMQ 或者 Kafka,只需要在项目中引用相应的依赖。至于消息中间件的使用,可以查阅相关资料。


  
  
  1. Spring Cloud Bus works by adding Spring Boot autconfiguration if it detects itself on the classpath. To enable the bus, add
  2. spring-cloud-starter-bus-amqp or spring-cloud-starter-bus-kafka to your dependency management. Spring Cloud takes care of the rest.
  3. Make sure the broker (RabbitMQ or Kafka) is available and configured. When running on localhost, you need not do anything. If you
  4. run remotely, use Spring Cloud Connectors or Spring Boot conventions to define the broker credentials

下面我们就基于上篇文章的项目进行改造即可。

(二)改造 spring-cloud-config 

 2.1 修改 spring-cloud-config 的 pom 文件,添加依赖

  
  
  1. <dependency>
  2. <groupId>org.springframework.cloud </groupId>
  3. <artifactId>spring-cloud-config-server </artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud </groupId>
  7. <artifactId>spring-cloud-starter-netflix-eureka-client </artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud </groupId>
  11. <artifactId>spring-cloud-starter-bus-amqp </artifactId>
  12. </dependency>
2.2 修改配置文件

从上面官方文档介绍来看,如果你的消息中间件是运行在本地(localhost),那么配置文件可以不用多余的配置,如果连接的是远程的消息中间件,那么就要配置消息中间件的相关信息如下:


  
  
  1. server:
  2. port: 8889
  3. spring:
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https: //github.com/shmilyah/cloud-config-samples.git
  9. search-paths: '{application}'
  10. application:
  11. name: spring-cloud-config
  12. rabbitmq:
  13. host: 192.168. 174.128
  14. port: 5672
  15. username: guest
  16. password: guest
  17. eureka:
  18. client:
  19. service-url:
  20. defaultZone: http: //localhost:8761/eureka/
  21. management:
  22. endpoints:
  23. web:
  24. exposure:
  25. include: '*'

我这里使用 rabbitmq,运行在我的虚拟机里,采用默认端口 5672,用户名和密码都是默认配置的 guest,这里其实不用配置也可以。

2.3 启动 spring-cloud-config

下面启动 spring-cloud-config,访问 http://192.168.174.128:15672,登录后,可以看到队列中已经多出一个


(三)改造 spring-cloud-config-client

3.1 修改 pom 文件
  1. <code class=”language-plain”><dependency>  
  2.     <groupId>org.springframework.cloud</groupId>  
  3.     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
  4. </dependency>  
  5. <dependency>  
  6.     <groupId>org.springframework.cloud</groupId>  
  7.     <artifactId>spring-cloud-config-client</artifactId>  
  8. </dependency>  
  9. <dependency>  
  10.     <groupId>org.springframework.boot</groupId>  
  11.     <artifactId>spring-boot-starter-web</artifactId>  
  12. </dependency>  
  13. <dependency>  
  14.     <groupId>org.springframework.boot</groupId>  
  15.     <artifactId>spring-boot-starter-actuator</artifactId>  
  16. </dependency>  
  17. <dependency>  
  18.     <groupId>org.springframework.cloud</groupId>  
  19.     <artifactId>spring-cloud-starter-bus-amqp</artifactId>  
  20. </dependency></code>  

   
   
  1. <dependency>
  2. <groupId>org.springframework.cloud </groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client </artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud </groupId>
  7. <artifactId>spring-cloud-config-client </artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot </groupId>
  11. <artifactId>spring-boot-starter-web </artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot </groupId>
  15. <artifactId>spring-boot-starter-actuator </artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.cloud </groupId>
  19. <artifactId>spring-cloud-starter-bus-amqp </artifactId>
  20. </dependency>
3.2 启动 spring-cloud-config-client

启动 spring-cloud-config-client,访问 http://192.168.174.128:15672,可以看到又多出一个 Queue


(四)验证动态刷新

此时 config-client 的 git 仓库配置文件内容如下:



访问 http://localhost:8989/hello,如下




下面我们修改下 git 仓库的配置文件内容为 hello: world,再次访问 http://localhost:8989/hello,结果发现内容并没有更新



其实我们还需要做一个操作,那就是发送一个 post 请求,curl -X POST http://localhost:8889/actuator/bus-refresh

注意,因为我们使用的 spring cloud 的是 F 版本,与低版本有所不同,这里暴露的端点是  /actuator/bus-refresh,这个在之前的文章中有讲解过。

ok,下面我们就发送一次 post 请求


这个时候,我们应该能刷新 config-client 的配置信息了吧,我们再次访问 http://localhost:8989/hello,结果发现内容还是没变,截图就不贴了,why?


这里面还有一个坑,我们还需要加一个注解 @RefreshScope 在需要刷新的地方

下面我们修改下 spring-cloud-config-client 的接口类,然后重启应用


  
  
  1. @SpringBootApplication
  2. @RestController
  3. @EnableEurekaClient
  4. @RefreshScope
  5. public class ConfigClientApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ConfigClientApplication.class, args);
  8. }
  9. @Value( "${hello}")
  10. private String hello;
  11. @RequestMapping( "hello")
  12. public String hello() {
  13. return hello;
  14. }
  15. }

访问 http://localhost:8989/hello,内容已经变为 world 了,因为我们重启了应用,所以配置内容更新了

OK,我们再次修改 config-client 的配置文件内容


再次发送 post 请求,curl -X POST http://localhost:8889/actuator/bus-refresh

我们可以从 spring-cloud-config-client 的后台日志看到,应用已经自动重新获取配置文件了,日志如下


再次访问 http://localhost:8989/hello


这个时候,内容已经自动刷新了,此时并没有重启应用,我们的初始目的已经达到了,但是这个时候还是有点麻烦,因为我们要手动去发送 post 请求,这个时候我们可以利用 GitHub 提供的 Webhooks

(五)使用 Webhooks


然后填写你要请求的 URL,Content type 选择 application/json


还有一个 Just the push event 的选择,这个选择是当你 push 内容到 GitHub 时,GitHub 就会去向你填写的 URL 自动发送 post 请求,就不用你自己手动发送请求了。

新版本有两个坑,就是上面所说的一个发送的 post 请求的 URL 有变化,暴露的端口为 /actuator/bus-refresh;还有一个就是要加一个 @RefreshScope 注解


源码下载:https://github.com/shmilyah/spring-cloud-componets
转载自:https://blog.csdn.net/hubo_88/article/details/80784730

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值