服务总线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{}
相关推荐
摇滚侠2 分钟前
2025最新 SpringCloud 教程,Gateway-断言-长短写法,笔记55
笔记·spring cloud·gateway
不被AI替代的BOT1 小时前
Spring Cloud Gateway WebFlux 模式架构分析
spring cloud·微服务
小坏讲微服务1 小时前
K8S 部署 Spring Cloud Alibaba 微服务企业实战完整使用
spring cloud·docker·微服务·云原生·容器·kubernetes·k8s
摇滚侠2 小时前
2025最新 SpringCloud 教程,Gateway-路由-工作原理,笔记54
笔记·spring cloud·gateway
摇滚侠2 小时前
2025最新 SpringCloud 教程,Gateway 路由-规则配置,笔记53
笔记·spring cloud·gateway
哈哈哈笑什么17 小时前
高并发分布式Springcloud系统下,使用RabbitMQ实现订单支付完整闭环的实现方案(反向撤销+重试+补偿)
分布式·spring cloud·rabbitmq
哈哈哈笑什么18 小时前
分布式高并发Springcloud系统下的数据图同步断点续传方案【订单/商品/用户等】
分布式·后端·spring cloud
哈哈哈笑什么20 小时前
企业级CompletableFuture并行化完整方案,接口从10s到100ms
java·后端·spring cloud
milixiang21 小时前
项目部署时接口短暂访问异常问题修复:Nacos+Gateway活跃节点监听
后端·spring cloud
-大头.1 天前
Spring Cloud避坑指南:10大实战经验
后端·spring·spring cloud