• HOME
  • CATEGORY
  • ABOUT ME
All Articles

  • HOME
  • CATEGORY
  • ABOUT ME

SpringCloud-Config-1

2021-05-08

构建微服务架构(Config 篇)

该篇文档,前置代码下载:下载
该篇文档,全部完成后的代码下载:下载

原文链接:https://blog.csdn.net/u011863024/article/details/114298270
SpringCloud: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

Config分布式配置中心介绍

分布式系统面临的配置问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

SpringCloud 提供了 ConfigServer 来解决这个问题,我们每一个微服务自己带着一个 application.yml,上百个配置文件的管理.……

是什么

SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

怎么玩

SpringCloud Config 分为服务端和客户端两部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

能干嘛

集中管理配置文件

不同环境不同配置,动态化的配置更新,分环境部署比如 dev/test/prod/beta/release

运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息

当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置

将配置信息以 REST 接口的形式暴露 - post/crul 访问刷新即可…

与 GitHub 整合配置

由于 SpringCloud Config 默认使用 Git 来存储配置文件(也有其它方式,比如支持 SVN 和本地文件),但最推荐的还是 Git,而且使用的是 http/https 访问的形式。

Config 配置总控中心搭建

1. 用你自己的账号在 GitHub 上新建一个名为 springcloud-config 的新 Repository。

由上一步获得刚新建的git地址 - https://github.com/Sevattal/springcloud-config.git

本地硬盘目录上新建git仓库并clone。

工作目录为 D:\SpringCloud2021

1
git clone https://github.com/Sevattal/springcloud-config.git

此时在工作目录会创建名为 springcloud-config 的文件夹。

在springcloud-config的文件夹种创建三个配置文件(为本次教学使用的),随后 git add .,git commit -m “sth” 等一系列上传操作上传到 springcloud-config 的新 Repository。

config-dev.yml

1
2
config:
info: "master branch,springcloud-config/config-dev.yml version=7"

config-prod.yml

1
2
config:
info: "master branch,springcloud-config/config-prod.yml version=1"

config-test.yml

1
2
config:
info: "master branch,springcloud-config/config-test.yml version=1"

2. 新建Module模块cloud-config-center-3344,它即为Cloud的配置中心模块CloudConfig Center

3. cloud-config-center-3344 项目 pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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>springcloud2021</artifactId>
<groupId>com.sevattal.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-center-3344</artifactId>
<dependencies>
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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>

4. cloud-config-center-3344 项目 application.yml 文件 (若是私有仓库需要配置 密钥或者是账号和密码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://github.com/Sevattal/springcloud-config.git # GitHub上面的git仓库名字
####搜索目录
search-paths:
- springcloud-config
####读取分支
label: master
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka

注意:目前 GitHub 上的默认分支为 main,请按照实际的分支进行配置

5. cloud-config-center-3344 项目 主启动类 文件

1
2
3
4
5
6
7
8
9
10
11
12
package com.sevattal.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344
{
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}

6. windows下修改hosts文件,增加映射

1
127.0.0.1 config-3344.com

7. 测试通过 Config 微服务是否可以从 GitHub 上获取配置内容

启动 ConfigCenterMain3344

浏览器防问 - http://config-3344.com:3344/master/config-dev.yml

页面返回结果:

1
2
config:
info: "master branch,springcloud-config/config-dev.yml version=7"

8. 配置读取规则

官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/#_quick_start

/{label}/{application}-{profile}.yml(推荐)

master分支

1
2
3
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分支

1
2
3
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

/{application}-{profile}.yml

1
2
3
4
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(不存在的配置)

/{application}/{profile}[/{label}]

1
2
3
http://config-3344.com:3344/config/dev/master
http://config-3344.com:3344/config/test/master
http://config-3344.com:3344/config/test/dev

重要配置细节总结

1
2
3
4
5
/{name}-{profiles}.yml
/{label}-{name}-{profiles}.yml
label:分支(branch)
name:服务名
profiles:环境(dev/test/prod)

成功实现了用 SpringCloud Config 通过 GitHub 获取配置信息

Config客户端配置与测试

1. 新建 cloud-config-client-3355 项目

2. cloud-config-client-3355 项目 pom.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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>springcloud2021</artifactId>
<groupId>com.sevattal.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3355</artifactId>
<dependencies>
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</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. cloud-config-client-3355 项目 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。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:
service-url:
defaultZone: http://localhost:7001/eureka

4. 修改 config-dev.yml 配置并提交到 GitHub 中,比如加个变量 age 或者版本号 version

5. cloud-config-client-3355 项目 主启动

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355
{
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}

6. cloud-config-client-3355 项目 业务类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.sevattal.springboot.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
{
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo()
{
return configInfo;
}
}

7. 测试

启动 Config 配置中心 3344 微服务并自测

http://config-3344.com:3344/master/config-prod.yml
http://config-3344.com:3344/master/config-dev.yml

启动 3355 作为 Client 准备访问

http://localhost:3355/configInfo

成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息可题随时而来

分布式配置的动态刷新问题

Linux 运维修改 GitHub 上的配置文件内容做调整
刷新 3344,发现 ConfigServer 配置中心立刻响应
刷新 3355,发现 ConfigClient 客户端没有任何响应
3355 没有变化除非自己重启或者重新加载
难到每次运维修改配置文件,客户端都需要重启??噩梦

Config 动态刷新之手动版

避免每次更新配置都要重启客户端微服务 3355

动态刷新步骤:

修改 3355 模块

1. POM引入actuator监控

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 修改 YML,添加暴露监控端口配置:

1
2
3
4
5
6
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"

3. @RefreshScope 业务类 Controller 修改

1
2
3
4
5
6
7
8
import org.springframework.cloud.context.config.annotation.RefreshScope;
...
@RestController
@RefreshScope//<-----
public class ConfigClientController
{
...
}

4. 测试

此时修改 github 配置文件内容 -> 访问 3344 -> 访问 3355

1
http://localhost:3355/configInfo

3355 改变没有 ??? 没有,还需一步

How

需要运维人员发送Post请求刷新3355

1
curl -X POST "http://localhost:3355/actuator/refresh"

再次测试

1
http://localhost:3355/configInfo

3355 改变没有 ??? 改了。

成功实现了客户端 3355 刷新到最新配置内容,避免了服务重启

想想还有什么问题?

假如有多个微服务客户端 3355/3366/3377
每个微服务都要执行—次 post 请求,手动刷新?
可否广播,一次通知,处处生效?
我们想大范围的自动刷新,求方法
赏

谢谢你请我吃糖果

支付宝
微信
  • Spring
  • Config
  • JAVA

扫一扫,分享到微信

微信分享二维码
SpringCloud-Bus-1
SpringCloud-GateWay-1
© 2025 Sevattal
这里只是一个微不足道的计算机从业者
  • All Articles

tag:

  • Ansible
  • JMeter
  • CRM
  • Mail
  • Conda
  • Corosync-Pacemaker-CRM
  • DM Database
  • Scrapy
  • Cloud
  • ASM
  • Mysql
  • Linux
  • Elasticsearch
  • FFmpeg
  • Hexo
  • RabbitMQ
  • SpringBoot
  • Spring
  • Maven
  • Kubernetes
  • Lightroom
  • Crontab
  • DRBD
  • LVM
  • Nginx
  • NTP
  • OpenSSH
  • SSH
  • NetworkCard
  • Supervisor
  • Swap
  • TinyProxy
  • GCC
  • VNC
  • Oracle
  • MongDB
  • MinIO
  • Mysql-Lock
  • Restore
  • MysqlDump
  • Procedure
  • HTML
  • XtraBackup
  • Nacos
  • Neo4j
  • Netty
  • NAT
  • Route
  • ACL
  • EIGRP
  • Cisco
  • OSPF
  • HUAWEI
  • Switch
  • STP
  • VLAN
  • VTP
  • Nmap
  • OpenFeign
  • Sentinel
  • Backup
  • EXP
  • RMAN
  • DG
  • FlashBack
  • Job
  • Oracle-Inspection
  • Oracle-Name
  • Oracle RAC
  • RAC
  • Trigger
  • Postgres
  • Postgres-Copy
  • Flask-SQLalchemy
  • PGPool-II
  • Postgres+PGPool-II
  • Rancher
  • Redis
  • Redis-Copy
  • Redis-Cluster
  • Redis-Memory
  • Redis-RDB
  • Redis-AOF
  • Redis-Sentinel
  • Interview
  • SNMP
  • Spring Security
  • Bus
  • Config
  • Consul
  • GateWay
  • Eureka
  • Sleuth
  • Stream
  • Zookeeper
  • Tomcat
  • VMware
  • ShareDisk
  • Video-Clip
  • Hystrix
  • Ribbon
  • Regedit
  • USB
  • Yaml
  • Zabbix

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true