本文属于【夯实Spring Cloud】系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货。系列目录如下:

【夯实Spring Cloud】Dubbo沉睡5年,Spring Cloud开始崛起!
【夯实Spring Cloud】Spring Cloud中基于maven的分布式项目框架的搭建
【夯实Spring Cloud】Spring Cloud中的Eureka服务注册与发现详解
【夯实Spring Cloud】Spring Cloud中如何完善Eureka中的服务信息
【夯实Spring Cloud】Spring Cloud中使用Eureka集群搭建高可用服务注册中心
【夯实Spring Cloud】Spring Cloud中的Eureka和Zookeeper的区别在哪?
【夯实Spring Cloud】Spring Cloud中使用Ribbon实现负载均衡详解(上)
【夯实Spring Cloud】Spring Cloud中使用Ribbon实现负载均衡详解(下)
【夯实Spring Cloud】Spring Cloud中自定义Ribbon负载均衡策略
【夯实Spring Cloud】Spring Cloud中使用Feign实现负载均衡详
【夯实Srping Cloud】Spring Cloud中使用Hystrix实现断路器原理详解(上)
【夯实Srping Cloud】Spring Cloud中使用Hystrix实现断路器原理详解(下)
【夯实Spring Cloud】Spring Cloud中使用Zuul实现路由网关详解
【夯实Spring Cloud】Spring Cloud分布式配置中心详解
【夯实Spring Cloud】未完待续


前面把 Spring Cloud 中的一些知识点都做了相应的介绍,相信读者对 Spring Cloud 的知识体系也有了一定的认识,这篇文章主要来介绍一下 Spring Cloud 中的分布式配置中心。

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

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

Spring Cloud 提供了 ConfigServer 来解决这个问题。

2. Spring Cloud Config 是什么?

Spring Cloud Config 分为两个部分:Config Server 和 Config Client。我们先来看一下整个 Spring Cloud Config 的一个流程。


Config Server:服务端也称为分布式配置中心,它是一个独立的微服务应用。用来连接配置服务器并为 Spring Client 客户端提供配置信息。

Config Client:客户但通过指定的配置中心来管理应用资源、以及业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。

配置服务器默认的是采用 git 来存储配置信息,这样有助于对环境配置进行版本管理,并且可以通过 git 客户端工具来方便的管理和访问配置内容。

下面来详细介绍下 Config Server 和 Config Client。

3. Config Server 与 GitHub 通信

由上图可知,Config Server 是与 GitHub 之间通信的,去 GitHub 读取配置信息。那么如何和 GitHub 通信呢?

1)在 GitHub 上新建一个 repository,我们命名为:microservice-config。

2)将该 repository 的东东 clone 到本地(git 命令啥的我就不说了)。

3)在本地新创建一个 application.yml 文件(保存格式必须是 UTF-8),并推送到 GitHub 上。application.yml 内容如下:

spring:profiles:active:- dev
---
spring:profiles: devapplication:name: microservice-config-client-dev
---
spring:profiles: testapplication:name: microservice-config-client-test

相当于我们写了三个配置,指定的是 dev 的配置,推送到 GitHub 之后,那么配置信息就搞好了,等下我们来读取这些配置信息。

4)在本地新建一个 model 项目:microservice-config,用作 Spring Cloud 的配置中心模块。导入依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>

主启动类需要添加 @EnableConfigServer 注解。

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

项目的配置文件:application.yml(跟上面的 application.yml 不是一个文件,上面的是推送到 GitHub 仓库的文件,这里是这个项目工程中的配置文件)。

server:port: 5555spring:application:name: microservice-configcloud:config:server:git:uri: https://github.com/eson15/microservice-config.git

端口号我们定为 5555,服务名称为:microservice-config。最重要的就是 spring.cloud.config.server.git.uri 这个值了,该值就是我们刚刚新建的 repository 的对应 git 地址。这样 Config Server 端就知道从哪获取配置信息了。

5)测试一下

启动该配置中心,在浏览器输入:http://localhost:5555/application-dev.yml 可以获取如下信息:

spring:application:name: microservice-config-client-devprofiles:active:- dev

再输入:http://localhost:5555/application-test.yml 可以获取如下信息:

spring:application:name: microservice-config-client-testprofiles:active:- dev

说明 Config Server 和 GitHub 通信成功,可以获取相应的配置信息,接下来我们就要来搞一下 Config Client 端了。

3. Conifg Client 通过 Config Server 与 GitHub 通信

之前我们写代码,客户端都是直接通过 application.yml 配置文件,获取相关的配置,也就是说,配置信息都写在服务本身的 application.yml 里了。现在客户端通过上面的Config Server(即配置中心),能否获取到我们传在 GitHub 上的配置呢?

我们新建一个客户端的 model 工程,取名为:microservice-config-client。导入 config 依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>

然后在 resource 目录下新建一个 bootstrap.yml 文件,application.yml 是用户级的资源配置项,bootstrap.yml 是系统级的,优先级更高。

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

所以新增一个 bootstrap.yml 文件,保证 bootstrap Contextapplication Context 配置的分离。

spring:cloud:config:uri: http://10.5.128.98:5555  # 服务启动后,去找5555服务name: microservice-config  # 从github上读取的资源名称,不要加 .yml后缀label: master # master分支profile: dev  # dev配置

解释:启动该服务,表示去 5555 服务找配置,5555 服务连了 GitHub。
Spring.cloud.config.name: microservice-config 表示读取 GitHub 上 microservice-config.yml 文件内容
Spring.cloud.config.label: master 表示读取 master 分支
Spring.cloud.config.profile: dev 表示读取 profile 为 dev 的配置

综合:表示读取 GitHub 上 master 分支下的 microservice-config.yml 文件中的 dev 分支里面的配置文件。

写一个Controller测试一下:

@RestController
@RequestMapping("/config")
public class ClientController {@Value("${spring.application.name}")private String springApplicationName;@Value("${server.port}")private String serverPort;@Value("${eureka.client.service-url.defaultZone}")private String defaultEurekaZone;@GetMapping("/test")public String getConfig() {return "applicationName=" + springApplicationName+ "; serverPort=" + serverPort+ "; defaultEurekaZone=" + defaultEurekaZone;}
}

启动5555服务,再启动该服务,在浏览器输入http://localhost:8881/config/test
即可出现:
applicationName=microservice-config-client; serverPort=8881; defaultEurekaZone=http://eureka01:7001/eureka/
说明配置读取完成。


源码下载地址:https://gitee.com/eson15/springcloud_study

更多优质文章请关注我的微信公众号【武哥聊编程】,回复“资源”、“架构”、“简历”等关键词,可以领取海量优质的视频学习资源。大家共同进步。

【夯实Spring Cloud】Spring Cloud分布式配置中心详解相关推荐

  1. Spring Cloud Alibaba Nacos 分布式配置中心

    文章目录 1 摘要 2 核心 Maven 依赖 3 核心代码 3.1 bootstrap 配置文件 3.2 application 配置文件 3.3 配置测试类 - Controller 层 3.4 ...

  2. Spring Cloud入门-Config分布式配置中心(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Spring Cloud Config 简介 在Git仓库中准备配置信息 配置仓库目录结构 master分支下的配置信息 dev分支下的配置信息 ...

  3. 第十二章 Spring Cloud Config 统一配置中心详解

    目录 一.配置问题分析及解决方案 1.问题分析 2.解决方案 二.Spring Cloud Config 介绍 1.Spring Cloud Config特性 2.Spring Cloud Confi ...

  4. Apollo微服务配置中心详解

    Apollo微服务配置中心详解 前言 一.Apollo架构 (一)简介 (二)角色介绍 (三)服务端实现 (四)客服端实现 二.Apollo部署 (一)准备数据库 (二)配置服务 1. 手动部署 (1 ...

  5. Apollo(阿波罗)分布式配置安装详解

    Apollo(阿波罗) Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于 ...

  6. Spring Cloud 如何选择分布式配置中心

    ##微服务必备的几样武器有了,才能独闯武林, 有哪几样呢? 注册中心(eureka, consul, zk, etcd) 配置中心 (Spring Cloud Config, disconf ) AP ...

  7. Java Spring Data Redis实战与配置参数详解 application.properties...

    Redis作为开源分布式高并发缓存,使用范围非常广泛,主流互联网公司几乎都在使用. Java Spring Boot 2.0实战开发Redis缓存可以参考下面的步骤,Redis安装可以直接使用Linu ...

  8. Java Spring Data Redis实战与配置参数详解 application.properties

    Redis作为开源分布式高并发缓存,使用范围非常广泛,主流互联网公司几乎都在使用. Java Spring Boot 2.0实战开发Redis缓存可以参考下面的步骤,Redis安装可以直接使用Linu ...

  9. 中小团队落地配置中心详解

    不知道配置文件上次什么时候修改的.修改了什么内容?改了配置文件还要重新发布项目或者手动触发重启服务?无缘无故发现配置文件错了影响到线上正常部署?你是否正在因为这些问题而困扰?50+线上项目,数百+配置 ...

最新文章

  1. oracle 常用sql
  2. php和python交互-PHP与Python进行数据交互
  3. python入门练习题-python入门练习题2
  4. JMeter初探五-配置元件与参数化
  5. 改进同步等待的网络服务端应用 (转)
  6. 知乎数据集成平台建设实践
  7. ARM硬件支持java技术Jazelle DBX
  8. MyBatis基本配置和实践(三)
  9. java实现从键盘上输入学生考试的科目和学生的每科分数,输出总分、最高分、最低分、平均分
  10. 给Eclipse插件的View加上菜单和工具条
  11. prthon日期型、字符串、数值、时间戳相互转换
  12. C# 机房重构——VS2017中没有报表控件(ReportView)
  13. 第三届厦门国际银行数创金融杯金融营销建模大赛-BaseLine
  14. Modbus 简单认识(楼宇自动化系统背景下的详实总结
  15. 操作系统——页面淘汰算法
  16. 游戏编程入门(5):使用键盘和鼠标控制游戏
  17. 新疆计算机考试ppt教程,职考宝典2021新疆 职称计算机考试模块ppt2007真题试题软件...
  18. 服务器文件夹 删除 修改 日志,win服务器 删除文件夹
  19. vue移动端下拉加载分页全面解决方案
  20. X、合宙Air模块Luat开发:全网首发,通过iic直接驱动OLED,720Sl开始有显时代

热门文章

  1. 大一计算机论文_大一计算机论文
  2. DW大学生网页作业制作设计 ——旅游门户网站(21页)HTML+CSS+JavaScript
  3. ETC“大跃进”乱象背后:超级工程引发新的AI盛宴
  4. wf 《计算机专业英语》,武汉4-5岁MFWF轻松自信说英语课程
  5. miniUI实现指定行可编辑,其他行仍然只读
  6. win7浏览器主页修改不过来_win7系统ie主页无法修改怎么办_解决win7ie主页改不过来的办法...
  7. 爬虫—dy直播各个类别下直播数据
  8. FPGA基础知识----第三章 第2节 综合和仿真
  9. Android 插件化学习 加载apk并调用类的函数
  10. 4.测试基础(软件测试阶段的划分)