SpringBoot2.x使用SpringCloud Bus刷新配置

本文介绍使用RabbitMQ作为SpringCloud消息组件刷新微服务配置的方法。需在config - client的pom.xml导入RabbitMQ和actuator依赖,在config - server和config - client的yml中添加配置,在config - client使用配置的类加@RefreshScope注解,修改配置后通过Postman发送请求获取最新配置。

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

使用的是RabbitMQ作为SpringCloud的消息组件去刷新更新微服务的配置

1、在config-client的pom.xml中导入RabbitMQ的依赖,actuator一定要导入,不然会影响下面的yml中的配置

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、在config-server和config-client的yml中加入以下配置

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

config-client的yml中加入RabbitMQ的配置

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

3、在config-client使用配置的类加上@RefreshScope注解

@RefreshScope
@RestController
public class ApiController {

    @Value("${foo}")
    String foo;

    @RequestMapping("/foo")
    public String getFoo() {
        return foo;
    }
}

4、依次启动config-server和config-client,在github上修改配置文件,通过Postman发送http://localhost:8762/actuator/bus-refresh的post请求,这里要注意的是SpringBoot1.5.x的请求是http://localhost:8762/bus/refresh,2.x变成:http://localhost:8762/actuator/bus-refresh

5、请求http://localhost:8762/foo就能拿到最修改的配置文件了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值