簡體   English   中英

Spring Cloud Config Server - 占位符標簽

[英]Spring Cloud Config Server - Placeholder Label

我使用Spring Boot開發了一個微服務。 此服務使用Spring雲配置服務器獲取屬性。 這個微服務接受頭中的版本,並根據版本,它執行適當的功能。 在我的github repo中,我有2個分支,每個版本1個。 該服務通常將以下信息發送到配置服務器以獲取屬性 -

應用程序名稱+配置文件+標簽

有沒有辦法在我的.yml文件中使用占位符代替標簽? 我希望標簽動態設置為v1,如果我在標題v2中看到v1。

編輯:

我在本文檔( http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html )的“Git URI中的占位符”一節中看到了對占位符的引用,但是我不確定值是如何的從傳入的請求動態替換

spring-cloud-config-server提供了幾個REST API,允許直接查詢屬性文件:

$ hostname:port/{label}/{name}-{profiles}.properties]

您可以動態使用您選擇的任何標簽,只要它與git上的現有標簽匹配即可。

例如,要檢索application.properties ,在git中標記為v1

 $ http://${hostname}:${port}/v1/application.properties

配置服務器REST API:

  • /{name}/{profiles}/{label:.*
  • /{label}/{name}-{profiles}.properties
  • /{name}-{profiles}.json
  • /{label}/{name}-{profiles}.json
  • /{label}/{name}-{profiles}.yml
  • /{label}/{name}-{profiles}.yaml
  • /{name}-{profiles}.yml
  • /{name}-{profiles}.yaml
  • / {名稱} / {型材:. [^ - ]。 }
  • /{name}-{profiles}.properties
  • / {名} / {輪廓} / {標簽} / **

我在git上嘗試了一個帶有屬性文件的示例spring-cloud-server項目。 我在文件中為每個標簽應用了不同值的git標簽v1v2 (我使用了配置文件remote ):

標簽v1

http://localhost:8888/v1/application-remote.properties
> testproperty: remotevalue-v1

標簽v2

http://localhost:8888/v2/application-remote.properties
> testproperty: remotevalue-v2

沒有標簽

http://localhost:8888/application-remote.properties
> testproperty: remotevalue-master

Java代碼

我沒有嘗試過,但我想你也可以使用cloud-config-server的java API(直接注入和調用控制器而不是執行http請求):

@Autowired
EnvironmentController environmentController;
...

Environment labelled = environmentController.labelled("application", "remote", "v1");
Map<?, ?> keyValues = labelled.getPropertySources().get(0).getSource();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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