(三)springcloud 消息总线-spring cloud bus

本文详细介绍了如何在Spring Cloud微服务架构中集成RabbitMQ实现配置动态刷新。包括了依赖添加、配置项设置、服务启动日志解析及通过HTTP触发配置刷新的实践过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RabbitMQ

1、RabbitMQ环境:略

2、每个服务都添加依赖,或者聚合工程中统一添加

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

3、配置

每个服务增加开放监控端点,和RabbitMQ链接参数配置

spring:
    config: 
        ...
  rabbitmq:
    port: 5672
    username: jianwei
    password: 654321
    host: localhost

eureka: 
    ...

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

或者在配置仓库增加公共配置:

# 全部开放监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

# 链接rabbitmq
spring:
  rabbitmq:
    port: 5672
    username: jianwei
    password: 654321
    host: localhost

4、启动服务

打印日志:

这里在网上看到很多个版本,还是以控制台打印的为准

Mapped "{[/actuator/bus-env/{destination}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v2+json || application/json]}" 
Mapped "{[/actuator/bus-env],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v2+json || application/json]}" 
Mapped "{[/actuator/bus-refresh],methods=[POST]}"  
Mapped "{[/actuator/bus-refresh/{destination}],methods=[POST]}" 

5、验证:

启动配置服务端和客户端并访问:http://localhost:4001/config/param

响应结果:config-client-example-value bus version0

修改配置文件:

value: config-client-example-value bus version1

向集中配置服务发送请求

POST /actuator/bus-refresh HTTP/1.1
Content-Length: 0
Host: localhost:8888
Content-Type: application/json

HTTP/1.1 204
Date: Mon, 22 Apr 2019 09:36:11 GMT

再次访问:http://localhost:4001/config/param

响应结果:config-client-example-value bus version1

6、指定配置刷新:

/actuator/bus-refresh

通配符匹配刷新:

/actuator/bus-refresh?destination=serviceName:** // 触发serviceName的所有实例

转载于:https://www.cnblogs.com/zuier/p/10752089.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值