Spring Cloud Bus实现动态配置更新

actuator方式刷新配置

客户端添加如下依赖:

一、依赖

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

二、@RefreshScope

在需要刷新的类上加@RefreshScope
Spring Cloud Bus实现动态配置更新

三、配置文件

application.properties添加

management.endpoints.web.exposure.include=refresh

四、修改配置文件后,执行刷新接口生效

post请求:
http://localhost:8086/actuator/refresh
注:localhost:8086为客户端访问地址

Gitee WebHooks自动触发
Spring Cloud Bus实现动态配置更新

Spring Cloud Bus实现动态配置更新
注:上面的localhost要换成外网可访问的地址

Spring Cloud Bus方式刷新配置

Spring Cloud Config Server

一、添加依赖

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

二、配置文件

添加kafka,(快速搭建Kafka单机运行,快速上手使用

spring.kafka.bootstrap-servers=10.1.255.101:9092
spring.kafka.consumer.group-id=config-server

management.endpoints.web.exposure.include=bus-refresh
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
spring.cloud.bus.refresh.enabled=true

三、如有修改配置文件,执行刷新接口

POST请求:
http://localhost:8081/actuator/bus-refresh
注:localhost:8081为Spring Cloud Config Server

使用Gitee WebHooks代码推送自动触发刷新接口,由于使用/actuator/bus-refresh会多加些参数导致报错,所以使用/monitor,path=监控文件,比如 news-user-dev.properties,* 为所有。
POST请求:
http://3hf8110135.zicp.vip/monitor?path=*
注:3hf8110135.zicp.vip为内网穿透地址
Spring Cloud Bus实现动态配置更新

Spring Cloud Config Client

一、添加依赖

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

二、配置文件

添加kafka

management.endpoints.web.exposure.include=refresh

spring.kafka.bootstrap-servers=10.1.255.101:9092
spring.kafka.consumer.group-id=config-server

问题排查:
Connection to node 0 could not be established. Broker may not be available.

上一篇:SpringBoot (3) WebServerApplicationContext


下一篇:django-rest-framework-simplejwt