- spring.rabbitmq.host=localhost
- spring.rabbitmq.port=5672
- spring.rabbitmq.username=young
- spring.rabbitmq.password=young
- spring.cloud.bus.enabled=true
- spring.cloud.bus.trace.enabled=true
- management.endpoints.web.exposure.include=bus-refresh
4. springcloud-feign-client 模块的启动类上加上注解:@RefreshScope
- package com.haly;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.cloud.openfeign.EnableFeignClients;
- @SpringBootApplication
- @EnableFeignClients
- @EnableDiscoveryClient
- @RefreshScope
- public class SpringcloudFeignClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringcloudFeignClientApplication.class, args);
- }
- }
5. springcloud-feign-client 模块的测试类 FeignController, 测试上一章节的 / testconfig 方法, 具体内容可以参考: 一起来学 Spring Cloud | 第七章: 分布式配置中心 (Spring Cloud Config)
- package com.haly.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.Web.bind.annotation.GetMapping;
- import org.springframework.Web.bind.annotation.RequestParam;
- import org.springframework.Web.bind.annotation.RestController;
- import com.haly.romote.FeignRemoteService;
- @RestController
- public class FeignController {
- @Autowired
- FeignRemoteService feignRemoteService;
- @Value("${configword}")
- String configword;
- @GetMapping(value = "/getHello")
- public String getHello(@RequestParam String name) {
- return feignRemoteService.hello(name);
- }
- @GetMapping(value = "/testzuul")
- public String testzuul(@RequestParam String name) {
- return name +", 这是 springcloud-feign-client 的服务接口";
- }
- @GetMapping(value = "/testconfig")
- public String testconfig(@RequestParam String name) {
- return name +",git 配置值:" + configword ;
- }
- }
6. 运行项目
启动 springcloud-eureka-server, 启动 springcloud-config-server, 启动 springcloud-config, 最后启动 springcloud-feign-client 模块
为了测试配置修改, 多个服务实例都能更新, 就启动两个 springcloud-feign-client 实例, 端口分别是 9600,9601,sts 启动两个实例 (端口为 9600 时启动项目, 然后将端口改成 9601, 再启动项目).
浏览器输入: http://localhost:9600/testconfig?name=young 码农 或者输入 http://localhost:9601/testconfig?name=young 码农
页面展示结果: young 码农, Git 配置值: NewConfig !
7. 修改 Git 配置, 重新运行项目
这时我们去代码仓库将 configword 的值改为 "update config", 即改变配置文件 configword 的值. 如果是传统的做法, 需要重启服务, 才能达到配置文件的更新.
现在, 我们只需要发送 post 请求: http://localhost:8881/actuator/bus-refresh, 你会发现 springcloud-feign-client 会重新读取配置文件, 接着我们查看页面运行结果.
浏览器输入: http://localhost:9600/testconfig?name=young 码农 或者输入 http://localhost:9601/testconfig?name=young 码农
页面展示结果: young 码农, Git 配置值: update config !
二, 总结:
使用 "destination" 参数,/actuator/bus-refresh 接口可以指定服务, 例如 "/actuator/bus-refresh?destination=client:**", 即刷新服务名为 client 的所有服务.
通过上面的测试, 我们可以知道当 Git 文件更改的时候, 用 post 向端口为 8882 的 config-client 发送请求 / bus/refresh/; 此时 8882 端口会发送一个消息, 由消息总线向其他服务传递, 从而使整个微服务集群都达到更新配置文件.
引入程序猿 DD 画的一张图片, 简单理解一下刷新原理:
最后项目目录结构:
来源: https://www.cnblogs.com/haly/p/11069622.html