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.pswp.cn/news/391389.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391389.shtml
英文地址,請注明出處:http://en.pswp.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:…