Spring Cloud Config 配置中心
(一)前言
Spring Cloud Config 是SpringCloud团队创建的一个全新项目,为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,
分为服务端和客户端两部分,外加配置文件的存储仓库;
- 配置仓库 : 用于存储各个服务的配置文件等,可将不同的服务配置分文件夹保存,用于不同服务获取;默认使用Git作为仓库;
- 服务端 : 分布式配置中心,独立的微服务应用,用来连接配置仓库,为客户端提供获取配置信息、加解密等接口;
- 客户端 : 微服务架构中的各个服务实例,通过指定配置中心,在项目启动的时候从配置中心获取配置信息.
(二) 配置中心的使用
- 首先创建配置仓库 1. 在git中新建一个项目Cloud-Config-Repository 2. 创建config-repo的文件夹作为存储配置文件的根目录地址 3. 针对我们的applicationClient服务,我们新建一个applicationClient文件夹,用于保存该服务的配置信息 4. 添加配置文件application.properties,内容为之前 applicationClient 服务中配置信息:
eureka.client.service-url.defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
eureka.client.service-url.register-with-eureka: true
eureka.client.service-url.fetch-registry: true
此时配置仓库我们创建好了;
-
创建配置中心服务端,我们依旧在CloudServer的项目中创建module,ConfigServer;
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>CloudServer</artifactId> <groupId>com.river</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Config-Server</artifactId> <dependencies> <!--表示为web工程--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--暴露各种指标 貌似是必须的 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
-
application.yaml
spring: application: name: config-server cloud: config: server: git: uri: http://*************.com/river/Cloud-Config-Repository.git #配置仓库的git地址, search-paths: config-repo/applicationClient #配置中心服务端浏览的目录 username: # 登陆git的用户名 password: # 登陆git的密码 server: port: 7001
-
启动类
@EnableConfigServer //开启配置中心支持 @SpringBootApplication public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }
-
启动项目 GET 方式请求 localhost:7001/application/dev/master,得到相应信息:
{ "name": "application", "profiles": [ "dev" ], "label": "master", "version": null, "state": null, "propertySources": [ { "name": "http://********.com/river/Cloud-Config-Repository.git/Cloud-Config/applicationClient/application-dev.properties", "source": { "eureka.client.service-url.fetch-registry": "true", "eureka.client.service-url.register-with-eureka": "true", "eureka.client.service-url.defaultZone": "http://peer1:8761/eureka/,http://peer2:8762/eureka/" } } ] }
说明配置中心服务端可以使用了;
`
- -
-
创建配置中心客户端,即微服务的各个服务实例,我们改造之前的ApplicationClient的两个服务实例
``` 1. 添加bootstrap.properties配置文件: ``` spring.application.name=applicationClient spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.uri=http://localhost:7001 spring.cloud.config.fail-fast=true #若配置中心服务不可用则立即启动失败 ``` 2. 修改application.yaml配置文件,只保留端口号的配置信息,如: ``` server: port: 9001 ``` 3. 启动applicationClient客户端,查看启动日志: ``` 2018-08-02 11:10:46.795 INFO 4596 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:7001 2018-08-02 11:10:47.237 INFO 4596 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-repos, profiles=[dev], label=master, version=null, state=null 2018-08-02 11:10:47.238 INFO 4596 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='http://hepengfei@git.scm.ppmoney.com/river/Cloud-Config-Server.git/Cloud-Config/config-repos/application-dev.properties']]] 2018-08-02 11:10:47.240 INFO 4596 --- [ main] c.r.a.two.ApplicationClientTwo : No active profile set, falling back to default profiles: default ... ... 2018-08-01 14:50:31.130 INFO 10820 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9002 (http) 2018-08-01 14:50:31.131 INFO 10820 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9002 2018-08-01 14:50:31.135 INFO 10820 --- [ main] c.r.a.two.ApplicationClientTwo : Started ApplicationClientTwo in 6.173 seconds (JVM running for 6.843) 2018-08-01 14:50:31.615 INFO 10820 --- [n(10)-127.0.0.1] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:7001 2018-08-01 14:50:31.937 INFO 10820 --- [n(10)-127.0.0.1] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=applicationClient, profiles=[dev], label=master, version=null, state=null ``` 可以看到客户端从配置中心获取到了配置,且打开Eureka注册中心的服务列表可以看到applicationClient 服务注册成功; ``` ` ``
``
(三)配置中心支持多个不同服务的客户端调用
我们查看配置中心服务端的配置可以知道,当前配置访问的是config-repo/applicationClient的目录下的配置,若想支持多个不同的客户端访问显然当前 不支持的;只要稍稍改动就可以了;
-
更改配置中心服务端的配置:
将search-paths配置的浏览目录改为 config-repo/{application}
该application在被客户端访问的时候会被客户端的应用名所动态替换; -
在配置仓库的config-repo下新建客户端spring.application.name应用名对应的文件夹目录,并将配置文件放在该文件夹下;
注意一点: 使用{application}这种动态支持的配置中心时,客户端的应用名应避免 'application' 这种名字,你可以试一下会获取不到配置中心的配置信息; 此问题我在做demo的时候出现,断断续续的排查了一天时间,所以还请注意此问题;
-
将客户端的spring.application.name应用名改为对应配置文件所在文件夹的名字;
(四)配置中心的高可用
配置中心部署到生产环境,与服务注册中心一样需要是高可用的服务;
- 传统模式 : 我们通过将多个配置中心服务端的Git仓库指向同一个配置仓库,然后在服务端上层做url的负载均衡;
- 服务模式 : 将配置中心服务端纳入Eureka管理,
-