服务总线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{}
相关推荐
CV_J7 小时前
安装kibana
java·elasticsearch·spring cloud·docker·容器
小马爱打代码1 天前
实时搜索:SpringCloud + Elasticsearch + Redis + Kafka
redis·elasticsearch·spring cloud
努力也学不会java1 天前
【Spring Cloud】环境和工程基本搭建
java·人工智能·后端·spring·spring cloud·容器
爱吃山竹的大肚肚1 天前
达梦(DM)数据库中设置表空间
java·数据库·sql·mysql·spring·spring cloud·oracle
这儿有个昵称2 天前
Java面试场景:从音视频到微服务的技术深挖
java·spring boot·spring cloud·微服务·面试·kafka·音视频
Remember_9932 天前
深入理解 Java String 类:从基础原理到高级应用
java·开发语言·spring·spring cloud·eclipse·tomcat
鸽鸽程序猿3 天前
【JavaEE】【SpringCloud】注册中心_nacos
java·spring cloud·java-ee
递归尽头是星辰3 天前
Spring Cloud Alibaba 核心理论体系:Nacos、Sentinel、Seata深度解析
spring cloud·nacos·sentinel·seata·微服务治理
lpfasd1233 天前
springcloud docker 部署问题排查与解决方案
spring·spring cloud·docker
蓝眸少年CY3 天前
(第七篇)spring cloud之Hystrix断路器
spring·spring cloud·hystrix