[英]Spring Cloud Config client doesn't get values from server
我一直在遵循Baeldung的本教程來設置Spring Cloud Config服務器和客戶端。 我相信我已經正確設置了服務器,因為轉到http:// localhost:8888 / service-config / development / master似乎可以正確顯示定義了user.role的json:
{"name":"service-config","profiles":["development"],"label":"master","version":"d30a6015a6b8cb1925f6e61cee22721f331e5783","state":null,"propertySources":[{"name":"file:///C:.../git/service-config-data/service-config-development.properties","source":{"user.role":"Developer"}},{"name":"file:///C:.../git/service-config-data/service-config.properties","source":{"user.role":"Developer"}}]}
但是,我似乎無法正確地將Spring Cloud Config客戶端連接到服務器-客戶端無法運行,無法解決@Value("${user.role}")
批注上的占位符錯誤。 我嘗試將默認值添加到注釋中,然后手動刷新值(使用@refreshscope
注釋),但無濟於事。
我已經通過運行帶有--debug --trace
標志的工具查看了調試和跟蹤日志,但是我找不到客戶端何時/是否正在調用服務器以及服務器實際上是哪個config-data .properties文件。尋找。 我對如何進行有點迷茫。
這是我的服務器application.properties
:
spring.application.name=service-config
server.port=8888
spring.cloud.config.server.git.uri=file:///${user.home}/git/service-config-data
spring.cloud.config.server.git.clone-on-start=false
spring.security.user.name=root
spring.security.user.password=toor
在service-config-data
文件夾中,里面有一些文件:
user.role=Developer
我嘗試了名為service-config.properties,service-config-client.properties,service-config-development.properties和service-config-client-development.properties的文件,但它們似乎都無法通過user.role
。
這是我的客戶:
@SpringBootApplication
@RestController
@RefreshScope
public class ServiceConfigClient {
@Value("${user.role:unknown}")
private String role;
@RequestMapping(
value = "/whoami/{username}",
method = RequestMethod.GET,
produces = MediaType.TEXT_PLAIN_VALUE)
public String whoami(@PathVariable("username") String username) {
return String.format("Hello! You're %s and you'll become a(n) %s...\n", username, role);
}
}
而我的客戶bootstrap.properties
:
spring.application.name=service-config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=root
spring.cloud.config.password=toor
management.endpoints.web.exposure.include=*
如何正確將客戶端連接到服務器,以便它可以成功提取配置數據? 謝謝。
在配置服務器中,關於客戶端服務名稱和相應屬性來源存在混淆。
您需要執行以下操作之一:
//client bootstrap.properties
spring.application.name=service-config
service-config
更改為service-config-client
通過將spring.cloud.config.name=service-config
添加到service-config-client的bootstrap.properties
中以及將spring-cloud-config-client
到並刪除spring-cloud-config-server
從我的pom.xml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.