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. Linux C语言面试考点

    数组 数组初始化方法 /* 以下为自动类型 */​/* 一维数组 */int arr[] = {1, 3, 5}; //不指定长度,由编译器自动计算int arr[5] = {0, }; //指定长度 ...

  2. .NET 响应式编程 System.Reactive 系列文章(三):Subscribe 和 IDisposable 的深入理解

    .NET 响应式编程 System.Reactive 系列文章(三):Subscribe 和 IDisposable 的深入理解 引言:为什么理解 Subscribe 和 IDisposable 很重 ...

  3. 微服务实战系列(一)-注册中心Springcloud Eureka服务端-copy

    1. 场景描述 springcloud提供了一整套可行的构建分布式系统的方案,使的企业/开发人员能够快速沟通分布式系统,今天快速构建下springcloud的注册中心Eureka. 2. 解决方案 2 ...

  4. 比特c语言-分支与循环

        # 分支与循环 if语句 目录 if语句 if eg:输入一个整数,判断是否为奇数 else eg:输入一个整数,判断是否为奇数,如果是奇数打印是奇数,否则打印偶数 嵌套if eg:输入一个人 ...

  5. 镜像分层复用与Dockerfile

  6. 【小记】在 Google Colab 等平台上运行 GPU 容器

    最近想到了可能的创新点,准备开始做实验了.咱想先在 Colab 这种提供免费 GPU 算力的平台上跑一些小实验,后续再转移到实验室机器上. 如果每次都要重复搭建环境多少有些麻烦了. 那咱用容器化技术不 ...

  7. 接口性能测试---locust脚本编写(一)

    本文分享自天翼云开发者社区<接口性能测试---locust脚本编写(一)>,作者:丁****乐 一.安装 locust是用python编写的一款开源接口性能测试工具,以python3为例, ...

  8. 天翼云发布边缘安全加速平台AccessOne,四大产品能力助力企业安全高速发展

    本文分享自天翼云开发者社区<天翼云发布边缘安全加速平台AccessOne,四大产品能力助力企业安全高速发展>,作者:天翼云社区官方账号 2023年5月30日全国科技工作者日,以" ...

  9. 安装VMware

    安装VMware 官网下载 首先需要进行账号注册:https://support.broadcom.com/ 注册完成后,进行账号登录:https://login.broadcom.com/signi ...

  10. 文本处理命令head tail more less tr cut paste wc

    文本处理命令 命令**head tail more less tr cut paste wc** 磁盘分区利用率 df|tr -s ' ' :|cut -d : -f5 df|tr -s ' ' :| ...