Spring Cloud配置最佳实践?

标签 spring spring-boot spring-cloud spring-cloud-config

对于基于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.ymleureka-prod.yml在 Spring Cloud 配置服务器中。

关于Spring Cloud配置最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030369/

相关文章:

java - 使用 Spring Tool Suite 部署 Spring Web 应用程序

java - DELETE 和 PUT 方法总是调用 POST

java - 找不到 PropertySource : I/O error on GET request

spring-boot - Spring Cloud 配置 : client doesn't attempt to connect to the config server

java - 根据查询参数动态更改 Zuul 中路由的 url

java - 在过滤器中以编程方式登录 Spring Security 中的用户

java - Spring cloud stream kafka binder 是否有一个很好的例子来使用通用的 json 消息

java - 这种 Spring 单例 bean 线程的设计是否安全?

java - SpringBoot : Where to put internal container classes

amazon-web-services - AWS-SQS : How to set maxConcurrentConsumers?