服务总线SpringCloudBus

1 简介

为了使用户微服务能够及时感知到Git仓库中配置文件的修改,可以使用SpringCloud Bus来实现配置的自动更新。

SpringCloud Bus底层是基于RabbitMQ实现的,默认使用本地的消息队列服务。它是用轻量级的消息代理将分布式的系统连接起来,用于广播配置文件的更改或服务的而监控管理。

SpringCloud Bus可选的消息代理有RabbitMQ和Kafka。

2 集成SpringCloudBus

2.1 改造配置中心
  • 在config-server中添加依赖
xml 复制代码
 <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>
  • 更新配置文件
yml 复制代码
server:
  port: 12000
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/goheima/heima-config.git
  # 配置rabbitmq信息;如果是都与默认值一致则不需要配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
management:
  endpoints:
    web:
      exposure:
        # 暴露触发消息总线的地址
        include: bus-refresh
2.2 改造用户服务
  • 在user-service中添加依赖
xml 复制代码
<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>
  • 配置rabbitmq信息
yml 复制代码
  # 配置rabbitmq信息;如果是都与默认值一致则不需要配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  • 在Controller添加配置刷新
java 复制代码
@RefreshScope
public class UserController{}
相关推荐
递归尽头是星辰1 天前
Spring Cloud Gateway架构全景:从底层线程模型到网关治理实战
spring cloud·微服务·微服务网关·网关治理·网关底层原理
让我上个超影吧2 天前
天机学堂——多级缓存
java·spring boot·spring cloud
CTO Plus技术服务中2 天前
2026版Java web高并发面试题和参考答案
java·jvm·spring·spring cloud·面试·tomcat·java-consul
瑶山2 天前
Spring Cloud微服务搭建二、分布式定时任务Quartz+MySQL接入
分布式·mysql·spring cloud·微服务·quartz
鸽鸽程序猿3 天前
【JavaEE】【SpringCloud】网关_GateWay
spring cloud·java-ee·gateway
蓝眸少年CY3 天前
(第十三篇)spring cloud之Sleuth分布式链路跟踪
分布式·spring·spring cloud
wfsm3 天前
【无标题】
spring cloud
短剑重铸之日3 天前
《SpringCloud实用版》 Gateway 4.3.x 保姆级实战:路由 + 限流 + 鉴权 + 日志全覆盖
java·后端·spring cloud·架构·gateway
ZealSinger3 天前
Nacos2.x 内存注册表:从服务调用链路深入理解
java·spring boot·spring·spring cloud·nacos
编程彩机3 天前
互联网大厂Java面试:从微服务到分布式事务的技术深度解析
java·spring cloud·微服务·分布式事务·saga·电商平台