对于基于spring的项目,例如有eureka config(eureka.properties)、zuul config(zuul.properties)、feign config(feign.properties)等。
还有多种环境,如开发、测试、暂存,如 application-dev.properties、application-prod.properties。
在项目中引入spring cloud config后,我们可以将所有的配置文件保存到git repo中,但是如何很好地组织这些配置文件呢?并最小化 Spring Cloud 客户端项目的配置?
最佳答案
看来我一开始就误解了spring cloud的配置,用documentation
The HTTP service has resources in the form:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
where the "application" is injected as the spring.config.name in the > SpringApplication (i.e. what is normally "application" in a regular Spring Boot > app), "profile" is an active profile (or comma-separated list of properties), and > "label" is an optional git label (defaults to "master".)
通过 Spring Cloud Config 内置机制,Spring Cloud Config 会将所有属性公开为 REST 资源,因此其中:
- application 是 Spring Boot 客户端项目
spring.application.name
- 个人资料是
spring.profiles.active
在 Spring Boot 客户端项目中 - label 是 git 分支名称,其中 git git repo 由 Spring Cloud 配置服务器指定,例如
spring.cloud.config.server.git.uri
然后客户端可以GET
所有违反上述规则的属性。
通常对于 Spring Boot 客户端项目,只需配置 Spring Cloud 配置服务器,如下所示:
spring:
application:
name: eureka
cloud:
config:
uri: http://localhost:8888
profiles:
active: dev, prod
所以客户端会GET
所有属性:eureka-dev.yml
和eureka-prod.yml
在 Spring Cloud 配置服务器中。
关于Spring Cloud配置最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030369/