在本文中,咱们将学习如何构建一个基于Git存储的分布式配置中心,并对客户端进行改造,并让其可以从配置中心获取配置信息并绑定到代码中的整个过程。git
准备一个git仓库,能够在码云或Github上建立均可以。web
假设咱们读取配置中心的应用名为config-client
,那么咱们能够在git仓库中该项目的默认配置文件config-client.yml
:spring
info:bootstrap
profile: default浏览器
config-client-dev.yml
:info:架构
profile: devapp
经过Spring Cloud Config来构建一个分布式配置中心很是简单,只须要三步:框架
config-server-git
,并在pom.xml
中引入下面的依赖(省略了parent和dependencyManagement部分):<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
建立Spring Boot的程序主类,并添加@EnableConfigServer
注解,开启Spring Cloud Config的服务端功能。分布式
@EnableConfigServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
application.yml
中添加配置服务的基本信息以及Git仓库的相关信息,例如:spring application: name: config-server cloud: config: server: git: uri: http://git.oschina.net/didispace/config-repo-demo/ server: port: 1201
到这里,使用一个经过Spring Cloud Config实现,并使用Git管理配置内容的分布式配置中心就已经完成了。咱们能够将该应用先启动起来,确保没有错误产生,而后再尝试下面的内容。spring-boot
若是咱们的Git仓库须要权限访问,那么能够经过配置下面的两个属性来实现;
spring.cloud.config.server.git.username:访问Git仓库的用户名
spring.cloud.config.server.git.password:访问Git仓库的用户密码
完成了这些准备工做以后,咱们就能够经过浏览器、POSTMAN或CURL等工具直接来访问到咱们的配置内容了。访问配置信息的URL与配置文件的映射关系以下:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties
对应的配置文件,其中{label}
对应Git上不一样的分支,默认为master。咱们能够尝试构造不一样的url来访问不一样的配置内容,好比,要访问master分支,config-client应用的dev环境,就能够访问这个url:http://localhost:1201/config-client/dev/master
,并得到以下返回:
{ "name": "config-client", "profiles": [ "dev" ], "label": "master", "version": null, "state": null, "propertySources": [ { "name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml", "source": { "info.profile": "dev" } }, { "name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml", "source": { "info.profile": "default" } } ] }
咱们能够看到该Json中返回了应用名:config-client,环境名:dev,分支名:master,以及default环境和dev环境的配置内容。
在完成了上述验证以后,肯定配置服务中心已经正常运做,下面咱们尝试如何在微服务应用中获取上述的配置信息。
config-client
,并在pom.xml
中引入下述依赖:<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
@SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
bootstrap.yml
配置,来指定获取配置文件的config-server-git
位置,例如:spring: application: name: config-client cloud: config: uri: http://localhost:1201/ profile: default label: master server: port: 2001
上述配置参数与Git中存储的配置文件中各个部分的对应关系以下:
{application}
部分{profile}
部分{label}
部分config-server
的地址这里须要格外注意:上面这些属性必须配置在bootstrap.properties中,这样config-server中的配置信息才能被正确加载。
在完成了上面你的代码编写以后,读者能够将config-server-git、config-client都启动起来
{
"profile": "default"
}
另外,咱们也能够修改config-client的profile为dev来观察加载配置的变化。 从如今开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,但愿能够帮助更多的好学者。你们来一块儿探讨spring cloud架构的搭建过程及如何运用于企业项目。