随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错。配置中心便是解决此类问题的灵丹妙药。
市面上开源的配置中心有很多,BAT 每家都出过,360 的 QConf、淘宝的 diamond、百度的 disconf 都是解决这类问题。国外也有很多开源的配置中心 Apache 的 Apache Commons Configuration、owner、cfg4j 等等。这些开源的软件以及解决方案都很优秀,但是我最钟爱的却是 Spring Cloud Config,因为它功能全面强大,可以无缝的和 spring 体系相结合,够方便够简单颜值高我喜欢。
在我们了解 spring cloud config 之前,我可以想想一个配置中心提供的核心功能应该有什么
Spring Cloud Config 可以完美的支持以上所有的需求。
Spring Cloud Config 项目是一个解决分布式系统的配置管理方案。它包含了 Client 和 Server 两个部分,server 提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client 通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud 使用 git 或 svn 存放配置文件,默认情况下使用 git,我们先以 git 为例做一套示例。
首先在 github 上面创建了一个文件夹 config-repo 用来存放配置文件,为了模拟生产环境,我们创建以下三个配置文件:
- // 开发环境neo-config-dev.properties// 测试环境neo-config-test.properties// 生产环境neo-config-pro.properties
每个配置文件中都写一个属性 neo.hello, 属性值分别是 hello im dev/test/pro 。下面我们开始配置 server 端
- <dependencies>
- <dependency>
- <groupId>
- org.springframework.cloud
- </groupId>
- <artifactId>
- spring-cloud-config-server
- </artifactId>
- </dependency>
- </dependencies>
只需要加入 spring-cloud-config-server 包引用既可。
- server:
- port:8040spring:
- application:
- name: spring-cloud-config-servercloud:
- config:server:
- git:
- uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git仓库的地址search-paths: config-repo# git仓库地址下的相对地址,可以配置多个,用,分割。username:# git仓库的账号password:# git仓库的密码
启动类添加
,激活对配置中心的支持
- @EnableConfigServer
- @EnableConfigServer
- @SpringBootApplication
- public class ConfigServerApplication{
- public static void main(String[] args) {
- SpringApplication.run(ConfigServerApplication.class, args);
- }
- }
到此 server 端相关配置已经完成
首先我们先要测试 server 端是否可以读取到 github 上面的配置信息,直接访问:http://localhost:8001/neo-config/dev
返回信息如下:
- {
- "name":"neo-config",
- "profiles":["dev"],
- "label":null,
- "version":null,
- "state":null,
- "propertySources":[
- {
- "name":"https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties",
- "source":{
- "neo.hello":"hello im dev"
- }}
- ]}
上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明 server 端已经成功获取了 git 仓库的配置信息。
如果直接查看配置文件中的配置信息可访问:http://localhost:8001/neo-config-dev.properties, 返回:
- neo.hello: hello im dev
修改配置文件 neo-config-dev.properties 中配置信息为:
, 再次在浏览器访问 http://localhost:8001/neo-config-dev.properties,返回:
- neo.hello=hello im dev update
。说明 server 端会自动读取最新提交的内容
- neo.hello: hello im dev update
仓库中的配置文件会被转换成 web 接口,访问可以参照以下的规则:
以 neo-config-dev.properties 为例子,它的 application 是 neo-config,profile 是 dev。client 会根据填写的参数来选择读取对应的配置。
主要展示如何在业务项目中去获取 server 端的配置信息
- <dependencies>
- <dependency>
- <groupId>
- org.springframework.cloud
- </groupId>
- <artifactId>
- spring-cloud-starter-config
- </artifactId>
- </dependency>
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter-web
- </artifactId>
- </dependency>
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter-test
- </artifactId>
- <scope>
- test
- </scope>
- </dependency>
- </dependencies>
引入 spring-boot-starter-web 包方便 web 测试
需要配置两个配置文件,application.properties 和 bootstrap.properties
application.properties 如下:
- spring.application.name=spring-cloud-config-client
- server.port=8002
bootstrap.properties 如下:
- spring.cloud.config.name=neo-config
- spring.cloud.config.profile=dev
- spring.cloud.config.uri=http://localhost:8001/
- spring.cloud.config.label=master
特别注意:上面这些与 spring-cloud 相关的属性必须配置在 bootstrap.properties 中,config 部分内容才能被正确加载。因为 config 的相关配置会先于 application.properties,而 bootstrap.properties 的加载也是先于 application.properties。
启动类添加
,激活对配置中心的支持
- @EnableConfigServer
- @SpringBootApplication
- public class ConfigClientApplication{
- public static void main(String[] args) {
- SpringApplication.run(ConfigClientApplication.class, args);
- }
- }
启动类只需要
注解就可以
- @SpringBootApplication
使用
注解来获取 server 端参数的值
- @Value
- @RestControllerclass HelloController {@Value("${neo.hello}")privateString hello;@RequestMapping("/hello")publicStringfrom() {return this.hello;
- }
- }
启动项目后访问:http://localhost:8002/hello,返回:
说明已经正确的从 server 端获取到了参数。到此一个完整的服务端提供配置服务,客户端获取配置参数的例子就完成了。
- `hello im dev update
我们在进行一些小实验,手动修改 neo-config-dev.properties 中配置信息为:
提交到 github, 再次在浏览器访问 http://localhost:8002/hello,返回:
- neo.hello=hello im dev update1
,说明获取的信息还是旧的参数,这是为什么呢?因为 spirngboot 项目只有在启动的时候才会获取配置文件的值,修改 github 信息后,client 端并没有在次去获取,所以导致这个问题。如何去解决这个问题呢?留到下一章我们在介绍。
- neo.hello: hello im dev update
示例代码
来源: http://blog.csdn.net/ityouknow/article/details/72636773