Spring Cloud Config在运行时刷新配置
Spring Cloud提供支持以使配置参数可随向/actuator/refresh端点的POST请求重新加载。
- 添加Spring Boot Actuator依赖项:
Maven坐标:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Gradle坐标:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
}
- 将
@RefreshScope添加到Spring配置类中,以使参数在运行时可重新加载。 - 将
management.endpoints.web.exposure.include=refresh添加到application.properties中,以允许不受限制地访问/actuator/refresh。 -
使用
gcloud更新属性:$ gcloud beta runtime-config configs variables set \ myapp.queue_size 200 \ --config-name myapp_prod
-
发送POST请求到刷新端点:
$ curl -XPOST https://myapp.host.com/actuator/refresh