Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud
中,有分布式配置中心组件spring cloud config
,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config
组件中,分两个角色,一是config server
,二是config client
,业界也有些知名的同类开源产品,比如百度的disconf
。
相比较同类产品,SpringCloudConfig
最大的优势是和Spring
无缝集成,支持Spring
里面Environment
和PropertySource
的接口,对于已有的Spring
应用程序的迁移成本非常低,在配置获取的接口上是完全一致,结合SpringBoot
可使你的项目有更加统一的标准(包括依赖版本和约束规范),避免了应为集成不同开软件源造成的依赖版本冲突。
Spring Cloud Config 简介
SpringCloudConfig
就是我们通常意义上的配置中心,把应用原本放在本地文件的配置抽取出来放在中心服务器,从而能够提供更好的管理、发布能力。SpringCloudConfig
分服务端和客户端,服务端负责将git svn
中存储的配置文件发布成REST
接口,客户端可以从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置,这需要每个客户端通过POST
方法触发各自的/refresh
。
SpringCloudBus
通过一个轻量级消息代理连接分布式系统的节点。这可以用于广播状态更改(如配置更改)或其他管理指令。SpringCloudBus
提供了通过POST
方法访问的endpoint/bus/refresh
,这个接口通常由git
的钩子功能调用,用以通知各个SpringCloudConfig
的客户端去服务端更新配置。
注意:这是工作的流程图,实际的部署中SpringCloudBus
并不是一个独立存在的服务,这里单列出来是为了能清晰的显示出工作流程。
下图是SpringCloudConfig
结合SpringCloudBus
实现分布式配置的工作流
服务端配置
Config Server
新建项目 spring-cloud-config-server
添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId>
</dependency>
开启服务注册
在程序的启动类 ConfigServerApplication
通过 @EnableConfigServer
开启 SpringCloudConfig
服务端
package io.ymq.example.config.server;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
}
添加配置
配置文件 application.properties
spring.application.name=config-server
server.port=8888
spring.cloud.config.label=master
spring.cloud.config.server.git.uri=https://github.com/souyunku/spring-cloud-config.git
spring.cloud.config.server.git.search-paths=spring-cloud-config#spring.cloud.config.server.git.username=your username
#spring.cloud.config.server.git.password=your password
- spring.cloud.config.server.git.uri:配置git仓库地址
- spring.cloud.config.server.git.searchPaths:配置仓库路径
- spring.cloud.config.label:配置仓库的分支
- spring.cloud.config.server.git.username:访问git仓库的用户名
- spring.cloud.config.server.git.password:访问git仓库的用户密码
Git仓库如果是私有仓库需要填写用户名密码,示例是公开仓库,所以不配置密码。
远程Git仓库
spring-cloud-config
文件夹下有 application-dev.properties
,application-test.properties
三个文件,内容依次是:content=hello dev
,content=hello test
,content=hello pre
测试服务
启动程序 ConfigApplication
类
访问 Spring Cloud Config Server
服务:
http://localhost:8888/springCloudConfig/dev/master
{"name": "springCloudConfig","profiles": ["dev"],"label": "master","version": "b6fbc2f77d1ead41d5668450e2601a03195eaf16","state": null,"propertySources": [{"name": "https://github.com/souyunku/spring-cloud-config.git/application-dev.properties","source": {"content": "hello dev"}}]
}
证明配置服务中心可以从远程程序获取配置信息。
http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
客户端配置
Config Client
新建项目 spring-cloud-config-client
添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
开启服务注册
在程序的启动类 ConfigClientApplication
通过 @Value
获取服务端的 content
值的内容
package io.ymq.example.config.client;@RestController
@SpringBootApplication
public class ConfigClientApplication {@Value("${content}")String content;@RequestMapping("/")public String home() {return "content:" + content;}public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}
}
添加配置
配置文件 application.properties
spring.application.name=config-client
server.port=8088spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8888/
- spring.cloud.config.label 指明远程仓库的分支
- spring.cloud.config.profile
- dev开发环境配置文件
- test测试环境
- pro正式环境
- spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。
测试服务
启动程序 ConfigClientApplication
类
访问服务:http://localhost:8088/
下一篇,继续Spring Cloud Config Server
整合 eureka
, 等更多特性
源码下载
GitHub:https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config
码云:https://gitee.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config
Contact
- 作者:鹏磊
- 出处:http://www.ymq.io
- Email:admin@souyunku.com
- 版权归作者所有,转载请注明出处
- Wechat:关注公众号,搜云库,专注于开发技术的研究与知识分享
Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config相关推荐
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
上一篇文章,留了一个悬念,Config Client 实现配置的实时更新,我们可以使用 /refresh 接口触发,如果所有客户端的配置的更改,都需要手动触发客户端 /refresh ,当服务越来越多 ...
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
转载请标明出处: http://blog.csdn.net/forezp/article/details/81041045 本文出自方志朋的博客 个人博客纯净版:https://www.fangzhi ...
- springCloud学习-高可用的分布式配置中心(Spring Cloud Config)
1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务
上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...
- java B2B2C Springcloud多租户电子商城系统-(七)高可用的分布式配置中心(Spring Cloud Config)...
讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下: 一. ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (七)高可用的分布式配置中心(Spring Cloud Config)...
讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下: 一. ...
- java springcloud版b2b2c社交电商spring cloud分布式微服务 (七)高可用的分布式配置中心(Spring Cloud Config)...
Springcloud b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取 ...
- SpringCloud学习(七)高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 准备工作 ...
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
转:https://blog.csdn.net/forezp/article/details/70037513 上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当 ...
最新文章
- 检查Bash Shell脚本中输入参数的存在
- HTML form 标签的 accept-charset 属性
- 本地预览图片html和js例子
- spring boot修改内置容器tomcat的服务端口
- IIS服务器上增加mp4格式MIME 类型映射设置具体步骤
- 年轻人应该谨记的十点
- word怎么调列宽_怎么给文件加密???
- 年轻人,你的小米和宜家,要在一起了
- 可变参数列表来实现printf函数的输出
- 举例说明jquery插件的编写方法
- 英语口语 MP3 下载网址
- Java时间处理第三方包:Joda-Time
- 拉格朗日/柯西中值定理与高考数学计算
- java在regedit找不到_Windows找不到文件regedit打不开注册表的解决办法
- 获取Linux系统的网卡ip地址
- [教程] PSP 5.00M33-6升级图文教程
- 2019, XII Samara Regional Intercollegiate Programming (G、H、J、K、L题解)
- 小学语文经典好词好句好段
- [leetcode/lintcode 题解] 谷歌面试题:基因相似度
- DRC设计规则设置介绍-Design Compiler(四)