使用的是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就能拿到最修改的配置文件了。