簡體   English   中英

Spring Cloud Config客戶端無法從服務器獲取值

[英]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=*

如何正確將客戶端連接到服務器,以便它可以成功提取配置數據? 謝謝。

在配置服務器中,關於客戶端服務名稱和相應屬性來源存在混淆。

您需要執行以下操作之一:

  1. 更改客戶端服務名稱:
//client bootstrap.properties
spring.application.name=service-config
  1. 將git repo中的文件夾名稱從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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM