spring.cloud.config.server.git.refreshRate 属性在 Config Server 中配置,并控制拉取更新的频率(如果有),来自 Git。否则,配置服务器的默认行为是仅在某些客户端请求其配置时才连接到 Git 存储库。
Git Repo -> Config Server
不影响 Config Server 与其客户端的通信。
Config Server -> Spring Boot app (Config Server clients)
使用 Config Server 客户端构建的 Spring Boot 应用程序拉在启动期间从 Config Server 中获取所有配置。要使它们能够动态更改初始加载的配置,您需要在 Spring Boot 应用程序 也就是 Config Server 客户端 中执行以下步骤:
- 在类路径中包含 Spring Boot Actuator,例如使用 Gradle:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
- 通过
management.endpoints.web.exposure.include=refresh 属性启用执行器刷新端点
- 在你的代码中用
@RefreshScope注释持有属性的Bean,例如:
@RefreshScope
@Service
public class BookService {
@Value("${book.default-page-size:20}")
private int DEFAULT_PAGE_SIZE;
//...
}
- 随着应用程序的运行,将一些属性更改提交到 repo:
git commit -am "Testing property changes"
- 例如,通过向 刷新端点 发送 HTTP POST 请求来触发更新过程(使用 httpie 并假设您的应用在端口 8080 上本地运行:
http post :8080/actuator/refresh
响应应该如下所示,指示哪些属性(如果有)已更改
HTTP/1.1 200
Connection: keep-alive
Content-Type: application/vnd.spring-boot.actuator.v3+json
Date: Wed, 30 Jun 2021 10:18:48 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked
[
"config.client.version",
"book.default-page-size"
]