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/服务名:端口号"
相关推荐
猿究院--Cu-Sn合金1 分钟前
Spring Boot + DeepSeek 求职 Agent 实践:工具注册、多轮调用与简历 JD 对比
数据库·人工智能·spring boot·后端·deepseek
程序员cxuan2 分钟前
别学 subagent 了,很可能要凉了。
后端·agent·ai编程
小咕聊编程9 分钟前
基于Django的个人摄影博客网站设计与开发
后端·python·django
未秃头的程序猿1 小时前
@ConditionalOnMissingBean 把我配好的 RabbitMQ 手动 ack 吃掉了
后端·rabbitmq
回家路上绕了弯1 小时前
当 AI 成为队友:团队协作使用 AI 的实践与思考
后端
陈随易1 小时前
副业搞钱点子之独立开发
前端·后端·程序员
MetaLite1 小时前
Spring Boot 加了 @Valid,嵌套参数为什么还没校验?
java·spring boot·后端
霸道流氓气质1 小时前
Lingua 语言检测库:n-gram 统计模型、置信度阈值调优与 Spring Boot 分层检测管道的生产级实战
java·spring boot·后端
霸道流氓气质1 小时前
智能表单生成器:结构化输出契约、JSON Schema 驱动渲染与 EAV/JSONB 混合存储的 Vue3 + Spring AI 生产级实战
人工智能·spring·json
geovindu2 小时前
rust: Read-Write Lock Pattern
开发语言·后端·设计模式·rust·读写锁模式·同步型模式