SpringCloud config分布式配置中心

概述

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

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

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

是什么



是什么

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

怎么玩

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

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

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

能干嘛

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

    post/crul访问刷新即可...

与GitHub整合配置

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

官网

https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/

Config服务端配置与测试

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

由上一步获得刚新建的git地址

  • git@github.com:zz
  • 说明一下,我之前创建过,所以这里我直接使用以前的config仓库 EiletXie/config-repo

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

注意:这个只是让你可以本地git修改config-repo仓库的信息与该项目关系不大

本地地址: 自己选一个目录,我这里选择的是 E:\IdeaWorkspace\springcloud_sell\config-repo

git命令:git clone git@github

此时在本地盘符下的文件

  • 表示多个环境的配置文件
  • 保存格式必须为UTF-8
  • 如果需要修改,此处模拟运维人员操作git和github
    • git add .
    • git commit -m "内容"
    • git push origin master

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

POM

<dependencies>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.eiletxie.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>

YML

server:
port: 3344 spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:eiletxie/config-repo.git #Github上的git仓库名字
##搜索目录.这个目录指的是github上的目录
search-paths:
- config-repo
##读取分支
label: master eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/

主启动类

ConfigCenterMain3344

@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 { public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}

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

127.0.0.1 config-3344.com

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

启动服务3344

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

读取配置规则

官网

/{label}/{application}-{profile}.yml

  • master分支
  • dev分支

/{application}-{profile}.yml

/{application}/{profile}/

重点配置细节总结

成功实现了SpringCloudConfig通过Github获取配置信息

Config客户端配置与测试

新建cloud-config-client-3355

POM

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.eiletxie.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>

bootstrap.yml

applicaiton. ym1是用户级的资源配置项

bootstrap . ym1是系统级的,优先级更加高

Spring Cloud会创建一个"Bootstrap Context" ,作为Spring应用的Application Context的父上下文。初始化的时候,‘Bootstrap Context'负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的'Environment'。

"Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。"Bootstrap context和Application Context有着不同的约定,所以新增了-个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。

要将Client模块下的application.ym|文件改为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
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/

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

主启动

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 { public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}

业务类

@RestController
public class ConfigClientController { // 因为config仓库以rest形式暴露,所以所有客户端都可以通过config服务端访问到github上对应的文件信息
@Value("${config.info}")
private String configInfo; @GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}

测试

启动Config配置中心3344和eureka7001:http://config-3344.com:3344/master/config-dev.yml

启动3355作为Client准备访问:

成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取信息配置

问题随之而来,分布式配置的动态刷新问题

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

Config客户端之动态刷新

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

动态刷新

步骤

修改3355模块

POM引入actuator监控

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

修改YML,暴露监控端口

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

@refreshScope业务类Controller修改

@RefreshScope

@RestController
@RefreshScope
public class ConfigClientController { // 因为config仓库以rest形式暴露,所以所有客户端都可以通过config服务端访问到github上对应的文件信息
@Value("${config.info}")
private String configInfo; @GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}

此时修改github 3344--->3355

http://localhost:3355/configInfo

3355改变没有??

没有呀

How

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

再次

http://localhost:3355/configInfo:ok了

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

想想还有什么问题

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

【SpringCloud】SpringCloud config分布式配置中心的更多相关文章

  1. java框架之SpringCloud(7)-Config分布式配置中心

    前言 分布式系统面临的配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中标会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...

  2. SpringCloud系列之分布式配置中心极速入门与实践

    SpringCloud系列之分布式配置中心极速入门与实践 @ 目录 1.分布式配置中心简介 2.什么是SpringCloud Config? 3.例子实验环境准备 4.Config Server代码实 ...

  3. SpringCloud 进阶之分布式配置中心(SpringCloud Config)

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

  4. SpringCloud学习笔记(九):SpringCloud Config 分布式配置中心

    概述 分布式系统面临的-配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...

  5. SpringCloud与微服务Ⅹ --- SpringCloud Config分布式配置中心

    一.SpringCloud Config是什么 分布式系统面临的问题 --- 配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个 ...

  6. springcloud 高可用分布式配置中心

    SpringCloud教程七:高可用的分布式配置中心(SpringCloud Config) 当服务有很多 都要从服务中心获取配置时 这是可以将服务中心分布式处理 是系统具备在集群下的大数据处理 主要 ...

  7. 《springcloud 三》分布式配置中心

    Git环境搭建 使用码云环境搭建git服务器端 码云环境地址:https://gitee.com/majie2018 服务端详解 项目名称:springboot2.0-config_server Ma ...

  8. springcloud学习之路: (五) springcloud集成SpringCloudConfig分布式配置中心

    SpringCloud全家桶中的分布式配置中心SpringCloudConfig, 它使用git来管理配置文件, 在修改配置文件后只需要调用一个接口就可以让新配置生效, 非常方便. SpringClo ...

  9. Spring Cloud Config 分布式配置中心【Finchley 版】

    一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...

  10. SrpingCloud 之SrpingCloud config分布式配置中心实时刷新

    默认情况下是不能及时获取变更的配置文件信息 Spring Cloud分布式配置中心可以采用手动或者自动刷新 1.手动需要人工调用接口   监控中心 2.消息总线实时通知  springbus 动态刷新 ...

随机推荐

  1. 关于前端上传excell时间的问题

    当前端导入excell里的数据时,只能获取到下面类似的这种数据 Excel存储的日期是从1900年1月1日开始按天数来计算的,也就是说1900年1月1日在Excel中是1. 转化的思路和对Excel中 ...

  2. 探索自联接(SELF JOIN):揭示数据间复杂关系的强大工具

    title: 探索自联接(SELF JOIN):揭示数据间复杂关系的强大工具 date: 2025/1/11 updated: 2025/1/11 author: cmdragon excerpt: ...

  3. 如何修改 Docker 和 Docker Compose 默认占用的网段

    在使用 Docker 和 Docker Compose 进行容器化部署时,Docker 默认会为容器分配一个私有网段(通常是 172.17.0.0/16).然而,在某些情况下,这个默认网段可能会与现有 ...

  4. biancheng-Hibernate框架

    目录http://c.biancheng.net/hibernate/ 1ORM是什么2Hibernate是什么3Hibernate项目创建流程4Hibernate增删改查操作5Hibernate工作 ...

  5. c代码部分封装为lib

    需求:将一个C工程中的核心代码封装为静态文件:lib. 环境 工具:VC6.0++ 语言:c 以封装一个DES工程为例 封装 (1)新建一个静态工程 (2)新建c文件和h文件 (3)挑选封装内容 在原 ...

  6. Web访问过程

    WEB访问的具体过程: 命令 浏览器中查看DNS chrome://net-internals/#dns 查看本地DNS ipconfig/displaydns 查看HOST文件 C:\Windows ...

  7. 使用kNN算法改进约会网站配对效果(尺度归一化问题)

    简单匹配:

  8. 央国企“严选”!天翼云夺得IaaS+PaaS市场桂冠!

    10月17日,赛迪顾问发布的<2024中国央国企云市场研究报告>显示,2023年,在中国央国企云"IaaS+PaaS"市场中,中国电信天翼云凭借行业云和全栈服务能力.渠 ...

  9. HTML 基本骨架

    HTML 基本骨架 HTML5的骨架是构建HTML5页面的基础结构,它主要由以下几个部分组成: <!DOCTYPE html> <html> <head> < ...

  10. MAC M1芯片 使用CocoaPods报错 ffi

    sudo arch -x86_64 gem install ffiarch -x86_64 pod install