1、Config分布式配置中心介绍
问题
:每一个微服务自己带着一个application.yml,上百个配置文件的管理怎么办呢?
解决
:SpringCloud Config
为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config分为服务端
和客户端
两部分
服务端也称为分布式配置中心
,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。客户端则是通过指定的配置中心来管理应用资源
,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
springcloud config具体作用
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露 - post/crul访问刷新即可…
2、Config配置总控中心搭建
服务端从码云上获取信息 服务端—码云
准备工作
1、在码云上新建仓库springcloud-config
2、克隆到本地磁盘任意目录下 git clone orign
3、自动生成.git
工作区
4、新建新文件到工作区
中
config-dev.yml
config:
info: "master branch,springcloud-config/config-dev.yml version=7"
config-prod.yml
config:
info: "master branch,springcloud-config/config-prod.yml version=1"
config-test.yml
config:
info: "master branch,springcloud-config/config-test.yml version=1"
4、提交新代码到暂存区
git add config-dev.yml
5、提供代码从暂存区到本地库
git commit -m "提交到本地库"
6、新版本git需要联通本地库和远程库 ,先拉取后推送
从远程库拉取:git pull https://gitee.com/pidaner/springcloud-config.git master --allow-unrelated-histories
推送到远程库:git push -u https://gitee.com/pidaner/springcloud-config.git master -f
7、到这步查看远程库已经成功,不成功再次git commit
举个栗子
1、新建Module模块cloud-config-center-3344
它即为Cloud的配置中心模块CloudConfig Center
2、pom
<?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>springcloud</artifactId>
<groupId>com.lian.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-center-3344</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--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.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--web test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
3、yaml
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://gitee.com/pidaner/springcloud-config.git #码云上面的git仓库名字
#搜索目录
search-paths:
- springcloud-config
#读取分支
label: master
#服务注册到eureka地址
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
4、主启动类
package com.lian.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer //开启分布式配置注解
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
windows下修改hosts文件,增加映射
#临时测试springcloud的分布式配置
127.0.0.1 config-3344.com
5、测试通过Config微服务是否可以从码云上获取配置内容
启动eureka
启动cloud-config-center-3344
浏览器防问 - http://config-3344.com:3344/master/config-dev.yml
http://config-3344.com:3344/config-dev.yml默认主分支
http://config-3344.com:3344/config/dev/master第一种的逆向操作
配置读取规则
1、/{label}/{application}-{profile}.yml(推荐)
master分支
- http://config-3344.com:3344/master/config-dev.yml
- http://config-3344.com:3344/master/config-test.yml
- http://config-3344.com:3344/master/config-prod.yml
dev分支
- http://config-3344.com:3344/dev/config-dev.yml
- http://config-3344.com:3344/dev/config-test.yml
- http://config-3344.com:3344/dev/config-prod.yml
2、/{application}-{profile}.yml 默认主分支
- http://config-3344.com:3344/config-dev.yml
- http://config-3344.com:3344/config-test.yml
- http://config-3344.com:3344/config-prod.yml
- http://config-3344.com:3344/config-xxxx.yml(不存在的配置)
3、/{application}/{profile}[/{label}]第1种的逆向操作(功能一样)
- http://config-3344.com:3344/config/dev/master
- http://config-3344.com:3344/config/test/master
- http://config-3344.com:3344/config/test/dev
重要配置细节总结
- /{name}-{profiles}.yml
- /{label}-{name}-{profiles}.yml
- label:分支(branch)
- name:服务名
- profiles:环境(dev/test/prod)
成功实现了用SpringCloud Config通过码云获取配置信息
3、Config客户端配置与测试
客户端从码云上获取信息 客户端—服务端—码云
1、新建cloud-config-client-3355
2、POM
<?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>springcloud</artifactId>
<groupId>com.lian.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3355</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--config客户端-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3、bootstrap.yml
applicaiton.yml是用户级
的资源配置项
bootstrap.yml是系统级
的,优先级更加高
Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。
初始化的时候,BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
Bootstrap属性有高优先级
,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载
的。bootstrap.yml优先级高于application.yml
。
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址k
#服务注册到eureka地址
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
4、主启动
package com.lian.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
5、controller
package com.lian.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
/**
* config-dev.yml文件内容
* config:
* info: "master branch,springcloud-config/config-dev.yml version=7"
*/
@Value("${config.info}")
private String configInfo;
@Value("${server.port}")
private String serverPort;
@GetMapping("/configInfo")
public String getConfigInfo(){
return "serverPort" + serverPort + "configInfo" + configInfo;
}
}
6、测试
启动Config配置中心3344微服务并自测
-
http://config-3344.com:3344/master/config-dev.yml
启动3355作为Client准备访问 -
http://localhost:3355/configlnfo
成功实现了客户端3355访问SpringCloud Config3344通过码云获取配置信息可题随时而来
分布式配置的动态刷新问题
Linux运维修改码云上的配置文件内容做调整,修改config-dev.yml配置并提交到码云中,比如加个变量age或者版本号version
刷新3344,发现ConfigServer配置中心立刻响应
刷新3355,发现ConfigClient客户端没有任何响应
3355没有变化除非自己重启或者重新加载
难到每次运维修改配置文件,客户端都需要重启??妈蛋
4、Config动态刷新之手动版
解决每次更新配置都要重启客户端微服务3355
1、修改cloud-config-client-3355
POM引入actuator监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、修改YML,添加暴露监控端口配置
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址k
#服务注册到eureka地址
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3、controller层添加@RefreshScope注解
package com.lian.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
/**
* config-dev.yml文件内容
* config:
* info: "master branch,springcloud-config/config-dev.yml version=7"
*/
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
4、测试
再次将 config.dev.yaml 的文件version改为9
提交暂存区,提交本地库,联通本地库和远程库
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: config-dev.yml
no changes added to commit (use "git add" and/or "git commit -a")
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$ git add config-dev.yml
warning: LF will be replaced by CRLF in config-dev.yml.
The file will have its original line endings in your working directory
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: config-dev.yml
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$ git commit -m "dev"
[master 8101a0f] dev
1 file changed, 1 insertion(+), 1 deletion(-)
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$ git status
On branch master
nothing to commit, working tree clean
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$ git pull https://gitee.com/pidaner/springcloud-config.git master --allow-unrelated-histories
From https://gitee.com/pidaner/springcloud-config
* branch master -> FETCH_HEAD
Already up to date.
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$ git push -u https://gitee.com/pidaner/springcloud-config.git master -f
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 273 bytes | 273.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/pidaner/springcloud-config.git
6f2574e..8101a0f master -> master
Branch 'master' set up to track remote branch 'master' from 'https://gitee.com/pidaner/springcloud-config.git'.
PF2D33Q1-WXG+Admin@PF2D33Q1-WXG MINGW64 /e/temp/practice/demo/practice/Demo_daily/springcloud-config/springcloud-config (master)
$
远程已修改成功
客户端依然不能更新出version,需要运维人员发送Post请求刷新3355
curl -X POST "http://localhost:3355/actuator/refresh"
再次从客户端查询码云上信息,即成功
想想还有什么问题?
- 假如有多个微服务客户端3355/3366/3377…10000个
- 每个微服务都要执行—次post请求,手动刷新?
- 可否广播,一次通知,处处生效?
- 我们想大范围的自动刷新,求方法