文章目录
1. 简介
Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments.
- Spring Cloud Config 支持服务端与客户端
- 是一个分布式配置中心
- 提供配置信息多环境切换
- 配置信息更新,实时同步
- 借助eureka实现高可用
2. 代码实现
2.1 涉及的模块及整体步骤
2.1.1 涉及的模块
- eureka-server-singleton:eureka服务发布注册中心
- config-server-ha:配置中心服务端,通过指定不同端口启动两个实例模拟服务端集群
- config-client-use-ha:新建的通过HA版服务配置中心访问远程配置信息模块,也可以使用原有模块
- config-repository:放置于GitHub的配置,
config-client-use-ha-test.properties
是对应的本次测试的保存配置信息的文件名称
2.1.2 整体步骤
- GitHub创建存放配置信息的config-repository目录与配置信息
config-client-use-ha-test.properties
,可通过demo中的config-repository
模块关联GitHub上的配置。 - 实现eureka-server-singleton:eureka服务发布注册中心,与前面没有任何区别
- 实现config-server-ha:关键是启动Spring Cloud Config Server功能,指定配置仓库的配置信息
- 实现config-client-use-ha:从配置仓库读取配置信息
- config-repository中添加
config-client-use-ha-test.properties
:放置4步骤的配置信息
2.2 源代码
2.2.1 Github地址
https://github.com/andyChenHuaYing/spring-cloud-demo
2.2.2 配置信息地址
配置信息在dev-20180827、与master分支都有,但是代码中使用的是dev-20180827分支的代码,如果想修改值验证,记得确认客户端bootstrap.properties中配置信息与想要访问的仓库地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository
2.3 eureka-server-singleton
与Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别
2.4 config-server-ha
2.4.1 整体实现
- pom.xml引入
spring-cloud-config-server
- application.yml指定配置仓库连接信息
- 启动类使用注解启动类使用注解
@EnableConfigServer
开启配置服务功能
2.4.2 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>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
2.4.3 application-{profile}.yml
两个配置文件除端口之外,其他配置项完一致,配置文件:application-8772.yml
、application-8773.yml
spring:
application:
name: config-server-ha
cloud:
config:
server:
git:
uri: https://github.com/andyChenHuaYing/spring-cloud-demo
searchPaths: config-repository
username:
password:
forcePull: true
label: dev-20180827
server:
port: 8772
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.4.4 ConfigServerHaApplication
package org.oscar.scd.config.server.ha;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerHaApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerHaApplication.class, args);
}
}
2.5 config-client-use-ha
2.5.1 整体实现
- pom.xml文件中引入依赖
spring-cloud-starter-config
、spring-cloud-starter-netflix-eureka-client
- bootstrap.yml中指定Config Server连接信息以及需要访问配置中心的具体配置信息
- ConfigClientUseHaApplication常规Spring Boot启动类
2.5.2 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>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client-use-ha</artifactId>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2.5.3 bootstrap.yml
下方信息与配置中心的配置文件的对应关系见后续验证部分
spring:
application:
name: config-client-use-ha
cloud:
config:
label: dev-20180827
profile: test
discovery:
enabled: true
serviceId: config-server-ha
server:
port: 8774
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.6 config-repository
保存配置文件
2.6.1 config-client-use-ha配置文件
config-client-use-ha-test.properties
foo=config-client-use-ha foo value.
3. 验证
3.1 创建SpringBoot启动类
简单创建Spring Boot启动类即可
3.1.1 EurekaServerSingletonApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
3.1.2 ConfigServerHaApplication-8772
3.1.3 ConfigServerHaApplication-8773
参考上一节,修改Active profiles:8773
3.1.4 ConfigClientUseHaApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
3.2 启动
EurekaServerSingletonApplication
ConfigServerHaApplication-8772
ConfigServerHaApplication-8773
ConfigClientUseHaApplication
3.3查看eureka服务信息界面
3.3 读取远程配置信息
3.3.1 ConfigClientUseHaApplication
- 启动之后可以根据指定规则访问远程配置完整信息,
/{name}/{profile}/{label}
- name:
spring.application.name
- profile:spring boot 的profile,即 application-{profile}.yml中的profile值,如application-dev.yml文件的profile是dev。
- label:application.yml配置文件中
spring.cloud.config.label
的值,这里是dev-20180827
- name:
- 查看config-client-use-ha的bootstrap.yml信息可知,其对应的远程配置文件为
https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-client-use-ha-test.properties.properties
访问地址:http://localhost:8773/config-client-use-ha/test/dev-20180827 可查看config-client-use-ha完整配置信息
3.3.2 ConfigClientApplication
- 浏览器中输入地址:http://localhost:8770/readFooProp
- 预期:读取的是:https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-client-dev.properties 文件中
foo=config-client-dev value
- 实际返回结果:
3.3.3 ConfigAnotherClientApplication
- 浏览器中输入地址:http://localhost:8774/readFooProp
- 实际返回结果:
###3.3.4 Config Server HA验证
- 重复启动ConfigClientUseHaApplication:观察启动日志中读取远程配置信息的server地址在
http://localhost:8772/
、http://localhost:8773/
之间切换 - 停止ConfigServerHaApplication-8772,再重复启动ConfigClientUseHaApplication:观察启动日志中读取远程配置信息的server地址只会是
http://localhost:8772/
- 停止ConfigServerHaApplication-8773,再重复启动ConfigClientUseHaApplication:启动失败
4. 思考
- HA也涉及到负载均衡,有哪些负载均衡算法可用,如何指定
- Spring Cloud Config有哪些常用的配置项,适用场景
5. 补充
5.1 资料
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html