springcloud中bus总线的配置方法(rabbitMQ)
使用方法:
服务端
1.在config服务端的pom文件加两个依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
2.在application.yml里做配置
rabbitmq:
host: 120.78.179.123
port: 5672
username: cc
password: 123456
management: #暴露出发消息总线地址
endpoints:
web:
exposure:
include: bus-refresh
其中rabbitmq是在spring下面,而management是定格写
客户端
1.配置客户端,添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud‐stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.配置application.yml
rabbitmq:
host: 120.78.179.123
port: 5672
username: cc
password: 123456
3.在客户端控制层加上:@RefreshScope
这样完成了配置:测试,修改了码云上的配置文件,代码里面能输出吗?
在码云上配置
sms:
ip: 6666666666666
在代码里面
@Value("${sms.ip}")
private String ip;
在取出来输出一下
改了,没有变化,原因是没有去触发configserver,告诉他已更新。
用postman执行:
http://localhost:5052/actuator/bus-refresh
其中localhost:5052是本地configserver所在的端口,actuator是固定写法,bus-refresh是对应configserver里面的 include: bus-refresh。
这是手动触发。
自动触发:
URL那里配置http://localhost:5052/actuator/bus-refresh,也就是config服务端的地址。但是不能这么写,因为码云不能访问自己本地,需要在部署上线时换成服务器地址(可访问的)。