SpringCloud-Bus

一、介绍

(1)bus搭配config可以实现客户端配置自动刷新

(2)bus支持两种消息代理,rabbitmq和kafka

(3)使用topic模式分发消息

二、项目搭建(广播)

(1)客户端和服务配置中心pom.xml增加依赖

java 复制代码
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

(2)修改客户端的bootstrap.yml,添加rabbitmq配置、暴露actuator的refresh端点

java 复制代码
server:
  port: 3355

spring:
  application:
    name: cloud-config-client-service
  cloud:
    config:
      label: master #分支名
      name: application # - 号前缀
      profile: prod # - 号后缀
      uri: http://localhost:3344 #配置中心地址
  rabbitmq:
    host: 192.168.0.166
    port: 5672
    username: guest
    password: guest

eureka:
  client:
    #    客户端设置为true
    register-with-eureka: true
    #    客户端设置为true
    fetch-registry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
  instance:
    instance-id: configClient3355
    prefer-ip-address: true

management:
  endpoints:
    web:
      exposure:
        include: "*"

(3)修改配置中心服务端的bootstrap.yml,暴露actuator的bus-refresh端点

java 复制代码
server:
  port: 3344

spring:
  application:
    name: cloud-config-service
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/feifanwsh/config.git
      label: master
  rabbitmq:
    host: 192.168.0.166
    port: 5672
    username: guest
    password: guest

eureka:
  client:
    #    客户端设置为true
    register-with-eureka: true
    #    客户端设置为true
    fetch-registry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
  instance:
    instance-id: config3344
    prefer-ip-address: true

management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"

(4)运行

java 复制代码
curl -X POST "http://localhost:3344/actuator/bus-refresh"

三、单播

运行

java 复制代码
curl -X POST "http://localhost:3344/actuator/bus-refresh/服务名:端口号"
相关推荐
几颗流星1 分钟前
Rust 常用语法速记 - 错误处理
后端·rust
lypzcgf4 分钟前
Coze源码分析-资源库-创建知识库-后端源码-应用/领域/数据访问
后端·go·coze·coze源码分析·智能体平台·ai应用平台·agent平台
LaoZhangAI1 小时前
Google Gemini AI图片编辑完全指南:50+中英对照提示词与批量处理教程(2025年9月)
前端·后端
小枫编程1 小时前
Spring Boot 调度任务在分布式环境下的坑:任务重复执行与一致性保证
spring boot·分布式·后端
用户11481867894841 小时前
从零搭建 Vue3 + Nest.js 实时通信项目:4 种方案(短轮询 / 长轮询 / SSE/WebSocket)
前端·后端
LaoZhangAI1 小时前
Google Gemini Nano与Banana AI完整部署指南:2025年轻量级AI解决方案
前端·后端
Java水解1 小时前
spring中的@SpringBootTest注解详解
spring boot·后端
似水流年流不尽思念1 小时前
Java线程状态转换的详细过程
后端
尚学教辅学习资料1 小时前
基于Spring Boot的家政服务管理系统+论文示例参考
java·spring boot·后端·java毕设
Java水解1 小时前
从 “Hello AI” 到企业级应用:Spring AI 如何重塑 Java 生态的 AI 开发
后端·spring