SpringCloud - Config分布式配置中心
创始人
2024-02-22 00:56:53

文章目录

  • 一.概述
    • 1. 分布式系统面临的配置问题
    • 2. 什么是配置中心
    • 3. 配置中心怎么用
    • 4. 配置中心能做什么
  • 二.Config服务端配置与测试
    • 1. 搭建
      • 1.1 在github上创建一个springcloud_config的新仓库
      • 1.2 获得新仓库的地址:
      • 1.3 本地硬盘目录上新建git仓库并clone
      • 1.4 创建文件
      • 1.5 新建Module模块
      • 1.6 POM
      • 1.7 YML
      • 1.8 主启动类
      • 1.9 测试
    • 2. 配置读取规则
      • 2.1 /{label}/{application}-{profile}.yml(最推荐使用这种方式)
      • 2.2 /{application}-{profile}.yml
      • 2.3 /{application}/{profile}[/{label}]
      • 2.4 总结
  • 三.Config客户端配置和测试
    • 1. 搭建
      • 1.1. 新建cloud-config-client-3355模块
      • 1.2. POM
      • 1.3. bootstrap.yml
      • 1.4. 主启动类
      • 1.5. 业务类
      • 1.6. 测试
  • 四.动态刷新问题
    • 1. 问题:分布式的动态刷新问题
    • 2. Config动态手动刷新
      • 2.1 修改3355客户端模块
      • 2.2 POM引入actuator
      • 2.3 改YML
      • 2.3 controller层添加@RefreshScope注解
      • 2.4 测试
    • 3. 存留问题

一.概述

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

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

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

2. 什么是配置中心

在这里插入图片描述

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

官网: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

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

3. 配置中心怎么用

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

  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器,默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

4. 配置中心能做什么

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如 dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务回想配置中心统一拉去配置自己的信息。
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置。
  • 将配置信息以Rest接口的形式暴露。

二.Config服务端配置与测试

1. 搭建

1.1 在github上创建一个springcloud_config的新仓库

1.2 获得新仓库的地址:

https://gitee.com/jm1107/springcloud-config.git

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

在这里插入图片描述

1.4 创建文件

在D:\workspace\SpringCloud2022\springcloud-config创建自己的配置文件.

该文件可以从周阳老师的github仓库克隆一份,复制到自己的仓库 https://github.com/zzyybs/springcloud-config

1.5 新建Module模块

模块名:cloud-config-center-3344,它即为Cloud的配置中心模块CloudConfig Center

1.6 POM


cloud2022com.jm.springcloud1.0-SNAPSHOT4.0.0cloud-config-center-3344org.springframework.cloudspring-cloud-config-serverorg.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-actuatororg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtest

1.7 YML

server:port: 3344spring:application:name:  cloud-config-center #注册进Eureka服务器的微服务名cloud:config:server:git:uri: https://gitee.com/jm1107/springcloud-config.git #Gitee上面的git仓库名字####搜索目录search-paths:- springcloud-config####读取分支label: master#服务注册到eureka地址
eureka:client:service-url:defaultZone: http://localhost:7001/eureka

1.8 主启动类

package com.jm.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain3344 {public static void main(String[] args) {SpringApplication.run(ConfigCenterMain3344.class, args);}
}

1.9 测试

  • 启动EurekaServer7001
  • 启动ConfigCenterMain3344
  • 浏览器访问 :http://localhost:3344/master/config-dev.yml

在这里插入图片描述

2. 配置读取规则

2.1 /{label}/{application}-{profile}.yml(最推荐使用这种方式)

1) 读取master分支

  • http://localhost:3344/master/config-dev.yml
  • http://localhost:3344/master/config-test.yml
  • http://localhost:3344/master/config-prod.yml

2)读取dev分支

  • http://localhost:3344/dev/config-dev.yml
  • http://localhost:3344/dev/config-test.yml
  • http://localhost:3344/dev/config-prod.yml

2.2 /{application}-{profile}.yml

  • http://localhost:3344/config-dev.yml
  • http://localhost:3344/config-test.yml
  • http://localhost:3344/config-prod.yml

没有了分支/那一项,是因为在application.yml中配置了 label: master
所以会先去读取master的 (就算不配label 也会先去master寻找)

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

这样读取的是 JSON串

  • http://localhost:3344/config/dev/master
  • http://localhost:3344/config/test/master
  • http://localhost:3344/config/prod/master

2.4 总结

  • label:分支(branch)
  • name:服务名
  • profiles:环境(dev/test/prod)

三.Config客户端配置和测试

1. 搭建

1.1. 新建cloud-config-client-3355模块

1.2. POM


cloud2022com.jm.springcloud1.0-SNAPSHOT4.0.0cloud-config-client-3355org.springframework.cloudspring-cloud-starter-configorg.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-actuatororg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtest

1.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: 3355spring:application:name: config-clientcloud:# config 配置config:label: master #分支名称name: config # 配置文件名称profile: prod # 读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://localhost:3344/master/config-dev.ymluri: http://localhost:3344 # 配置中心地址
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka

在这里插入图片描述

1.4. 主启动类

package com.jm.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);}
}

1.5. 业务类

@Value("${config.info}')是从git上的配置文件中获取到的

@RestController
@RefreshScope
public class ConfigClientController {@Value("${config.info}")private String configInfo;@GetMapping("/configInfo")public String getConfigInfo(){return configInfo;}
}

1.6. 测试

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

  • http://localhost:3344/master/config-prod.yml
  • http://localhost:3344/master/config-dev.yml

启动3355作为Client准备访问

  • http://localhost:3355/configInfo

四.动态刷新问题

1. 问题:分布式的动态刷新问题

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

2. Config动态手动刷新

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

2.1 修改3355客户端模块

2.2 POM引入actuator


org.springframework.bootspring-boot-starter-actuator

2.3 改YML

# 暴露监控端点
management:endpoints:web:exposure:include: "*"

2.3 controller层添加@RefreshScope注解

@RestController
@RefreshScope//<------------
public class ConfigClientController {@Value("${config.info}")private String configInfo;@GetMapping("/configInfo")public String getConfigInfo(){return configInfo;}
}

2.4 测试

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

访问 http://localhost:3355/configInfo,发现并没有修改,需要运维人员发送Post请求刷新3355

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

再次访问http://localhost:3355/configInfo,发现配置已经需改了,成功实现了客户端3355刷新到最新配置内容,避免了服务重启。

3. 存留问题

但动态手动刷新存在多个问题:

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

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...