1、为何要统一管理管理微服务配置html
在微服务框架中,微服务的配置管理通常有一下需求:java
一、集中管理配置git
一个使用微服务架构的应用系统可能会包含成百上千个微服务,集中管理配置很是用必要。spring
二、不一样环境不一样配置bootstrap
如:数据源配置在不一样的环境(开发、测试、预发布、生产等)中是不一样的。缓存
三、运行期可动态调整服务器
如:可根据各个微服务的负载状况,动态调整数据源链接池大小,而且在调整配置时不中止微服务。架构
四、配置修改后可自动更新app
如:配置内容发送变化,微服务可以自动更新配置。框架
对于微服务架构而言,一个通用的配置管理机制是必不可少的,常见作法是使用配置服务器管理配置。
2、Spring Cloud Config简介
Spring Cloud Config为分布式系统外部化配置提供了服务端和客户端的支持,它包括Config Server和Config Client两部分。因为Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,所以,Spring Cloud Config很是适合Spring应用。
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置的内容(也能够属于SVN),所以能够很方便的实现对配置的版本控制与内容审计。
Config Client是Config Server的客户端,用于操做存储在Config Server中的配置属性。全部的微服务在启动时,会请求Config Server以获取所须要的配置属性,而后缓存这些属性以提升性能。
3、编写Config Server与Config Client
3.一、Config Server
a、在gti上新建一个项目,如spring-cloud-config,而后新建几个配置文件,如:
里面的内容这里写的:
profile: 环境-1.0
如spring-cloud-demo-test.yml中是profile:test-1.0
为了测试版本控制,这里拉一个分支,如:v2.0
里面的内容这里写的:
profile: 环境-2.0
b、新建一个工程,如config-server,添加以下依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
c、编写启动类,添加@EnableConfigServer注解
package com.liuy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * config服务启动类 * @description config服务启动类 * @author luis * @version 1.0 * @date:2017年8月29日下午5:09:14 */ @SpringBootApplication @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
d、编写application.yml配置文件
server: port: 5020 spring: application: name: config-server cloud: config: server: git: uri: https://git.oschina.net/wadjz/spring-cloud-config.git username: 用户名 password: 密码
这样,一个config Server就行了,咱们可使用Config Server的端点获取配置文件的内容。端点与配置文件的映射规则以下:
/{application}/{profile}[/{lable}] /{application}-{profile}.yml /{lable}/{application}-{profile}.yml /{application}-{profile}.properties /{lable}/{application}-{profile}.properties
以上端点均可以映射到{application}-{profile}.yml这个配置文件,{application}表示微服务的名称,{profile}表示环境,{lable}表示对应git仓库的分支,默认master。
按照以上规则,使用以下URL能访问到GIT仓库里的master的spring-cloud-demo-test.yml
http://localhost:5020/spring-cloud-demo-test.yml
http://localhost:5020/v2.0/spring-cloud-demo-test.yml
下面URL可获取应用名称、项目profile等配置文件的详情:
http://localhost:5020/spring-cloud-demo/test
http://localhost:5020/spring-cloud-demo/test/v2.0
3.二、Config Client
a、建立一个项目,如config-client,添加如下依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
b、编写启动类
package com.liuy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * config客户端启动类 * @description config客户端启动类 * @author luis * @version 1.0 * @date:2017年8月29日下午5:09:14 */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
c、编写application.yml配置文件
server: port: 5021
d、编写bootstrap.yml配置文件
spring: application: # 对应config Server所获取的配置文件的{application} name: spring-cloud-demo cloud: config: # config Server的地址 uri: http://localhost:5020/ # profile对应config Server所获取的配置文件的{profile} profile: dev # 指定git的分支,对应config Server所获取的配置文件的{label} label: master
注意:这个必须配置在bootstrap.yml配置文件中。
e、编写Controller
@RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String hello() { return this.profile; } }
经过注解@Value("${profile}"),绑定GIT仓库配置文件中的profile属性。