在分布式系统中,因为服务数量巨多,为了方便服务配置文件统一管理,实时更新,因此须要分布式配置中心组件。在Spring Cloud
中,有分布式配置中心组件spring cloud config
,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config
组件中,分两个角色,一是config server
,二是config client
,业界也有些知名的同类开源产品,好比百度的disconf
。java
相比较同类产品,SpringCloudConfig
最大的优点是和Spring
无缝集成,支持Spring
里面Environment
和PropertySource
的接口,对于已有的Spring
应用程序的迁移成本很是低,在配置获取的接口上是彻底一致,结合SpringBoot
可以使你的项目有更加统一的标准(包括依赖版本和约束规范),避免了应为集成不一样开软件源形成的依赖版本冲突。git
SpringCloudConfig
就是咱们一般意义上的配置中心,把应用本来放在本地文件的配置抽取出来放在中心服务器,从而可以提供更好的管理、发布能力。SpringCloudConfig
分服务端和客户端,服务端负责将git svn
中存储的配置文件发布成REST
接口,客户端能够从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置,这须要每一个客户端经过POST
方法触发各自的/refresh
。github
SpringCloudBus
经过一个轻量级消息代理链接分布式系统的节点。这能够用于广播状态更改(如配置更改)或其余管理指令。SpringCloudBus
提供了经过POST
方法访问的endpoint/bus/refresh
,这个接口一般由git
的钩子功能调用,用以通知各个SpringCloudConfig
的客户端去服务端更新配置。web
注意:这是工做的流程图,实际的部署中SpringCloudBus
并非一个独立存在的服务,这里单列出来是为了能清晰的显示出工做流程。spring
下图是SpringCloudConfig
结合SpringCloudBus
实现分布式配置的工做流json
新建项目 spring-cloud-config-server
服务器
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
复制代码
在程序的启动类 ConfigServerApplication
经过 @EnableConfigServer
开启 SpringCloudConfig
服务端app
package io.ymq.example.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
复制代码
配置文件 application.properties
分布式
spring.application.name=config-server
server.port=8888
spring.cloud.config.label=master
spring.cloud.config.server.git.uri=https://github.com/souyunku/spring-cloud-config.git
spring.cloud.config.server.git.search-paths=spring-cloud-config
#spring.cloud.config.server.git.username=your username
#spring.cloud.config.server.git.password=your password
复制代码
Git仓库若是是私有仓库须要填写用户名密码,示例是公开仓库,因此不配置密码。svn
远程Git仓库
spring-cloud-config
文件夹下有 application-dev.properties
,application-test.properties
三个文件,内容依次是:content=hello dev
,content=hello test
,content=hello pre
启动程序 ConfigApplication
类
访问 Spring Cloud Config Server
服务:
http://localhost:8888/springCloudConfig/dev/master
{
"name": "springCloudConfig",
"profiles": [
"dev"
],
"label": "master",
"version": "b6fbc2f77d1ead41d5668450e2601a03195eaf16",
"state": null,
"propertySources": [
{
"name": "https://github.com/souyunku/spring-cloud-config.git/application-dev.properties",
"source": {
"content": "hello dev"
}
}
]
}
复制代码
证实配置服务中心能够从远程程序获取配置信息。
http请求地址和资源文件映射以下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
新建项目 spring-cloud-config-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码
在程序的启动类 ConfigClientApplication
经过 @Value
获取服务端的 content
值的内容
package io.ymq.example.config.client;
@RestController
@SpringBootApplication
public class ConfigClientApplication {
@Value("${content}")
String content;
@RequestMapping("/")
public String home() {
return "content:" + content;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
复制代码
配置文件 application.properties
spring.application.name=config-client
server.port=8088
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8888/
复制代码
启动程序 ConfigClientApplication
类
下一篇,继续Spring Cloud Config Server
整合 eureka
, 等更多特性
GitHub:github.com/souyunku/sp…