Spring Cloud构建微服务架构(四)分布式配置中心
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行。
配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。当然他也提供本地化文件系统的存储方式,下面从这两方面介绍如何使用分布式配置来存储微服务应用多环境的配置内容。
构建Config Server
通过Spring Cloud构建一个Config Server,非常简单,只需要三步:
- pom.xml中引入
spring-cloud-config-server
依赖,完整依赖配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
- 创建Spring Boot的程序主类,并添加
@EnableConfigServer
注解,开启Config Server
1 2 3 4 5 6 7 8 9 |
@EnableConfigServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } |
application.properties
中配置服务信息以及git信息,例如:
1 2 3 4 5 6 7 8 |
spring.application.name=config-server server.port=7001 # git管理配置 spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringBoot-Learning/ spring.cloud.config.server.git.searchPaths=Chapter9-1-4/config-repo spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password |
- spring.cloud.config.server.git.uri:配置git仓库位置
- spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个
- spring.cloud.config.server.git.username:访问git仓库的用户名
- spring.cloud.config.server.git.password:访问git仓库的用户密码
到这里,使用一个通过Spring Cloud Config实现,并使用git管理内容的配置中心已经完成了,启动该应用,成功后开始下面的内容。
Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native
,Config Server会默认从应用的src/main/resource
目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/
属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。
服务端验证
为了验证上面完成的配置服务器,在http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/ 下创建了一个config-repo目录作为配置仓库,并根据不同环境新建了下面四个配置文件:
- didispace.properties
- didispace-dev.properties
- didispace-test.properties
- didispace-prod.properties
其中设置了一个from属性,为每个配置文件分别设置了不同的值,如:
- from=git-default-1.0
- from=git-dev-1.0
- from=git-test-1.0
- from=git-prod-1.0
为了测试版本控制,在master中,我们都加入1.0的后缀,同时创建一个config-label-test分支,并将各配置文件中的值用2.0作为后缀。
完成了这些准备工作之后,我们就可以通过浏览器或POSTMAN等工具直接来访问到我们的配置内容了。
URL与配置文件的映射关系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties
对应的配置文件,{label}
对应git上不同的分支,默认为master。
我们可以尝试构造不同的url来访问不同的配置内容,比如:要访问config-label-test分支,didispace应用的prod环境,可以通过这个url:http://localhost:7001/didispace/prod/config-label-test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "name": "didispace", "profiles": [ "prod" ], "label": "config-label-test", "version": "19de8a25575a7054a34230f74a22aa7f5575a9d1", "propertySources": [ { "name": "http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace-prod.properties", "source": { "from": "git-prod-2.0" } }, { "name": "http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace.properties", "source": { "from": "git-default-2.0" } } ] } |
微服务端映射配置
在完成并验证了配置服务中心之后,下面看看我们如何在微服务应用中获取配置信息。
- 创建一个Spring Boot应用,在pom.xml中引入spring-cloud-starter-config依赖,完整依赖关系如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
- 创建最基本的Spring Boot启动主类
1 2 3 4 5 6 7 8 |
@SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } |
- 创建
bootstrap.properties
配置,来指定config server,例如:
1 2 3 4 5 6 |
spring.application.name=didispace spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.uri=http://localhost:7001/ server.port=7002 |
- spring.application.name:对应前配置文件中的{application}部分
- spring.cloud.config.profile:对应前配置文件中的{profile}部分
- spring.cloud.config.label:对应前配置文件的git分支
- spring.cloud.config.uri:配置中心的地址
这里需要格外注意:上面这些属性必须配置在bootstrap.properties
中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties
,而bootstrap.properties
的加载也是先于application.properties
。
- 创建一个Rest Api来返回配置中心的from属性,具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@RefreshScope @RestController class TestController { @Value("${from}") private String from; @RequestMapping("/from") public String from() { return this.from; } } |
通过@Value("${from}")
绑定配置服务中配置的from属性。
启动该应用,并访问:http://localhost:7002/from ,我们就可以根据配置内容输出对应环境的from内容了。
完整示例:Chapter9-1-4
【转载请注明出处】:http://blog.didispace.com/springcloud4/
Spring Cloud构建微服务架构(四)分布式配置中心相关推荐
- Spring Cloud构建微服务架构:分布式配置中心【Dalston版】
Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端也称为 ...
- Spring Cloud构建微服务架构:分布式配置中心(加密解密)
最近正好想发一篇关于配置中心加密的细节内容,结果发现基础的加密解密居然漏了,所以在这个入门系列中补充一下.后面再更新一下,使用配置中心的一些经验和教训. 在微服务架构中,我们通常都会采用DevOps的 ...
- Spring Cloud构建微服务架构:分布式服务跟踪(整合zipkin)【Dalston版】
通过上一篇<分布式服务跟踪(整合logstash)>,我们虽然已经能够利用ELK平台提供的收集.存储.搜索等强大功能,对跟踪信息的管理和使用已经变得非常便利.但是,在ELK平台中的数据分析 ...
- Spring Cloud构建微服务架构:分布式服务跟踪(整合logstash)【Dalston版】
通过之前的<入门示例>,我们已经为两个由SpringCloud构建的微服务项目 trace-1和 trace-2引入了Spring Cloud Sleuth的基础模块 spring-clo ...
- Spring Cloud构建微服务架构:分布式服务跟踪(跟踪原理)
通过上一篇<分布式服务跟踪(入门)>的例子,我们已经通过Spring Cloud Sleuth往微服务应用中添加了实现分布式跟踪具备的基本要素.下面通过本文来详细说说实现分布式服务跟踪的一 ...
- Spring Cloud构建微服务架构:分布式服务跟踪(入门)
通过之前的N篇博文介绍,实际上我们已经能够通过使用它们搭建起一个基础的微服务架构系统来实现我们的业务需求了.但是,随着业务的发展,我们的系统规模也会变得越来越大,各微服务间的调用关系也变得越来越错综复 ...
- Spring Cloud构建微服务架构:分布式服务跟踪(入门)【Dalston版】
通过之前的N篇博文介绍,实际上我们已经能够通过使用它们搭建起一个基础的微服务架构系统来实现我们的业务需求了.但是,随着业务的发展,我们的系统规模也会变得越来越大,各微服务间的调用关系也变得越来越错综复 ...
- Spring Cloud构建微服务架构:分布式服务跟踪(抽样收集)【Dalston版】
通过 TraceID和 SpanID已经实现了对分布式系统中的请求跟踪,而这些记录的跟踪信息最终会被分析系统收集起来,并用来实现对分布式系统的监控和分析功能,比如:预警延迟过长的请求链路.查询请求链路 ...
- Spring Cloud构建微服务架构:分布式服务跟踪(收集原理)【Dalston版】
在本节内容之前,我们已经对如何引入Sleuth跟踪信息和搭建Zipkin服务端分析跟踪延迟的过程做了详细的介绍,相信大家对于Sleuth和Zipkin已经有了一定的感性认识.接下来,我们介绍一下关于Z ...
- Spring Cloud构建微服务架构:分布式服务跟踪(跟踪原理)【Dalston版】
通过上一篇<分布式服务跟踪(入门)>的例子,我们已经通过Spring Cloud Sleuth往微服务应用中添加了实现分布式跟踪具备的基本要素.下面通过本文来详细说说实现分布式服务跟踪的一 ...
最新文章
- 小马儿随笔——实地参观A级数据中心
- LUA Learning Note 4: 数据结构
- 【时间序列】Github最受欢迎的10大深度学习时间序列项目!
- CodeForces - 1300D Aerodynamic(几何+思维)
- Java读写大文本文件(2GB以上)
- 游戏引擎架构第二版中文pdf_阿里架构有多牛,一文带你看遍阿里技术架构!
- php七牛云rtmp直播推流,GitHub - jangocheng/FlutterQiniucloudLivePlugin: Flutter 七牛云直播云 推流/播放 SDK集成...
- 蓝桥杯 ALGO-83 算法训练 阶乘
- 网络分流器|100G网络分流器,不仅仅是带宽升级!
- 4个常用的计算机应用软件,信息技术应用--常用计算机工具软件4常用工具软件单元四.pdf...
- 关于static的使用
- Android获取用户通讯录上传,Android获取通讯录并上传(包含通讯录加密)(示例代码)...
- 初中计算机课堂游戏设计方案,初中信息技术教案设计
- kibana本地安装
- 工业相机基础知识详述 —— 焦平面,像平面,弥散圆,光圈,分辨率,景深,接口,靶面尺寸
- 题目1:输入两个整数A和B,输出它们的和。
- AI视频换脸方向论文阅读
- 为了给YiYi节省时间,写了个能自动拼图贴水印的机器人,很多bug,能用就行。...
- discuz 数据字典大全
- 单台服务器做redis集群