通过前文讲解,至此,微服务架构已经日趋完善——如今已经能够作一个大型的应用了!然而,随着项目的迭代,微服务数目每每与日俱增,如何高效地管理配置成为咱们必须解决的问题。本节来讨论如何使用Spring Cloud Config管理配置。git
Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。因为Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,所以,Spring Cloud Config很是适合Spring应用程序,固然也可与任何其余语言编写的应用程序配合使用。github
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可以使用Subversion、MySQL、本地文件系统或Vault存储配置,本博客以Git为例进行讲解),所以能够很方便地实现对配置的版本控制与内容审计。spring
Config Client是Config Server的客户端,用于操做存储在Config Server中的配置属性。引入Spring Cloud Config后的架构以下:bootstrap
TIPS服务器
Spring Cloud Config的GitHub:https://github.com/spring-cloud/spring-cloud-config架构
加依赖app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
加注解:@EnableConfigServer
分布式
写配置:微服务
server: port: 8080 spring: application: name: microservice-config-server cloud: config: server: git: # Git仓库地址 uri: https://git.oschina.net/itmuch/spring-cloud-config-repo.git # Git仓库帐号 username: # Git仓库密码 password:
Spring Cloud Config Server提供了RESTful API,可用来访问存放在Git仓库中的配置文件。测试
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
其中的{appliation}、{profile}、{label} 都是占位符。
TIPS
事实上,可以使用Spring Cloud Config实现配置的“继承”与“组合”,举个例子——
假设有一个应用:microservice-foo
,其profile是dev,那么其实Spring Cloud Config会查找以下几个文件:
microservice-foo-dev.yml
microservice-foo.yml
application-dev.yml
application.yml
对于相同属性的配置,从上至下优先级逐渐递减;最终得到的配置属性是四个文件的组合。由此,不难分析,可以下规划几个配置文件:
microservice-foo-dev.yml
做为指定应用在指定profile下的配置文件microservice-foo.yml
做为制定应用在任何profile下都通用的配置文件application-dev.yml
做为全部应用在指定profile下的配置文件application.yml
做为全部应用在任何profile下都通用的配置文件http://localhost:8080/microservice-foo-dev.yml
可访问到Git仓库的microservice-foo-dev.properties
并组合application.properties
。加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
加配置:applicaiton.yml
server: port: 8081
加配置:bootstrap.yml
spring: application: name: microservice-foo # 对应config server所获取的配置文件的{application} cloud: config: uri: http://localhost:8080/ profile: dev # profile对应config server所获取的配置文件中的{profile} label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
其中:
spring.application.name:对应Config Server所获取的配置文件中的{application} ;
spring.cloud.config.uri:指定Config Server的地址,默认是http://localhost:8888 ;
spring.cloud.config.profile:profile对应Config Server所获取的配置文件中的{profile} ;
spring.cloud.config.label:指定Git仓库的分支,对应Config Server所获取配置文件的{label}。
值得注意的是,以上属性应配置在bootstrap.yml,而不是application.yml中。若是配置在application.yml中,该部分配置就不能正常工做。例如,Config Client会链接spring.cloud.config.uri的默认值 http://localhost:8888
,而并不是咱们配置的 http://localhost:8080/
。
Spring Cloud有一个“引导上下文”的概念,这是主应用程序上下文(Application Context)的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.*
(yml或properties)中的属性不一样,引导上下文加载bootstrap.*
中的属性。配置在bootstrap.*
中的属性有更高的优先级,所以默认状况下它们不能被本地配置覆盖。
写代码
@RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String hello() { return this.profile; } }
访问http://localhost:8081/profile
可返回Git仓库中的配置属性。
Config Server
Config Client
http://www.itmuch.com/spring-cloud/finchley-19/