正在运行的微服务将使用存储在Spring Cloud Config文件中的新属性进行更新,而无需重新启动它们。通过这种方式,可以更新存储在集中式配置文件中的属性文件中的值,然后一次更新所有数百个微服务并使用新值。
1.修改全部Spring Boot微服务应用的pom.xml
添加两个依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.修改全部Spring Boot微服务应用的application.properties
增加如下配置:
management.endpoints.web.exposure.include=bus-refresh
management.endpoints.web.exposure.include=bus-env
#安装rabbitmq服务的计算机IP地址
spring.rabbitmq.host = localhost
#rabbitmq服务的端口
spring.rabbitmq.port = 5672
#rabbitmq服务的用户名和密码
spring.rabbitmq.username = guest
spring.rabbitmq.password = guest
3.增加@RefreshScope 和 @ConfigurationProperties注解
使Bean能够接收更新的配置属性:
- 增加@RefreshScope注解, 如果是用@Value注解获取的配置
- 使用@ConfigurationProperties 注解
4.刷新配置
验证上述所有设置是否生效,进行如下操作即可:
进行HTTP Post请求:http://localhost:8012/actuator/bus-refresh
- localhost:8012 – Spring Cloud Config 所在计算机的IP和服务端口号
- /actuator – 固定的
- /bus-refresh –固定值
完成请求后,配置就全部生效了。