环境搭建

演示广播效果,增加复杂度,再以3355为模板再制作一个3366

新建cloud-config-client-3366

image-20200710100127464

pom

1
2
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

image-20200710103540342

1
2
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

image-20200710100805390

1
2
3
4
5
6
7
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3366.class,args);
}
}

controller

创建ConfigClientController

image-20200710101018691

1
2
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

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

yml

添加rabbitmq相关配置,暴露bus刷新配置的端点

image-20200710102434234

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#rabbitmq相关配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest


#rabbitmq相关配置,暴露bus刷新配置的端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh'

配置中心客户端

cloud-config-center-3355cloud-config-center-3366客户端添加消息总线支持

pom

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

yml

3355

image-20200710103403640

3366

image-20200710103742503

1
2
3
4
5
6
#rabbitmq相关配置 15672是Web管理界面的端口,5672是MQ访问端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

测试

启动7001 3344 3355 3366

image-20200710104046795)image-20200710104059132

3344

image-20200710104228083

3355

image-20200710104239229

3366

image-20200710104248844

目前都能获取到github上的文件信息

将github上的master分支下的config-dev.yml的version改为4

image-20200710104440494

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

image-20200710104514566

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

image-20200710104555209

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

image-20200710104717072

此时再刷新3355 3366页面

image-20200710104743183

image-20200710104757506

全部更新成功!一次发送,处处生效,向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

image-20200710105700972

3344

image-20200710105645394

3355 和 3366都应该是4

image-20200710105736201

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

image-20200710105814909

再次刷新查看3355和3366页面

image-20200710105851588

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