分布式配置中心
为何要有用分布式配置中心这玩意儿?如今这微服务大军已经覆盖了各类大小型企业,每一个服务的粒度相对较小,所以系统中会出现大量的服务,每一个服务都要有本身都一些配置信息,或者相同的配置信息,可能不一样环境每一个服务也有单独的一套配置,这种状况配置文件数量比较庞大,维护起来至关费劲,举个栗子:
在开发的过程当中,通常数据库是开发环境数据库,全部服务DB的IP配置为:92.168.0.1,忽然老大说,开发环境换了,DB的IP要修改,这下可很差受了,全部模块挨个修改DB的配置,就问你难受不难受?
这个时候分布式配置中心就发挥了很大的优点,只须要修改配置中心配置,全部服务便可自动生效,爽不爽!java
<!-- more -->git
Spring Cloud Config
官网地址:http://cloud.spring.io/spring-cloud-config/spring
简介
Spring Cloud Config
为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的全部环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment
和PropertySource
抽象的映射,因此它除了适用于Spring构建的应用程序,也能够在任何其余语言运行的应用程序中使用。做为一个应用能够经过部署管道来进行测试或者投入生产,咱们能够分别为这些环境建立配置,而且在须要迁移环境的时候获取对应环境的配置来运行。数据库
置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,而且能够经过git客户端工具来方便的管理和访问配置内容。固然他也提供本地化文件系统的存储方式。json
使用 spring Cloud 进行集中式配置管理,将以往的配置文件从项目中摘除后放到Git 或svn中集中管理,并在须要变动的时候,能够通知到各应用程序,应用程序刷新配置不须要重启。bootstrap
实现原理
其实这个实现原理相对比较简单一些,基于git的交互操做。springboot
- 咱们把配置文件存放到git上面
- Spring Cloud Config配置中心服务链接git
- 客户端须要配置配置信息从配置中心服务获取
- 当客户端启动,会从配置中心获取git上面的配置信息
配置中心服务端
pom.xml添加依赖服务器
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Application启动类添加注解app
添加@EnableConfigServer
注解,启用配置中心:分布式
package com.qianxunclub; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * @author chihiro.zhang */ @SpringBootApplication @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
配置文件
在application.yml
或者application.properties
添加配置信息:
spring: cloud: config: server: git: uri: https://gitee.com/qianxunclub/spring-boot-config-repo default-label: master search-paths: /** basedir: target/config
- spring.cloud.config.server.git.uri:配置git仓库地址
- spring.cloud.config.server.git.search-paths:仓库文件夹目录,若是是
/**
,就是全部目录全部文件 - spring.cloud.config.server.git.default-label:配置仓库的分支
- spring.cloud.config.server.git.basedir:配置文件拉去到本地的目录位置
启动测试
首先在git里面添加一个application-dev.yml
配置文件,内容如此下:
test: 我是配置中心配置信息
已经配置完成了,启动一波试试,看效果咋样,正常状况下是能够正常启动的,而后获取配置文件试试
访问地址:http://localhost:8888/test/dev
若是返回以下,就是成功了:
{ "name":"test", "profiles":[ "dev" ], "label":null, "version":"64e7882a8f280641724e454a2db5a3da7b44d3d4", "state":null, "propertySources":[ { "name":"https://gitee.com/qianxunclub/spring-boot-config-repo/application-dev.yml", "source":{ "test":"配置中心的配置信息" } } ] }
http请求地址和资源文件映射以下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
配置中心客户端使用
pom.xml添加依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
配置文件
建立bootstrap.yml
文件,切记,是bootstrap.yml
文件bootstrap.yml
文件,我就由于写到了application.yml
这个里面,各类出现问题啊,添加以下配置:
spring: cloud: config: name: application profile: dev label: master uri: http://localhost:8888/
- spring.cloud.config.label:指明远程仓库的分支
- spring.cloud.config.profile:指定不一样环境配置文件,和git仓库的
application-dev.yml
对应 - spring.cloud.config.name:配置名称,通常和git仓库的
application-dev.yml
对应 - spring.cloud.config.uri:上面的配置中心服务地址
启动测试
先添加一个获取配置信息的类:
/** * @author chihiro.zhang */ @Configuration @EnableAutoConfiguration public class DemoConfiguration { @Value("${test}") private String test; }
找个地方随便调用一下,输出这个test,就会打印上面git里面配置的信息了,爽不!
说说中间遇到的坑
- 服务端git配置死活获取不了git仓库配置文件
spring: cloud: config: server: git: uri: https://gitee.com/qianxunclub/spring-boot-config-repo default-label: master search-paths: /** basedir: target/config
当时这个uri
配置的是公司的git仓库,公司的git仓库访问是须要开代理才能有权限访问的,代理也开了,但是一直报错:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Jun 06 11:10:56 CST 2018 There was an unexpected error (type=Not Found, status=404). Cannot clone or checkout repository: http://xxx.com:5080/framework/config-repo
很郁闷,不知道为啥,但是就在刚刚,就刚刚,写博客的时候,有测试了一下,通了。。。。日了狗了,不知道啥缘由,等研究出来了再来补充。
- 客户端配置必定要配置在
bootstrap.yml
里面uri
默认会调用端口为8888
的地址http://localhost:8888/
启动的时候,会加载label
和uri
,profile
配置,profile
能够在启动参数添加,profile
也能够加在application.yml
添加
name
也能够加在application.yml
添加
demo
配置中心服务端:https://gitee.com/qianxunclub/qianxunclub-springboot-config
配置git仓库:https://gitee.com/qianxunclub/qianxunclub-springboot-config
配置客户端使用:https://gitee.com/qianxunclub/qianxunclub-starter-demo
客户端主要配置在:https://gitee.com/qianxunclub/qianxunclub-starter-parent/tree/master/qianxunclub-starter-config