服务总线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 天前
专项攻克-springcloud及其组件
后端·spring·spring cloud
砍材农夫2 天前
物联网实战|Spring Boot MQTT平台 |emqx broker实战
spring boot·spring·spring cloud·mybatis
篮框坏了2 天前
配置改了不生效?我翻出 8 个坑,没一个报错
后端·spring cloud
wangjialelele2 天前
Spring Cloud 全体系学习笔记(REST、Nacos、Feign、Gateway)
java·spring boot·笔记·学习·spring·spring cloud·gateway
攻城有术2 天前
专项攻克——spring、springMVC、springBoot、springCloud的启动流程
spring boot·spring·spring cloud
空谷有来人4 天前
springcloud中配置ci/cd集成docker的配置文件
spring cloud·ci/cd·docker
Pluchon4 天前
Java个人综合项目——萌部落社区V2.0
java·python·spring·spring cloud·postman·idea
豆瓣鸡5 天前
Spring Cloud Gateway 核心原理:从请求转发到微服务流量治理
网关·spring cloud·微服务
过往的时光5 天前
Spring Boot、Spring Cloud 与 Spring Cloud Alibaba 版本对照终极指南
java·spring boot·spring cloud