环境搭建
演示广播效果,增加复杂度,再以3355为模板再制作一个3366
新建cloud-config-client-3366

pom
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 |   <dependency><groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
 </dependency>
 
 <dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <optional>true</optional>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>
 
 | 
yml
使用bootstrap.yml

| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | server:port: 3366
 
 spring:
 application:
 name: config-client
 cloud:
 config:
 label: master
 name: config
 profile: dev
 uri: http://localhost:3344
 
 eureka:
 client:
 service-url:
 defaultZone: http://eureka7001.com:7001/eureka
 management:
 endpoints:
 web:
 exposure:
 include: "*"
 
 
 | 
主启动类
创建ConfigClientMain3366

| 12
 3
 4
 5
 6
 7
 
 | @EnableEurekaClient@SpringBootApplication
 public class ConfigClientMain3366 {
 public static void main(String[] args) {
 SpringApplication.run( ConfigClientMain3366.class,args);
 }
 }
 
 | 
controller
创建ConfigClientController

| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | @RestController@RefreshScope
 public class ConfigClientController {
 
 @Value("${server.port}")
 private String serverPort;
 
 @Value("${config.info}")
 private String configInfo;
 
 
 @GetMapping("/configInfo")
 public String getConfigInfo(){
 return "serverPort:"+serverPort+"\t\n\n configInfo: "+configInfo;
 }
 
 
 }
 
 | 
动态刷新全局广播
配置中心服务端
给cloud-config-center-3344配置中心服务端添加消息总线支持
pom
| 12
 3
 4
 
 | <dependency><groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 </dependency>
 
 | 
yml
添加rabbitmq相关配置,暴露bus刷新配置的端点

| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | rabbitmq:
 host: localhost
 port: 5672
 username: guest
 password: guest
 
 
 
 management:
 endpoints:
 web:
 exposure:
 include: 'bus-refresh'
 
 | 
配置中心客户端
给cloud-config-center-3355,cloud-config-center-3366客户端添加消息总线支持
pom
| 12
 3
 4
 
 | <dependency><groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 </dependency>
 
 | 
yml
3355

3366

| 12
 3
 4
 5
 6
 
 | rabbitmq:
 host: localhost
 port: 5672
 username: guest
 password: guest
 
 | 
测试
启动7001 3344 3355 3366
 )
)
3344

3355

3366

目前都能获取到github上的文件信息
将github上的master分支下的config-dev.yml的version改为4

此时刷新3344页面,version是最新的

这时候我们的3355和3366都应该还是version=3

我们向3344发送post请求curl -X POST "http://localhost:3344/actuator/bus-refresh"

此时再刷新3355 3366页面


全部更新成功!一次发送,处处生效,向3344配置中心服务端发送post请求,广播更新其他的配置中心客户端!
动态刷新定点通知
有时候我们更新配置之后不想全部通知,只想定点通知,指定具体某一个实例生效而不是全部。
公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}
destination代表微服务名称加端口号
/bus/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类指定需要更新配置的服务或实例
我们这里以刷新运行在3355端口上的config-client为例 只通知3355 不通知3366
将github上的master分支下的config-dev.yml的version改为5

3344

3355 和 3366都应该是4

发送curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"

再次刷新查看3355和3366页面

发现3355的更新成功,3366没有被广播通知,测试成功!