springcloud(六):配置中心git示例
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错。配置中心便是解决此类问题的灵丹妙药。
市面上开源的配置中心有很多,BAT每家都出过,360的QConf、淘宝的diamond、百度的disconf都是解决这类问题。国外也有很多开源的配置中心Apache Commons Configuration、owner、cfg4j等等。这些开源的软件以及解决方案都很优秀,但是我最钟爱的却是Spring Cloud Config,因为它功能全面强大,可以无缝的和spring体系相结合,够方便够简单颜值高我喜欢。
Spring Cloud Config
在我们了解spring cloud config之前,我可以想想一个配置中心提供的核心功能应该有什么
- 提供服务端和客户端支持
- 集中管理各环境的配置文件
- 配置文件修改之后,可以快速的生效
- 可以进行版本管理
- 支持大的并发查询
- 支持各种语言
Spring Cloud Config可以完美的支持以上所有的需求。
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例。
首先在github上面创建了一个文件夹config-repo用来存放配置文件,为了模拟生产环境,我们创建以下三个配置文件:
// 开发环境 neo-config-dev.properties // 测试环境 neo-config-test.properties // 生产环境 neo-config-pro.properties
每个配置文件中都写一个属性neo.hello,属性值分别是 hello im dev/test/pro 。下面我们开始配置server端
server 端
1、添加依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-config-server</artifactId> 5 </dependency> 6 </dependencies>
只需要加入spring-cloud-config-server包引用既可。
2、配置文件
1 server: 2 port: 8040 3 spring: 4 application: 5 name: spring-cloud-config-server 6 cloud: 7 config: 8 server: 9 git: 10 uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git仓库的地址 11 search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。 12 username: # git仓库的账号 13 password: # git仓库的密码
Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native
,Config Server会默认从应用的src/main/resource
目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/
属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。
3、启动类
启动类添加@EnableConfigServer
,激活对配置中心的支持
1 @EnableConfigServer 2 @SpringBootApplication 3 public class ConfigServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigServerApplication.class, args); 7 } 8 }
到此server端相关配置已经完成
4、测试
首先我们先要测试server端是否可以读取到github上面的配置信息,直接访问:http://localhost:8001/neo-config/dev
返回信息如下:
1 { 2 "name": "neo-config", 3 "profiles": [ 4 "dev" 5 ], 6 "label": null, 7 "version": null, 8 "state": null, 9 "propertySources": [ 10 { 11 "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", 12 "source": { 13 "neo.hello": "hello im dev" 14 } 15 } 16 ] 17 }
上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明server端已经成功获取了git仓库的配置信息。
如果直接查看配置文件中的配置信息可访问:http://localhost:8001/neo-config-dev.properties
,返回:neo.hello: hello im dev
修改配置文件neo-config-dev.properties
中配置信息为:neo.hello=hello im dev update
,再次在浏览器访问http://localhost:8001/neo-config-dev.properties
,返回:neo.hello: hello im dev update
。说明server端会自动读取最新提交的内容
仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
以neo-config-dev.properties为例子,它的application是neo-config,profile是dev。client会根据填写的参数来选择读取对应的配置。
client 端
主要展示如何在业务项目中去获取server端的配置信息
1、添加依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-config</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-web</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-test</artifactId> 13 <scope>test</scope> 14 </dependency> 15 </dependencies>
引入spring-boot-starter-web包方便web测试
2、配置文件
需要配置两个配置文件,application.properties和bootstrap.properties
application.properties如下:
spring.application.name=spring-cloud-config-client server.port=8002
bootstrap.properties如下:
spring.cloud.config.name=neo-config spring.cloud.config.profile=dev spring.cloud.config.uri=http://localhost:8001/ spring.cloud.config.label=master
- spring.application.name:对应{application}部分
- spring.cloud.config.profile:对应{profile}部分
- spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
- spring.cloud.config.uri:配置中心的具体地址
- spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。
特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。
3、启动类
启动类添加@EnableConfigServer
,激活对配置中心的支持
1 @SpringBootApplication 2 public class ConfigClientApplication { 3 4 public static void main(String[] args) { 5 SpringApplication.run(ConfigClientApplication.class, args); 6 } 7 }
启动类只需要@SpringBootApplication
注解就可以
4、web测试
使用@Value
注解来获取server端参数的值
1 @RestController 2 class HelloController { 3 @Value("${neo.hello}") 4 private String hello; 5 6 @RequestMapping("/hello") 7 public String from() { 8 return this.hello; 9 } 10 }
启动项目后访问:http://localhost:8002/hello
,返回:hello im dev update
说明已经正确的从server端获取到了参数。到此一个完整的服务端提供配置服务,客户端获取配置参数的例子就完成了。
我们在进行一些小实验,手动修改neo-config-dev.properties
中配置信息为:neo.hello=hello im dev update1
提交到github,再次在浏览器访问http://localhost:8002/hello
,返回:neo.hello: hello im dev update
,说明获取的信息还是旧的参数,这是为什么呢?因为springboot项目只有在启动的时候才会获取配置文件的值,修改github信息后,client端并没有在次去获取,所以导致这个问题。如何去解决这个问题呢?留到下一章我们在介绍。
示例代码
转载于:https://www.cnblogs.com/UniqueColor/p/7510481.html
springcloud(六):配置中心git示例相关推荐
- springcloud(七):配置中心svn示例和refresh
上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...
- springcloud 分布式配置中心 config server config client
---------------------------------------------------------------------------------------------------- ...
- SpringCloud config 配置中心集群配置以及整合消息总线BUS实现关联微服务配置自动刷新
一.SpringCloud Config 基本配置中的问题 在上一章节<SpringCloud config 配置中心介绍与基本配置使用>中我们现实了配置中心的配置集中管理.调用微服务应用 ...
- Java之 Spring Cloud 微服务的 SpringCloud Config 配置中心(第四个阶段)【二】【SpringBoot项目实现商品服务器端调用】
SpringCloud学习目录点击跳转对应的文章 Java之 Spring Cloud 微服务搭建(第一个阶段)[一][SpringBoot项目实现商品服务器端是调用] Java之 Spring Cl ...
- SpringCloud Config配置远程git仓库获取配置 解决Cannot clone or checkout repository
SpringCloud Config配置远程git仓库获取配置 解决Cannot clone or checkout repository 在学习springcloud Config配置远程git仓库 ...
- 六、springcloud之配置中心Config
一.配置中心提供的核心功能 Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对S ...
- java B2B2C源码电子商务平台-配置中心svn示例和refresh
国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使用svn+Spring Cloud Config来做配置中心.需要了解电子商务平台源码可加企鹅邱邱 一零三八七七四六二六 svn版本 ...
- SpringCloud 分布式配置中心Config Hoxton版本
Spring Cloud Config简介:Spring Cloud Config为分布式系统提供了服务端和客户端用于支持外部配置.使用Config Server可以在所有环境中管理应用程序的外部属性 ...
- java B2B2C电子商务平台分析之八--配置中心svn示例和refresh
国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使用svn+Spring Cloud Config来做配置中心.愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三 svn版本 ...
最新文章
- 剑指offer23:从上到下打印二叉树
- 线程共享全局变量(.data和.bbs)
- linux 下运行 jar包 java.lang.ClassNotFoundException: 解决办法
- shell脚本备份MySQL
- 一张图看懂AI、机器学习和深度学习的区别
- 基于vue-cli的快速开发框架
- 如何禁止用户安装应用程序,记下来,方便以后用
- python语言中浮点数_在Python中截断浮点数
- Python 识别图片文字( Tesseract 安装使用 )
- C++11:lambda表达式详细介绍
- 嵌入式开发培训好学吗?嵌入式培训课程怎么选?
- 使用Python进行OpenCV颜色检测和过滤
- LCD显示屏选购技巧是什么?
- 小白入门python教程自学python
- 带翻转特效的会员登录注册html页面源码
- 使用云效应用交付平台 AppStack进行应用管理
- 人工智能技术发展概述
- DNS安全(一)DNS缓存投毒与防护
- 【深度科普】辐射的真相
- 论文笔记【A Comprehensive Study of Deep Video Action Recognition】
热门文章
- 计算机专业多元协同,项目主导多元协同资源开放—软件技术专业人才培养体系的创新实践.pdf...
- 视网膜脱离oct报告图_刚刚,爱尔眼科发布关于艾芬医生诊疗过程的核查报告
- php string slice,substring()与str.slice()区别
- GDB与远程(交叉)GDB调试
- Java Formatter out()方法与示例
- 网络名称 转换 网络地址_网络地址转换| 计算机网络
- Java ObjectInputStream readLong()方法(带示例)
- C---编写程序:实现一个随堂测试,能进行加减乘除运算。要求如下:(1)随机产生两个1~10的正整数,在屏幕上输出题目,如:5+3=?(2)学生输入答案,程序检查学生输入答案是否正确,若正确,
- Java——设计模式(工厂方法模式)
- FLV封装格式的分析