SpringCloud教程六:Config(配置中心)
一、概述
配置中心为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,它就是Spring Cloud Config.
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
二、原理
springCloudConfig分服务端和客户端,服务端负责将本地,git或者svn中存储的配置文件发布成REST风格的接口,客户端可以从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置,这需要每个客户端通过POST方法触发各自的/refresh接口。而我们上面说的SpringCloudBus就发挥了其作用了
SpringCloudBus通过一个轻量级消息代理连接分布式系统的节点(有点像消息队列那种)。这可以用于广播状态更改(如配置更改)或其他管理指令。SpringCloudBus提供了通过post方法访问的endpoint/bus/refresh(spring boot 有很多监控的endpoint,比如/health),这个接口通常由git的钩子功能(监听触发)调用,用以通知各个SpringCloudConfig的客户端去服务端更新配置。
三、实践
3.1 Config-Server
创建一个spring-boot项目,取名为config-server,其pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.wyma.config</groupId> 7 <artifactId>config-server</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>config-server</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.2.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.cloud</groupId> 30 <artifactId>spring-cloud-config-server</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-test</artifactId> 36 <scope>test</scope> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework.cloud</groupId> 41 <artifactId>spring-cloud-starter-eureka</artifactId> 42 </dependency> 43 </dependencies> 44 45 <dependencyManagement> 46 <dependencies> 47 <dependency> 48 <groupId>org.springframework.cloud</groupId> 49 <artifactId>spring-cloud-dependencies</artifactId> 50 <version>Camden.SR6</version> 51 <type>pom</type> 52 <scope>import</scope> 53 </dependency> 54 </dependencies> 55 </dependencyManagement> 56 57 58 <build> 59 <plugins> 60 <plugin> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-maven-plugin</artifactId> 63 </plugin> 64 </plugins> 65 </build> 66 67 <repositories> 68 <repository> 69 <id>spring-milestones</id> 70 <name>Spring Milestones</name> 71 <url>https://repo.spring.io/milestone</url> 72 <snapshots> 73 <enabled>false</enabled> 74 </snapshots> 75 </repository> 76 </repositories> 77 78 79 </project>
在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.cloud.config.server.EnableConfigServer; 4 5 @SpringBootApplication 6 @EnableConfigServer 7 public class ServerConfigApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(ServerConfigApplication.class, args); 11 } 12 13 }
application.properties文件配置如下:
1 spring.application.name=config-server 2 server.port=8888 3 4 5 spring.cloud.config.server.git.uri=https://github.com/xx/SpringCloudConfig/ 6 spring.cloud.config.server.git.searchPaths=config-repo 7 spring.cloud.config.label=master 8 spring.cloud.config.server.git.username= 9 spring.cloud.config.server.git.password=
- spring.cloud.config.server.git.uri:配置git仓库地址
- spring.cloud.config.server.git.searchPaths:配置仓库路径
- spring.cloud.config.label:配置仓库的分支
- spring.cloud.config.server.git.username:访问git仓库的用户名
- spring.cloud.config.server.git.password:访问git仓库的用户密码
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写。
远程仓库https://github.com/xx/SpringcloudConfig/ 中有个文件config-client-dev.properties文件中有一个属性:
foo = foo version 21
启动程序:访问http://localhost:8888/foo/dev
{"name":"foo","profiles":["dev"],"label":"master","version":"0fc8081c507d694b27967e9074127b373d196431","state":null,"propertySources":[]}
证明配置服务中心可以从远程程序获取配置信息。
http请求地址和资源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
3.2 Config-Client
重新创建一个springboot项目,取名为config-client,其pom文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.wyma.config</groupId> 7 <artifactId>client-config</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>client-config</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.2.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.cloud</groupId> 30 <artifactId>spring-cloud-starter-config</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-web</artifactId> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-test</artifactId> 41 <scope>test</scope> 42 </dependency> 43 </dependencies> 44 45 <dependencyManagement> 46 <dependencies> 47 <dependency> 48 <groupId>org.springframework.cloud</groupId> 49 <artifactId>spring-cloud-dependencies</artifactId> 50 <version>Dalston.RC1</version> 51 <type>pom</type> 52 <scope>import</scope> 53 </dependency> 54 </dependencies> 55 </dependencyManagement> 56 57 <build> 58 <plugins> 59 <plugin> 60 <groupId>org.springframework.boot</groupId> 61 <artifactId>spring-boot-maven-plugin</artifactId> 62 </plugin> 63 </plugins> 64 </build> 65 66 <repositories> 67 <repository> 68 <id>spring-milestones</id> 69 <name>Spring Milestones</name> 70 <url>https://repo.spring.io/milestone</url> 71 <snapshots> 72 <enabled>false</enabled> 73 </snapshots> 74 </repository> 75 </repositories> 76 77 78 </project>
配置文件为:
1 spring.application.name=config-client 2 spring.cloud.config.label=master 3 spring.cloud.config.profile=dev 4 spring.cloud.config.uri= http://localhost:8888/ 5 server.port=8881
- spring.cloud.config.label 指明远程仓库的分支
- spring.cloud.config.profile
- dev开发环境配置文件
- test测试环境
- pro正式环境
- spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。
程序的入口类,写一个API接口“/hello”,返回从配置中心读取的foo变量的值,代码如下:
1 import org.springframework.beans.factory.annotation.Value; 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 @SpringBootApplication 8 @RestController 9 public class ClientConfigApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(ClientConfigApplication.class, args); 13 } 14 15 16 @Value("${foo}") 17 String foo; 18 @RequestMapping(value = "/hello") 19 public String hello(){ 20 return foo; 21 } 22 }
打开网址访问:http://localhost:8881/hello,网页显示:
foo version 25
这就说明,config-client从config-server获取了foo的属性,而config-server是从git仓库读取的。
四、参考资料
五、源码
加入qq群:Spring全家桶技术交流①群196165973,免费获取源码。