spring-cloud-config-server 提供了几个可用的 REST API,允许直接查询属性文件:
$ hostname:port/{label}/{name}-{profiles}.properties]
您可以动态使用您选择的任何标签,只要它与 git 上的现有标签匹配。
例如,要在 git 中检索 application.properties,标记为 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:.[^-].}
- /{name}-{profiles}.properties
- /{name}/{profile}/{label}/**
我尝试了一个示例 spring-cloud-server 项目,其中包含 git 上的属性文件。我在文件中为每个标签应用了具有不同值的 git 标签v1 和v2(我使用了配置文件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();