[英]Spring Cloud - Configuration client caching properties
當我從我的屬性存儲庫中更改一個值並重新啟動 Spring Cloud Config Server 時,這些更改不會反映在它的使用者身上。
我的微服務/application.properties:
spring.application.name=my-service
spring.cloud.config.uri=http://localhost:8888
服務控制器.java
@RestController
public class MyServiceController {
@Autowired
private Configuration configuration;
@GetMapping("/my-service")
public MyServiceBean retrieveMyServiceProperties() {
// show propertie's values
return new MyServiceBean(configuration.getPropertie1(), configuration.getPropertie2());
}
}
spring-cloud-config-server/application.properties
server.port=8888
spring.application.name=spring-cloud-config-server
spring.cloud.config.server.git.uri=file://path
Git 倉庫
我的服務.properties
my-service.propertie1=1
my-service.propertie2=2
當我向localhost:8080/my-service發送 GET 請求時,這就是我得到的結果:
{
"propertie1":1,
"propertie2":2
}
好吧,沒關系! 但是,如果我更改my-service.properties
並重新啟動 Spring Cloud Config Server,這些更改不會反映MyServiceController
。 我確實需要重新啟動my-microservice應用程序,以使更改生效。 這是正常行為嗎? 我的意思是,如果這是遠程的,那么應該配置是否緩存。
為了更新配置,我向localhost:8080/actuator/refresh
發送了一個POST
請求。
默認情況下, /refresh
不會在執行器端點中公開。
我確實在 application.properties 中使用了以下行:
management.endpoints.web.exposure.include=*
然后,向上面的端點發送一個沒有正文的POST
請求。
要更新您的客戶端應用程序,最好使用像 RabbitMQ 或 Apache Kafka 這樣的消息代理。 這個過程分為三個層次:
客戶端應用程序和配置服務器訂閱消息代理中的特定主題 ( /refresh )。
配置服務器將刷新事件發送到該主題( /refresh ),一旦它被更新。 (例如 application.properties 文件在 git 中更新)。
所有客戶端應用程序都在監聽刷新事件,當它們收到刷新消息時,它們將被更新
簡而言之,我們可以使用pub-sub模型來更新我們的客戶端應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.