spring cloud 入门系列七:基于Git存储的分布式配置中心
我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.
它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分。
其中服务端也称为分布式配置中心,他是独立的微服务应用,用来连接配置仓库并为客户端提供获取接口(这些接口返回配置信息、加密、解密信息等);
客户端是微服务架构中的各个微服务应用或基础设施,它们通过制定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
由于配置中心默认采用Git来存储配置信息,因此我们会用到Git相关的内容,如果没有用过Git或者忘记怎么用了,可以参考下廖雪峰老师的Git教程。
另外,我自己用的Git远程仓库是码云。
====================华丽的分割线===================
接下来看下代码怎么实现。
一、准备远程Git仓库
- 在Gitee上新建一个项目https://gitee.com/sam-uncle/spring-cloud-learning
- 在项目下新建子目录spring-cloud-config-file,然后新建三个文件
-

- 内容分别是from=git-dev-1.0、from=git-test-1.0、from=git-1.0
- 新建一个分支config-lable-test,新分支里面新建三个同名的文件,不过内容分别是from=git-dev-2.0、from=git-test-2.0、from=git-2.0
二、构建配置中心
先给出最终代码结构:

搭建过程如下:
- 新建maven工程config-server
- 修改POM文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sam</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent> <properties>
<javaVersion>1.8</javaVersion>
</properties>
<!-- 使用dependencyManagement进行版本管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencyManagement> <dependencies>
<!-- 引入config server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> </dependencies> </project> - 创建启动类
/**
* @EnableConfigServer
*
* 开启Spring Cloud Config 的服务端功能
*
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp { public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
} - 配置application.properties文件,指定远程仓库信息
server.port=7001
spring.application.name=config-server #配置Git仓库的地址
spring.cloud.config.server.git.uri=https://gitee.com/sam-uncle/spring-cloud-learning/
#配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.server.git.search-paths=spring-cloud-config-file
#这里配置你的Git仓库的用户名
spring.cloud.config.server.git.username=用户名
#这里配置你的Git仓库的密码
spring.cloud.config.server.git.password=密码 - 启动并验证
访问配置信息的URL与配置文件的映射关系如下:
- /{application}/{profile} [/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{appliction}-{profile}.properties
上面的url会映射{application}-{profile}.properties对应的配置文件,其中{label}对应Git上不同的分支,默认是master。
通过浏览器访问http://localhost:7001/sam/dev/config-label-test,结果如下:

三、实现客户端
最终代码结构:

搭建过程如下:
- 新建maven工程config-client
- 修改POM文件
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sam</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent> <properties>
<javaVersion>1.8</javaVersion>
</properties>
<!-- 使用dependencyManagement进行版本管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencyManagement> <dependencies>
<!-- 引入config依赖 -->
<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> </dependencies> </project> - 创建启动类
@SpringBootApplication
public class ConfigClientApp { public static void main(String[] args) {
SpringApplication.run(ConfigClientApp.class, args);
} } - 配置bootstrap.properties文件,指定config-server位置
server.port=7002
#{application}
spring.application.name=sam
#{profile}
spring.cloud.config.profile=dev
#{label}
spring.cloud.config.label=master #config server uri
spring.cloud.config.uri=http://localhost:7001/ - 创建controller
@RefreshScope
@RestController
public class TestController { /**
* 通过@Value 来讲配置文件中的值写入到代码中
*/
@Value("${from}")
private String from; @RequestMapping("/from")
public String from() {
return from;
}
} - 启动并测试

四、工作原理
Spring Cloud Config配置中心的工作原理如下:
- 客户端启动时,根据bootstrap.properties中配置的应用名{application}、环境名{profile}、分支名{label},向Config Server请求获取配置信息。
- Config Server根据自己维护的Git仓库信息和客户传递过来的配置定位信息去查找配置信息。
- 通过git clone命令将找到的配置信息下载到本地(Config Server的文件系统中)。在通过页面访问或启动客户端的时候,我们在服务端能看到如下下载的log:
-- ::58.055 INFO --- [nio--exec-] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-/spring-cloud-config-file/sam-dev.properties
-- ::58.055 INFO --- [nio--exec-] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-/spring-cloud-config-file/sam.properties - Config Server创建Spring 的ApplicationContext实例,并从Git本地仓库中加载配置文件,最后将这些配置内容读取出来返回给客户端。
- 客户端在获取外部配置信息后加载到客户端的applicationContext实例。
spring cloud 入门系列七:基于Git存储的分布式配置中心的更多相关文章
- spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config
我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...
- Spring Cloud Alibaba系列(二)nacos作为服务配置中心
Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持.使用 Spring Cloud Alibaba Nacos Config,您可 ...
- Spring Cloud构建微服务架构(四)分布式配置中心
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...
- Spring Cloud构建微服务架构(二)分布式配置中心
注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...
- Spring Cloud构建微服务架构(四)分布式配置中心(续)
先来回顾一下,在前文中我们完成了什么: 构建了config-server,连接到Git仓库 在Git上创建了一个config-repo目录,用来存储配置信息 构建了config-client,来获取G ...
- spring cloud 入门系列:总结
从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- spring cloud 入门系列五:使用Feign 实现声明式服务调用
一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...
- Spring Cloud 入门教程(七): 熔断机制 -- 断路器
对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...
随机推荐
- 使用Visual Studio创建图片精灵(Image Sprite)——Web Essential
原文:Creating Image Sprite in Visual Studio - Web Essential 译者注:有关图片精灵的信息请参阅http://baike.baidu.com/vie ...
- rabbitMQ之AMQP协议
1.什么是AMQP协议 即高级消息队列协议,规范客户端与消息中间件服务器之间的通信,并能相互操作. 2.AMQP协议的作用 降低应用程序之间的耦合度,这样不同应用之间的集成的难度将变得更小,并开发出更 ...
- Java 条形码生成(一维条形码)
utl:http://mianhuaman.iteye.com/blog/1013945 在这里给大家介绍一个java 生成条形码 jbarcode.jar 生成条形码 支持EAN13, EAN8, ...
- Leetocde_290_Word Pattern
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/49717803 Given a pattern and a ...
- unity C#更改系统默认鼠标指针
最近项目需要替换鼠标的默认图标,实现的效果是初始状态为一种图标,点击鼠标左键要换成另一种图标,按网上通用的方法做了以后,隐藏鼠标指针,在指针的位置画一个图片就可以了,但不知道怎么回事,这种方法画的图标 ...
- SharePoint WebPart 简单的读取列表内容的web部件
最近,自己也在学习写一些SharePoint的部件,也就是使用对象模型,下面,介绍一下自己刚刚写的小测试程序,不足之处,还请指正. 1. 新建项目 Vs2008 – 新建 – 项目 – 类库 – 输 ...
- android decorView详解
摘要 一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. ...
- windows from 手风琴
public class OutlookBar : Panel { private int SelectedBandHeight { get; set; } public int ButtonHeig ...
- Xamarin引用第三方包错误解决方法
http://www.cnblogs.com/ThenDog/p/7623720.html
- Python并发编程-RabbitMQ消息队列
RabbitMQ队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列 ...