spring cloud 入門系列七:基于Git存儲的分布式配置中心--Spring Cloud Config

我們前面接觸到的spring cloud組件都是基于Netflix的組件進行實現的,這次我們來看下spring cloud 團隊自己創建的一個全新項目:Spring Cloud Config.
它用來為分布式系統中的基礎設施和微服務提供集中化的外部配置支持,分為服務端和客戶端兩個部分。

其中服務端也稱為分布式配置中心,他是獨立的微服務應用,用來連接配置倉庫并為客戶端提供獲取接口(這些接口返回配置信息、加密、解密信息等);

客戶端是微服務架構中的各個微服務應用或基礎設施,它們通過制定的配置中心來管理應用資源與業務相關的配置內容,并在啟動的時候從配置中心獲取和加載配置信息。
由于配置中心默認采用Git來存儲配置信息,因此我們會用到Git相關的內容,如果沒有用過Git或者忘記怎么用了,可以參考下廖雪峰老師的Git教程。
另外,我自己用的Git遠程倉庫是碼云。
====================華麗的分割線===================
接下來看下代碼怎么實現。

一、準備遠程Git倉庫

  1. 在Gitee上新建一個項目https://gitee.com/sam-uncle/spring-cloud-learning
  2. 在項目下新建子目錄spring-cloud-config-file,然后新建三個文件
    1.   

      ?

    2. 內容分別是from=git-dev-1.0、from=git-test-1.0、from=git-1.0
  3. 新建一個分支config-lable-test,新分支里面新建三個同名的文件,不過內容分別是from=git-dev-2.0、from=git-test-2.0、from=git-2.0

?

二、構建配置中心

  先給出最終代碼結構:

  

  搭建過程如下:

  1. 新建maven工程config-server
  2. 修改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>
  3. 創建啟動類
    /*** @EnableConfigServer* * 開啟Spring Cloud Config 的服務端功能**/
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApp {public static void main(String[] args) {SpringApplication.run(ConfigServerApp.class, args);}
    }
  4. 配置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=密碼
  5. 啟動并驗證

    訪問配置信息的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,結果如下:

    


三、實現客戶端

? ??最終代碼結構:

  

?

?  搭建過程如下:

  1. 新建maven工程config-client
  2. 修改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>
  3. 創建啟動類
    @SpringBootApplication
    public class ConfigClientApp {public static void main(String[] args) {SpringApplication.run(ConfigClientApp.class, args);}}
  4. 配置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/
  5. 創建controller
    @RefreshScope
    @RestController
    public class TestController {/*** 通過@Value 來講配置文件中的值寫入到代碼中*/@Value("${from}")private String from;@RequestMapping("/from")public String from() {return from;}
    }
  6. 啟動并測試

    

?

四、工作原理
Spring Cloud Config配置中心的工作原理如下:

  1. 客戶端啟動時,根據bootstrap.properties中配置的應用名{application}、環境名{profile}、分支名{label},向Config Server請求獲取配置信息。
  2. Config Server根據自己維護的Git倉庫信息和客戶傳遞過來的配置定位信息去查找配置信息。
  3. 通過git clone命令將找到的配置信息下載到本地(Config Server的文件系統中)。在通過頁面訪問或啟動客戶端的時候,我們在服務端能看到如下下載的log:
    2018-05-14 22:51:58.055  INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam-dev.properties
    2018-05-14 22:51:58.055  INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam.properties
  4. Config Server創建Spring 的ApplicationContext實例,并從Git本地倉庫中加載配置文件,最后將這些配置內容讀取出來返回給客戶端。
  5. 客戶端在獲取外部配置信息后加載到客戶端的applicationContext實例。

轉載于:https://www.cnblogs.com/sam-uncle/p/9036053.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389648.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389648.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389648.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

458. 可憐的小豬

458. 可憐的小豬 有 buckets 桶液體&#xff0c;其中 正好 有一桶含有毒藥&#xff0c;其余裝的都是水。它們從外觀看起來都一樣。為了弄清楚哪只水桶含有毒藥&#xff0c;你可以喂一些豬喝&#xff0c;通過觀察豬是否會死進行判斷。不幸的是&#xff0c;你只有 minutesToTest…

熊貓數據集_大熊貓數據框的5個基本操作

熊貓數據集Tips and Tricks for Data Science數據科學技巧與竅門 Pandas is a powerful and easy-to-use software library written in the Python programming language, and is used for data manipulation and analysis.Pandas是使用Python編程語言編寫的功能強大且易于使用…

圖嵌入綜述 (arxiv 1709.07604) 譯文五、六、七

應用 圖嵌入有益于各種圖分析應用&#xff0c;因為向量表示可以在時間和空間上高效處理。 在本節中&#xff0c;我們將圖嵌入的應用分類為節點相關&#xff0c;邊相關和圖相關。 節點相關應用 節點分類 節點分類是基于從標記節點習得的規則&#xff0c;為圖中的每個節點分配類標…

聊聊自動化測試框架

無論是在自動化測試實踐&#xff0c;還是日常交流中&#xff0c;經常聽到一個詞&#xff1a;框架。之前學習自動化測試的過程中&#xff0c;一直對“框架”這個詞知其然不知其所以然。 最近看了很多自動化相關的資料&#xff0c;加上自己的一些實踐&#xff0c;算是對“框架”有…

1971. Find if Path Exists in Graph

1971. Find if Path Exists in Graph 有一個具有 n個頂點的 雙向 圖&#xff0c;其中每個頂點標記從 0 到 n - 1&#xff08;包含 0 和 n - 1&#xff09;。圖中的邊用一個二維整數數組 edges 表示&#xff0c;其中 edges[i] [ui, vi] 表示頂點 ui 和頂點 vi 之間的雙向邊。 …

移動磁盤文件或目錄損壞且無法讀取資料如何找回

文件或目錄損壞且無法讀取說明這個盤的文件系統結構損壞了。在平時如果數據不重要&#xff0c;那么可以直接格式化就能用了。但是有的時候里面的數據很重要&#xff0c;那么就必須先恢復出數據再格式化。具體恢復方法可以看正文了解&#xff08;不格式化的恢復方法&#xff09;…

python 平滑時間序列_時間序列平滑以實現更好的聚類

python 平滑時間序列In time series analysis, the presence of dirty and messy data can alter our reasonings and conclusions. This is true, especially in this domain, because the temporal dependency plays a crucial role when dealing with temporal sequences.在…

基于SmartQQ協議的QQ自動回復機器人-1

0. 本項目的原始代碼及我二次開發后的代碼 1. 軟件安裝:【myeclipse6.0 maven2】 0. https://blog.csdn.net/zgmzyr/article/details/6886440 1. https://blog.csdn.net/shuzhe66/article/details/45009175 2. https://www.cnblogs.com/whgk/p/7112560.html<mirror><…

1725. 可以形成最大正方形的矩形數目

1725. 可以形成最大正方形的矩形數目 給你一個數組 rectangles &#xff0c;其中 rectangles[i] [li, wi] 表示第 i 個矩形的長度為 li 、寬度為 wi 。 如果存在 k 同時滿足 k < li 和 k < wi &#xff0c;就可以將第 i 個矩形切成邊長為 k 的正方形。例如&#xff0c…

幫助學生改善學習方法_學生應該如何花費時間改善自己的幸福

幫助學生改善學習方法There have been numerous studies looking into the relationship between sleep, exercise, leisure, studying and happiness. The results were often quite like how we expected, though there have been debates about the relationship between sl…

Spring Boot 靜態資源訪問原理解析

一、前言 springboot配置靜態資源方式是多種多樣&#xff0c;接下來我會介紹其中幾種方式&#xff0c;并解析一下其中的原理。 二、使用properties屬性進行配置 應該說 spring.mvc.static-path-pattern 和 spring.resources.static-locations這兩屬性是成對使用的&#xff0c;如…

深挖“窄帶高清”的實現原理

過去幾年&#xff0c;又拍云一直在點播、直播等視頻應用方面潛心鉆研&#xff0c;取得了不俗的成果。我們結合點播、直播、短視頻等業務中的用戶場景&#xff0c;推出了“省帶寬、壓成本”系列文章&#xff0c;從編碼技術、網絡架構等角度出發&#xff0c;結合又拍云的產品成果…

學習總結5 - bootstrap學習記錄1__安裝

1.bootstrap是什么&#xff1f; 簡潔、直觀、強悍的前端開發框架&#xff0c;說白了就是給后端二把刀開發網頁用的&#xff0c;讓web開發更迅速、簡單。 復制代碼 2.如何使用&#xff1f; 如圖所示到bootstrap中文網進行下載 復制代碼 下載完成之后&#xff0c;如圖所示&#x…

519. 隨機翻轉矩陣

519. 隨機翻轉矩陣 給你一個 m x n 的二元矩陣 matrix &#xff0c;且所有值被初始化為 0 。請你設計一個算法&#xff0c;隨機選取一個滿足 matrix[i][j] 0 的下標 (i, j) &#xff0c;并將它的值變為 1 。所有滿足 matrix[i][j] 0 的下標 (i, j) 被選取的概率應當均等。 …

模型的搜索和優化方法綜述:

一、常用的優化方法&#xff1a; 1.爬山 2.最陡峭下降 3.期望最大值 二、常用的搜索方法&#xff1a; 1.貪婪搜索 2.分支界定 3.寬度&#xff08;深度&#xff09;優先遍歷轉載于:https://www.cnblogs.com/xyp666/p/9042143.html

Redis 服務安裝

下載 客戶端可視化工具: RedisDesktopManager redis官網下載: http://redis.io/download windos服務安裝 windows服務安裝/卸載下載文件并解壓使用 管理員身份 運行命令行并且切換到解壓目錄執行 redis-service --service-install windowsR 打開運行窗口, 輸入 services.msc 查…

熊貓數據集_對熊貓數據框使用邏輯比較

熊貓數據集P (tPYTHON) Logical comparisons are used everywhere.邏輯比較隨處可見 。 The Pandas library gives you a lot of different ways that you can compare a DataFrame or Series to other Pandas objects, lists, scalar values, and more. The traditional comp…

初級功能筆試題-1

給我徒弟整理的一些理論性的筆試題&#xff0c;不喜勿噴。&#xff08;所以沒有答案哈&#xff09; 1、測試人員返測缺陷時&#xff0c;如果缺陷未修復&#xff0c;把缺陷的狀態置為下列什么狀態&#xff08;&#xff09;。 2、當驗證被測系統的主要業務流程和功能是否實現時&a…

ansbile--playbook劇本案例

個人博客轉至&#xff1a; www.zhangshoufu.com 通過ansible批量管理三臺服務器&#xff0c;使三臺服務器實現備份&#xff0c;web01、nfs、backup&#xff0c;把web和nfs上的重要文件被分到backup上&#xff0c;主機ip地址分配如下 CharacterIP地址IP地址主機名Rsync--server1…

5938. 找出數組排序后的目標下標

5938. 找出數組排序后的目標下標 給你一個下標從 0 開始的整數數組 nums 以及一個目標元素 target 。 目標下標 是一個滿足 nums[i] target 的下標 i 。 將 nums 按 非遞減 順序排序后&#xff0c;返回由 nums 中目標下標組成的列表。如果不存在目標下標&#xff0c;返回一…