【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置
一、为什么要统一管理微服务配置
对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护。
微服务的配置管理一般有以下需求:
1.集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的。
2.不同环境不同配置,比如数据源配置在不同环境(开发,生产,测试)中是不同的。
3.运行期间可动态调整。
4.配置修改后可自动更新。
好在Spring Cloud Config已经全部实现了上面几点。
二、Spring Cloud Config简介和使用
2.1原理
Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Client 和 Config Server两个部分。原理是所有的配置信息都存储在Config Server,所有的微服务都指向Config Server,
各个微服务启动时都会请求Config Server来获取配置信息,然后缓存到本地以提高性能。

2.2编写Config Server
1.在Git仓库https://github.com/2YSP/spring-cloud-config-repo(可以使用自己的仓库)新建几个配置文件,例如:

内容分别为:
profile=dev-1.0
profile=production-1.0
profile=test-1.0
profile=default-1.0
2.新建一个SpringBoot项目microservice-config-server,并添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3.在启动类添加 @EnableConfigServer注解
4.编写application.yml文件
server:
port: 8080
spring:
application:
name: microservice-config-server
cloud:
config:
server:
git:
# 配置Git仓库的地址
uri: https://github.com/2YSP/spring-cloud-config-repo.git
# 配置Git仓库的用户名
username: 2YSP
# 配置Git仓库的密码
password: XX
这样就完成了,可以使用端点来获取配置文件,端点与配置文件的映射规则如下:
/{application}/{profile}[/{lable}]
/{application}-{profile}.yml
/{lable}/{application}-{profile}.yml
/{application}-{profile}.properties
/{lable}/{application}-{profile}.properties
{application}表示微服务的名称,{profile}代表环境,{lable}表示Git仓库的分支,默认是master。
本例如果要访问microservice-foo-dev.properties,则可以访问这些URL:
http://localhost:8080/microservice-foo/dev
http://localhost:8080/microservice-foo-dev.properties
http://localhost:8080/microservice-foo-dev.yml
2.3编写Config Client
1.创建一个SpringBoot工程,ArtifactId为microservice-config-client,并添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.编写配置文件application.yml
server:
port: 8081
3.创建配置文件bootstrap.yml,并添加以下内容。
spring:
application:
# 对应Config Server所获取的配置文件的{application}
name: microservice-foo
cloud:
config:
uri: http://localhost:8080/
#对应config server所获取配置文件的{profile}
profile: dev
# 指定Git仓库的分支,对应config server所获取配置文件的{label}
label: master
需要注意的是,以上属性应配置在bootstrap.yml而不是application.yml文件中,否则部分配置就不能正常工作。
4.编写Controller
@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello(){
return this.profile;
}
}
这里通过注解 @Value("${profile}") 来绑定Git仓库的profile属性。
5.测试
先启动microservice-config-server,再启动microservice-config-client,访问http://localhost:8081/profile即可获得以下结果。
dev-1.0
说明能够正常的获取Git仓库的配置信息。
三、配置文件的手动刷新和自动刷新
3.1通过/refresh端点手动刷新
1.复制项目microservice-config-client更改为microservice-config-client-refresh
2.为项目添加spring-boot-starter-actuator依赖,如果有了就不添加了。
3.在Controller类上添加@RefreshScope注解
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello(){
return this.profile;
}
}
4.修改Git仓库中microservice-foo-dev.properties文件的内容,然后先发送POST请求到http://localhost:8081/refresh,再访问http://localhost:8081/refresh即可获取最新的配置。
3.2使用Spring Cloud Bus 实现自动刷新配置
1.首先安装RabbitMQ,安装步骤这里不介绍我的博客里有。
2.为项目添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
3.在bootstrap.yml中添加以下内容
spring:
application:
# 对应Config Server所获取的配置文件的{application}
name: microservice-foo
cloud:
config:
uri: http://localhost:8080/
#对应config server所获取配置文件的{profile}
profile: dev
# 指定Git仓库的分支,对应config server所获取配置文件的{label}
label: master
rabbitmq:
host: localhost
port: 5672 #默认端口 5672
username: guest
password: guest
四、Config Server的高可用
【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置的更多相关文章
- 9.Spring Cloud Config统一管理微服务配置
Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...
- 使用Spring Cloud Config统一管理配置,别再到处放配置文件了
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 可配置是一个成熟软件系统应该提供的特性,而配置管理对于大型系统就显得十分重要,特别是对于拥有多个应用的微服务系统.可喜的是, ...
- .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- SpringCloud学习笔记(7):使用Spring Cloud Config配置中心
简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务
上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...
- Spring Cloud Config实现集群配置中心
Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,可以管理集群中的配置文件.使用Git.SVN等版本管理系统存放配置文件,配置服务器会到版本管理系统获取配置,集群中的配置 ...
- Spring Cloud config之一:分布式配置中心入门介绍
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...
- Spring Cloud Config 1 (分布式配置中心)
spring cloud config是spring cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端和客户端两部分. 服务端也被称为 ...
- Spring Cloud Config入门(本地配置)
spring cloud config 简介 Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外 ...
随机推荐
- [Bzoj3611][Heoi2014]大工程(虚树)
3611: [Heoi2014]大工程 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 2000 Solved: 837[Submit][Status ...
- 最长不下降子序列 (O(nlogn)算法)
分析: 定义状态dp[i]表示长度为i的最长不下降子序列最大的那个数. 每次进来一个数直接找到dp数组第一个大于于它的数dp[x],并把dp[x - 1]修改成 那个数.就可以了 AC代码: # in ...
- 微信接入登录功能access_token流程记录
提示:只有认证过的订阅号或者服务号才能获取access_token. 1.app微信登录第一步是,app调起来微信客户端,通过app端的配置,引入一个微信类库, 2.授权成功后,微信会返回你一个cod ...
- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装
平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...
- VMware虚拟机上安装linux和克隆
虚拟机上安装好一台linux 系统后.为了高速搭建hadoop集群.须要再安装几个linux系统,比較笨的办法能够又一次用ios 镜像文件进行安装.可是又一次安装须要又一次配置一些信息并且安装时间比較 ...
- [Angular] Modify User Provided UI with Angular Content Directives
If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the con ...
- Office WORD里插入图片,嵌入型只能显示一半怎么办
如下图所示,公式编辑器插入的图片如果用嵌入型只能显示一半,但是改成其他方式即可全部显示 选中有问题的段落,点击设置为单倍行距即可
- IIS 配置 FTP 网站 H5 音频标签自定义样式修改以及添加播放控制事件
IIS 配置 FTP 网站 在 服务器管理器 的 Web服务器IIS 上安装 FTP 服务 在 IIS管理器 添加FTP网站 配置防火墙规则 说明:服务器环境是Windows Server 200 ...
- Swift String 一些经常用法
直接上代码 //字符串 //1 推断字符串是否为空 var test1Str="" var test1Str2:String = String(); println("t ...
- Android学习 多读官网,故意健康---手势
官网地址 ttp://developer.android.com/training/gestures/detector.html: 一.能够直接覆盖Activity的onTouch方法 public ...