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