spring cloud config 配置中心快速实现过程解析
作者:云天 发布时间:2022-02-19 06:50:22
spring-cloud-config 配置中心实现
Spring Cloud Config 用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为server端和client端。
server端为分布式配置中心,是一个独立的微服务应用;client端为分布式系统中的基础设置或微服务应用,通过指定配置中心来管理相关的配置。
Spring Cloud Config 构建的配置中心,除了适用于 Spring 构建的应用外,也可以在任何其他语言构建的应用中使用。
Spring Cloud Config 默认采用 Git 存储配置信息,支持对配置信息的版本管理。
本示例主要内容
配置中心演示client端和server端实现
配置文件放在git(因github有时候不太稳定,我放到了国内服务器)
版本切换(test、pro、dev)
Spring Cloud Config 特点
提供server端和client端支持(Spring Cloud Config Server和Spring Cloud Config Client);
集中式管理分布式环境下的应用配置;
基于Spring环境,实现了与Spring应用无缝集成;
可用于任何语言开发的程序;
默认实现基于Git仓库(也支持SVN),从而可以进行配置的版本管理;同时也支持配置从本地文件或数据库读取。
代码构建
server端实现
1.pom.xml添加maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2.application.yml配置
server:
port: 8001
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/tqlin/spring-boot-demo.git #因为github有时候不稳定,我这里改到了码云仓
searchPaths: /cloud-config/config-repo/ #配置文件目录
force-pull: true
3.CloudConfigServerApplication.java启动类
@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigServerApplication.class, args);
}
}
client端实现
1.pom.xml添加maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.bootstrap.properties配置文件
spring.cloud.config.name=easy-config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
spring.application.name:对应{application}部分
spring.cloud.config.profile:对应{profile}部分
spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
spring.cloud.config.uri:配置中心的具体地址(sever端地址)
spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。
特别注意:Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。
这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。
3.application.properties配置文件
spring.application.name=cloud-config-client
server.port=8002
运行示例
1.首先在码云上面创建一个文件夹config-repo用来存放配置文件,我们创建以下三个配置文件:
// 开发环境
easy-config-dev.properties 内容为:easy.hello=dev config
// 测试环境
easy-config-test.properties 内容为:easy.hello=test config
// 生产环境
easy-config-pro.properties 内容为:easy.hello=pro config
根据上面构建的代码指定的项目地址为:https://gitee.com/tqlin/spring-boot-demo.git 目录为: /cloud-config/config-repo/
2.分别运行server端和client端
找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分别运行
3.测试server端
直接访问:http://localhost:8001/easy-config/dev
我们看到成功返回了开发配置文件信息
{
name: "easy-config",
profiles: [
"dev"
],
label: null,
version: "6053b4c1c2343ac27e822b2a9b60c6343be72f96",
state: null,
propertySources: [
{
name: "https://gitee.com/tqlin/spring-boot-demo.git/cloud-config/config-repo/easy-config-dev.properties",
source: {
easy.hello: "dev config"
}
}
]
}
访问:http://localhost:8001/easy-config/test、http://localhost:8001/easy-config/pro,相应的会返回测试及正式环境的配置
仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
以easy-config-dev.properties为例子,它的application是easy-config,profile是dev。client会根据填写的参数来选择读取对应的配置。
4.测试client端
访问:http://localhost:8002/hello 我们发现界面成功返回了 test config,说明测试配置文件client端读取成功了
我们修改bootstrap.properties配置的spring.cloud.config.profile的值为dev,重启client端,访问:http://localhost:8002/hello 这时候界面返回 dev config,表示开发配置访问成功。
资料
Spring Cloud Config 示例源码
官网文档
来源:https://www.cnblogs.com/tqlin/p/11401870.html


猜你喜欢
- 1.把springboot项目打包成三个jar包,并指定端口为14341,14342,143432.下载腾讯云免费ssl证书,解压后会出现如
- 本文实例讲述了Android编程开发之TextView单击链接弹出Activity的方法。分享给大家供大家参考,具体如下:话不多说直接上码:
- 数值类型之间的转换6个实心箭头箭头表示无信息丢失的转换;3个虚箭头表示可能有精度损失的转换.当使用上面两个数值进行二元操作时,先要将两个操作
- 本文以实例描述了C#实现让窗体永远在窗体最前面显示的方法,具体步骤如下:1、新建一个窗体程序,添加一个Timer以及设置它可用并绑定事件。2
- 1. 最小生成树连通图中的每一棵生成树 , 都是原图的极大无环子图 , 即: 从中删去任何一条边 , 生成树就不再连通;反之 , 在其中引入
- SpringBoot默认的页面映射路径(即模板文件存放的位置)为“classpath:/templates/*.html”。静态文件路径为“
- 1.在C#中使用FolderBrowserDialog类,就可以实现选择文件夹的功能,并将所选择的的文件夹路径记录下来。(1).首先先引入命
- 本文实例讲述了java使用dom4j生成与解析xml文档的方法。分享给大家供大家参考,具体如下:xml是一种新的数据格式,主要用于数据交换。
- 概念装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。装饰者和被装饰对象有相同的超类型。你可以用一个或
- 优麒麟Ubuntu20.04中使用VS Code。VS Code的版本是1.48.0。以下内容仅限于上述环境,对于Windows环境下的使用
- 原理简介 & OpenGL 的优势裸眼 3D 效果的本质是——将整个图片结构分为 3 层:上
- 此处项目环境为简单的springboot+mybatis环境。可查看到上一篇文章搭建的简单springboot+mybatis的项目想要控制
- 利用 Spring 工厂加载机制,实例化 ApplicationListener 实现类,并排序对象集合创建应用事件 * 创建类实现接口Ap
- 由于 Spring 拥有对象的管理权,所以我们也需要拥有较为高效的对象存储和取出的手段,下面我们来分别总结一下:存对象配置文件在存储对象之前
- autoMapping和autoMappingBehavior的区别autoMappingBehaviormybatis核心配置文件中set
- 本文实例为大家分享了C++实现幸运大抽奖的具体代码,供大家参考,具体内容如下程序效果:#ifndef DIALOG_H#define DIA
- 看看效果图:我们项目中头像显示一般都是圆形的,但是有时候不排除各种样式(不一定是个规则的形状),比如 上次UI给了我一个 圆形下面少了一块。
- 在进行winform开发过程中有时候会需要关闭其他程序或者关闭进程,以前写过一篇相关介绍的文章,今天有同事问起来,于是在次翻出来和大家分享一
- 前言在上篇文章《初识GraphQL》中我们大致的了解了GraphQL作用,并通过简单示例初步体验了GraphQL的使用。下面我们从Hello
- 引导语本小节和大家一起来看看 CountDownLatch 和 Atomic 打头的原子操作类,CountDownLatch 的源码非常少,