分布式架构的配置问题
&emsp: 在分布式微服务架构中,由于服务数量很多 ,使得有很多配置文件,在更新配置文件时很麻烦。我们每个微服务自已带着一个 application.yml,上百个配置文件的管理起来就很麻烦,所以一套集中式的、动态的配置管理功能是必不可少的,在Spring Cloud中,有分布式配置中心组件Spring Cloud Config来解决这个问题。
什么是spring-cloud-config
Spring Cloud Config 为微服务架构中的微服务提供 集中式的外部配置支持, 配置服务器为各个不同微服务
应用的所有环境提供了一个中心化的外部配置.
- spring-cloud-config分为两部分
- 服务端 configer-server
分布式配置中心,它是一个独立的应用,主要用来连接配置服务器提供获取到的配置信息,加密 解密等接口
spring-cloud-config中推荐使用git来存储配置信息。 - 客户端 config-client
通过指定的服务端来拉取信息,以及业务相关配置,并在启动的时候获取加载配置信息
- 服务端 configer-server
案例
1.创建配置文件并提交到github,这里分享主要用到的三个命令(在git bash窗口中 Tab键可自动补全文件名)
添加本地文件到暂存区。
git add studyCloudConfApplication.yml
提交到本地仓库
git commit -m "init " studyCloudConfApplication.yml
推送到远程仓库master分支
git push origin master
studyCloudConfApplication.yml
spring:
profiles:
active: dev #激活开发环境
---
server:
port: 4001
spring:
profiles: dev
application:
name: springCloudConf
---
sever:
port: 4002
spring:
profiles: prod
application:
name: springCloudProd
2.新建confServer项目
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>springCloudParent</artifactId>
<groupId>com.study</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>confSever</artifactId>
<dependencies>
<!-- Spring Cloud Config配置中心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.创建application.yml
server:
port: 5001
spring:
application:
name: configServer
cloud:
config:
server:
git: # 远程库的git地址
uri: https://github.com/dushao1/studyCloudConf.git
4.创建启动类并添加@EnableConfigSever
package com.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author 王振读
* @date 2019/10/24 13:38
*/
@EnableConfigServer
@SpringBootApplication
public class ConfSever {
public static void main(String[] args) {
SpringApplication.run(ConfSever.class, args);
}
}
测试
(1)先测试 confSever能否正常获取到配置文件,访问规则如下

当我们访问不存在的配置时
spring cloud config 客户端服务
1.新建confClient项目,并添加依赖jar
<?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>springCloudParent</artifactId>
<groupId>com.study</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>confClient</artifactId>
<dependencies>
<!-- Spring Cloud Config 客户端依赖-->
<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>
</dependencies>
</project>
2.配置bootstrap.xml
application.xml是用户级别的配置
bootstrp.xml是系统级别的配置,优先级更高
spring cloud会创建一个Bootstrap context ,它会负责从外部获取配置并加载,因为高优先级,默认情况下不会被覆盖
bootstrap.yml
spring:
cloud:
config:
name: studyCloudConfApplication #github上的配置名称,注意没有yml后缀名
profile: dev #本次访问的环境配置项
label: master #远程库的分支名
uri: http://localhost:5001 #Config配置中心地址,通过它获取m配置信息
3.创建application.yml
server:
port: 8080
spring:
application:
name: confClient
4.创建接口,并获取Github上的配置信息
package com.study;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 王振读
* @date 2019/10/24 14:12
*/
@RestController
public class ConfClient {
@Value("${server.port}")
private String port;
@Value("${spring.application.name}")
private String applicationName;
@RequestMapping("getConfig")
public String config(){
return port+applicationName;
}
}
5.创建启动类 ConfClientStarter
package com.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* @author 王振读
* @date 2019/10/24 14:18
*/
@SpringBootApplication
public class ConfClientStarter {
public static void main(String[] args) {
SpringApplication.run(ConfClientStarter.class);
}
}
测试
启动confSever,以及confClient并调用接口获取数据

源码地址:https://pan.baidu.com/s/1vfBV6zsH2kwVNGTOrrO7BA