Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.
Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。 使用Config Server,您可以集中管理所有环境中应用程序的外部属性。 客户端和服务器上的概念与Spring Environment和PropertySource抽象完全相同,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。 当应用程序从开发人员迁移到测试人员并进入生产过程时,您可以管理这些环境之间的配置,并确保应用程序具备迁移时所需的一切。 服务器存储后端的默认实现使用git,因此它轻松支持配置环境的标记版本,并且可用于管理内容的各种工具。 添加替代实现并将其插入Spring配置很容易。
Spring Cloud Config Server features:
HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content)Encrypt and decrypt property values (symmetric or asymmetric)Embeddable easily in a Spring Boot application using @EnableConfigServerConfig Client features (for Spring applications):
Bind to the Config Server and initialize Spring Environment with remote property sourcesEncrypt and decrypt property values (symmetric or asymmetric)Spring Cloud Config Server功能:
HTTP,用于外部配置的基于资源的API(名称-值对,或等效的YAML内容)
加密和解密属性值(对称或非对称)
使用@EnableConfigServer可轻松嵌入到Spring Boot应用程序中
Config Client功能(用于Spring应用程序):
绑定到Config Server并使用远程属性源初始化Spring Environment
加密和解密属性值(对称或非对称)
Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。**初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。**这两个上下文共享一个从外部获取的Environment。Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。
management.endpoints.web.exposure.include:暴露监控端口,供config动态刷新 当Config Client需要刷新时,需要以POST发送刷新请求。如: curl -X POST "http://localhost:3355/actuator/refresh"
server: port: 3355 spring: application: name: cloud-config-client cloud: consul: host: localhost port: 8500 discovery: prefer-ip-address: true service-name: ${spring.application.name} config: label: master name: application profile: dev uri: http://localhost:3344 management: endpoints: web: exposure: include: "*"是spring cloud提供的一种特殊的scope实现,用来实现配置、实例热加载。
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping(value = "/configInfo") public String getConfigInfo() { return configInfo; } }