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
<Environment>
<name>springCloudConfig</name>
<profiles>
<profiles>dev</profiles>
</profiles>
<label>master</label>
<version/>
<state/>
<propertySources>
<propertySources>
<name>
https://github.com/souyunku/spring-cloud-config.git/application-dev.properties
</name>
<source>
<content>hello dev Spring Cloud Bus Test</content>
</source>
</propertySources>
</propertySources>
</Environment>
或者
{
"name": "springCloudConfig",
"profiles": ["dev"],
"label": "master",
"version": null,
"state": null,
"propertySources": [{
"name": "https://github.com/souyunku/spring-cloud-config.git/application-dev.properties",
"source": {
"content": "hello dev Spring Cloud Bus Test"
}
}]
}
证明配置服务中心可以从远程程序获取配置信息。
http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
客户端配置
Config Client
新建项目 spring-cloud-config-client
添加依赖(注意不能有server的依赖,服务器端也不能有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=8088
spring.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
类
Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config的更多相关文章
- springCloud学习-高可用的分布式配置中心(Spring Cloud Config)
1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务
上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...
- 【SpringCloud】第七篇: 高可用的分布式配置中心(Spring Cloud Config)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
详见:https://www.w3cschool.cn/spring_cloud/spring_cloud-jl8a2ixp.html 上一篇文章,留了一个悬念,Config Client 实现配置的 ...
- SpringCloud学习(七)高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 准备工作 ...
- Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh
上一篇文章讲了SpringCloudConfig 集成Git仓库,配和 Eureka 注册中心一起使用,但是我们会发现,修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置,这一篇我们尝试使用 ...
随机推荐
- [dart学习]第五篇:操作符
前言:本系列内容假设读者有一定的编程基础,如了解C语言.python等. 本节一起来学习dart的操作符,直接拷贝官网的操作符描述表如下: Description Operator unary pos ...
- 共享打印机,错误0x80070035和错误0x00000709的解决办法
这两个错误可以说是共享打印机里经常出现的错误了. 首先,要确认客户机可以ping通打印机的直连电脑的IP,如果这一步不通,那别玩了. 其次,很多人会忽略的一点儿,两个电脑的dns最好设置为相同的,经测 ...
- Hystrix-基本概念(设计原则和两种隔离技术)
一.Hystrix是什么在微服务的架构系统中,每个服务都可能会调用很多其他服务,被调用的那些服务就是依赖服务.有的时候某些依赖服务出现故障也是很正常的.Hystrix可以让我们在对服务间的调用进行控制 ...
- protobuf / Consul / 边缘计算 / MEC / CDN / Serverless / GraphQL / 微服务 / 网关 / 云原生 / Serverless (真能造概念啊!!!)
技术概念层出不穷,学吧!记录下自己多这些概念的理解 protobuf: 数据结构而已,类比XML, JSON consul 解决的只是微服务里的服务注册与发现,健康检查等. 边缘计算:可以理解为是指利 ...
- JQ也要面向对象~在JQ中扩展静态方法和实例方法(jq扩展方法)
JQ也要面向对象,事实上,无论哪种开发语言,在开发功能时,都要把面向对象拿出来,用它的思想去干事,去理解事,面向对象会使问题简单化,清晰化,今天说两个概念“静态方法”与“实现方法”,这个在面向对象的语 ...
- Scrapy五大核心组件工作流程
一.Scrapy五大核心组件工作流程 1.核心组件 # 引擎(Scrapy) 对整个系统的数据流进行处理, 触发事务(框架核心). # 调度器(Scheduler) 用来接受引擎发过来的请求. 由过滤 ...
- Linux中nohup和&的用法和区别
在Linux执行任务时,如果键入Ctrl+C退出进行其他任务或者关闭当前session 当前任务就会终止 要想不让进程停止或者让进程在后台运行,就需要一些命令,nohup和&就是一种非常好的方 ...
- 记录一次SignalR服务端实现过程
前言:最近手上一个项目需要后端实时推送数据到前端,第一个想到的就是微软的SignalR,由于之前都是平时没事写的Demo,没有用到实际项目中,这次恰好用到了,因此记录下来整个实现过程(网上也有很多类似 ...
- 第一个go程序
进入到工作空间中(我的是$HOME/go, 所以使用cd $HOME/go命令直接进入) 然后创建一个目录 src/hello( mkdir src/hello), 然后进入到该目录中(cd src/ ...
- 子组件props接受父组件传递的值,能不能修改的问题
参考链接:https://www.cnblogs.com/pangchunlei/p/11139356.html