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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391389.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

写作工具_4种加快数据科学写作速度的工具

写作工具I’ve been writing about data science on Medium for just over two years. Writing, in particular, technical writing can be time-consuming. Not only do you need to come up with an idea, write well, edit your articles for accuracy and flow, and proofr…

leetcode 91. 解码方法(dp)

解题思路 记忆化搜索&#xff0c;记录已经计算过的子问题 代码 func numDecodings(s string) int {temp:make([]int,len(s),len(s))for i : range temp {temp[i]-1}return de(s,0,temp) } func de(s string,cur int,dp []int) int {if curlen(s){return 1}if dp[cur]!-1{re…

python数据结构与算法

2019独角兽企业重金招聘Python工程师标准>>> http://python.jobbole.com/tag/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/ 转载于:https://my.oschina.net/u/3572879/blog/1611369

test5

test5 转载于:https://www.cnblogs.com/Forever77/p/11468284.html

ux和ui_阅读10个UI / UX设计系统所获得的经验教训

ux和uiAs a way to improve my UI/UX skills I decided to read the guidelines for 10 popular UI/UX design systems. In this article I will give you a concise summary of the most important concepts. 为了提高我的UI / UX技能&#xff0c;我决定阅读10种流行的UI / UX…

大数据(big data)_如何使用Big Query&Data Studio处理和可视化Google Cloud上的财务数据...

大数据(big data)介绍 (Introduction) This article will show you one of the ways you can process stock price data using Google Cloud Platform’s BigQuery, and build a simple dashboard on the processed data using Google Data Studio.本文将向您展示使用Google Cl…

第1次作业:阅读优秀博文谈感想

摘要&#xff1a;本文介绍第1次作业的详细内容&#xff0c;包括评分标准。 注&#xff1a;本次作业提交截止时间为UTC8(北京时间)&#xff0c;2017-9-17 22:00&#xff08;星期日&#xff09;&#xff0c;以博客发表日期为准。 1. 作业内容 阅读一些优秀博文&#xff08;见第二…

ubuntu 16.04常用命令

ip配置&#xff1a; 终端输入vi /etc/network/interfaces命令编辑配置文件,增加如下内容&#xff1a;         auto enp2s0    iface enp2s0 inet static    address 192.168.1.211    netmask 255.255.255.0    gateway 192.168.1.1 重启网卡&#xf…

leetcode 28. 实现 strStr()(kmp)

实现 strStr() 函数。 给你两个字符串 haystack 和 needle &#xff0c;请你在 haystack 字符串中找出 needle 字符串出现的第一个位置&#xff08;下标从 0 开始&#xff09;。如果不存在&#xff0c;则返回 -1 。 说明&#xff1a; 当 needle 是空字符串时&#xff0c;我们…

git 代码推送流程_Git 101:一个让您开始推送代码的Git工作流程

git 代码推送流程Im going to explain Git the way I wish someone had explained to me back when I was first learning. 我将以我希望有人在我第一次学习时向我解释的方式来解释Git。 Ill show how you can get started with just a few commands, and the concepts at wor…

多元时间序列回归模型_多元时间序列分析和预测:将向量自回归(VAR)模型应用于实际的多元数据集...

多元时间序列回归模型Multivariate Time Series Analysis多元时间序列分析 A univariate time series data contains only one single time-dependent variable while a multivariate time series data consists of multiple time-dependent variables. We generally use mult…

字符串基本操作

1.已知‘星期一星期二星期三星期四星期五星期六星期日 ’&#xff0c;输入数字&#xff08;1-7&#xff09;&#xff0c;输出相应的‘星期几 s星期一星期二星期三星期四星期五星期六星期日 d int(input(输入1-7:)) print(s[3*(d-1):3*d]) 2.输入学号&#xff0c;识别年级、专业…

linux:使用python脚本监控某个进程是否存在(不使用crontab)

背景&#xff1a; 需要每天定时去检测crontab进程是否启动&#xff0c;所以不能用crontab来启动检测脚本了&#xff0c;直接使用while 循环和sleep方式实现定时检测 # coding:utf-8 import os import send_message import datetime import timecurr_time datetime.datetime.no…

Go语言实战 : API服务器 (1) 技术选型

1. API是什么&#xff1f; API&#xff08;Application Programming Interface&#xff0c;应用程序编程接口&#xff09;是一些预先定义的函数或者接口&#xff0c;目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力&#xff0c;而又无须访问源码&#xf…

天猫客户端组件动态化方案——VirtualView 工具大更新

前文《天猫客户端组件动态化的方案——VirtualView 上手体验》都提到了自定义模板编译成二进制数据的过程&#xff0c;在 Android 版的 Playground 里内置了一个编译工具可以实时调测&#xff0c;然而业务开发过程中&#xff0c;不可能在手机上编译&#xff0c;而是在电脑或者后…

tableau可视化_如何在Tableau中构建自定义地图可视化

tableau可视化Sometime last year, I got fascinated with bubble charts when I saw a data visualization video, Hans Roslings 200 Countries, 200 Years, 4 Minutes - The Joy of Stats from BBC.去年的某个时候&#xff0c;当我看到一个数据可视化视频时&#xff0c;我迷…

数据分析和大数据哪个更吃香_处理数据,大数据甚至更大数据的17种策略

数据分析和大数据哪个更吃香Dealing with big data can be tricky. No one likes out of memory errors. ☹️ No one likes waiting for code to run. ⏳ No one likes leaving Python. &#x1f40d;处理大数据可能很棘手。 没有人喜欢内存不足错误。 No️没有人喜欢等待代码…

MySQL 数据还原

1.1还原使用mysqldump命令备份的数据库的语法如下&#xff1a; mysql -u root -p [dbname] < backup.sq 示例&#xff1a; mysql -u root -p < C:\backup.sql 1.2还原直接复制目录的备份 通过这种方式还原时&#xff0c;必须保证两个MySQL数据库的版本号是相同的。MyISAM…

test6

test6 转载于:https://www.cnblogs.com/Forever77/p/11474320.html

VueJs学习入门指引

新产品开发决定要用到vuejs&#xff0c;总结一个vuejs学习指引。 1.安装一个Node环境 去Nodejs官网下载windows版本node 下载地址&#xff1a; https://nodejs.org/zh-cn/ 2.使用node的npm工具搭建一个Vue项目&#xff0c;这里混合进入了ElementUI 搭建指引地址: https:…