在不重启微服务的情况
下更新配置如何来实现呢? 我们使用SpringCloudBus来实现配置的自动更新。
引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring‐cloud‐bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring‐cloud‐stream‐binder‐rabbit</artifactId>
</dependency>
配置文件
rabbitmq: host: 127.0.0.1 management: #暴露触发消息总线的地址 endpoints: web: exposure: include: bus‐refresh
客户端
引入依赖
<dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐stream‐binder‐rabbit</artifactId>
</dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring‐boot‐starter‐actuator</artifactId>
</dependency>
配置文件中配置rabbitmq
rabbitmq:
host: 127.0.0.1
测试
Url: http://127.0.0.1:8888/actuator/bus-refresh Method: post
自定义配置文件的读取
@RefreshScope //注解@RefreshScope 此注解用于刷新配置
@RestController
public class TestController {
@Value("${sms.ip}") private String ip; @RequestMapping(value = "/ip", method = RequestMethod.GET)
public String ip() {
return ip;
}
}