SpringCloud Bus将分布式系统的节点与轻量级消息系统链接起来的框架,是对SpringCloud Config的加强,广播自动版的配置。
支持两种消息代理:RabbitMQ和Kafka
一、创建工程,添加依赖
spring-cloud-starter-config
spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-actuator
spring-boot-devtools
spring-boot-starter-web
spring-cloud-starter-bus-amqp
二、配置文件
server:
port: 3366
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名称
name: config #配置文件名称
profile: dev
uri: http://localhost:3344 #配置中心地址
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
三、启动类
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366{
public static void main(String[] args){
SpringApplication.run(ConfigClientMain3366.class,args);
}
}
四、controller类
@RestController
@RefreshScope
public class ConfigClientController{
@Value("${server.port}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String configInfo(){
return "serverPort: " + serverPort + "\t\n\n configInfo:" + configInfo;
}
}
五、服务端添加消息总线的支持(配置中心模块)
spring-cloud-starter-bus-amqp 依赖
配置文件
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:#github仓库名称
search-paths:
- springcloud-config
label: master
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#rabbitmq相关配置暴露bus刷新配置的端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh' #地址
六、运维
修改仓库的版本
刷新:
结果就是消费端节点都相应得到更新
如果是定点更新对应的节点,需要发送给对应的更新端点(微服务名:端口号)