SpringBoot实战电商项目mall(20k+star)地址:github.com/macrozheng/…git
Spring Cloud Bus 使用轻量级的消息代理来链接微服务架构中的各个服务,能够将其用于广播状态更改(例如配置中心配置更改)或其余管理指令,本文将对其用法进行详细介绍。github
咱们一般会使用消息代理来构建一个主题,而后把微服务架构中的全部服务都链接到这个主题上去,当咱们向该主题发送消息时,全部订阅该主题的服务都会收到消息并进行消费。使用 Spring Cloud Bus 能够方便地构建起这套机制,因此 Spring Cloud Bus 又被称为消息总线。Spring Cloud Bus 配合 Spring Cloud Config 使用能够实现配置的动态刷新。目前 Spring Cloud Bus 支持两种消息代理:RabbitMQ 和 Kafka,下面以 RabbitMQ 为例来演示下使用Spring Cloud Bus 动态刷新配置的功能。web
rabbitmq-plugins enable rabbitmq_management
复制代码
使用 Spring Cloud Bus 动态刷新配置须要配合 Spring Cloud Config 一块儿使用,咱们使用上一节中的config-server、config-client模块来演示下该功能。spring
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码
server:
port: 8904
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/macrozheng/springcloud-config.git
username: macro
password: 123456
clone-on-start: true # 开启启动时直接从git获取配置
rabbitmq: #rabbitmq相关配置
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh'
复制代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
复制代码
server:
port: 9004
spring:
application:
name: config-client
cloud:
config:
profile: dev #启用环境名称
label: dev #分支名称
name: config #配置文件名称
discovery:
enabled: true
service-id: config-server
rabbitmq: #rabbitmq相关配置
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
management:
endpoints:
web:
exposure:
include: 'refresh'
复制代码
# 修改前信息
config:
info: "config info for dev(dev)"
# 修改后信息
config:
info: "update config info for dev(dev)"
复制代码
update config info for dev(dev)
复制代码
WebHooks至关因而一个钩子函数,咱们能够配置当向Git仓库push代码时触发这个钩子函数,这里以Gitee为例来介绍下其使用方式,这里当咱们向配置仓库push代码时就会自动刷新服务配置了。bootstrap
springcloud-learning
├── eureka-server -- eureka注册中心
├── config-server -- 配置中心服务
└── config-client -- 获取配置的客户端服务
复制代码
mall项目全套学习教程连载中,关注公众号第一时间获取。架构