[菜鳥SpringCloud實戰入門]第六章:配置中心Spring Cloud Config初體驗

在這裏插入圖片描述

前言

歡迎來到菜鳥SpringCloud實戰入門系列(SpringCloudForNoob),該系列經過層層遞進的實戰視角,來一步步學習和理解SpringCloud。html

本系列適合有必定Java以及SpringBoot基礎的同窗閱讀。git

每篇文章末尾都附有本文對應的Github源代碼,方便同窗調試。github

Github倉庫地址:算法

github.com/qqxx6661/sp…spring

菜鳥SpringCloud實戰入門系列

你能夠經過如下兩種途徑查看菜鳥SpringCloud實戰入門系列bootstrap

前文回顧:後端

實戰版本

  • SpringBoot:2.0.3.RELEASE
  • SpringCloud:Finchley.RELEASE

-----正文開始-----

配置中心Spring Cloud Config初體驗

使用Git存放配置文件

每一個項目都會有不少的配置文件,若是採用分佈式的開發模式,須要的配置文件隨着服務增長而不斷增多。配置中心即是解決此類問題的靈丹妙藥。安全

Spring Cloud Config核心功能:springboot

  • 提供服務端和客戶端支持
  • 集中管理各環境的配置文件
  • 配置文件修改以後,能夠快速的生效
  • 能夠進行版本管理
  • 支持大的併發查詢
  • 支持各類語言

Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。Spring cloud使用git或svn存放配置文件,默認狀況下使用git,咱們先以git爲例作一套示例。bash

建立配置倉庫文件夾

在github上面建立了一個文件夾config-repo用來存放配置文件,爲了模擬生產環境,咱們建立如下三個配置文件:

// 開發環境
spring-cloud-config-dev.properties
// 測試環境
spring-cloud-config-test.properties
// 生產環境
spring-cloud-config-prod.properties
複製代碼

目錄爲:

在這裏插入圖片描述

每一個配置文件中都寫一個屬性config.hello,屬性值分別是 hello Im dev/test/pro 。

配置server端

下面咱們開始配置server端,咱們新建一個模塊名爲config-server,建立模塊請參考教程第一章。

pom中添加依賴:

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
</dependencies>
複製代碼

而後修改配置文件:

這裏須要配置你本身的github或者別的git倉庫,而且須要填寫本身的帳戶密碼,你能夠fork個人springcloud_for_noob項目,在項目基礎上進行修改。

server:
  port: 8769
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xxxxx(你本身的帳戶名)/springcloud_for_noob.git # 配置git倉庫的地址
          search-paths: config-repo                              # git倉庫地址下的相對地址,能夠配置多個,用,分割。
          username: xxxxxxxxx                               # git倉庫的帳號
          password: xxxxxxx                    
複製代碼

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的方式。

啓動類添加@EnableConfigServer:

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}
複製代碼

直接查看配置文件中的配置信息可訪問:

http://localhost:8769/spring-cloud-config-dev.properties

在這裏插入圖片描述

這樣服務端就創建好了,而且鏈接上了遠程配置文件倉庫。

配置client端

server搞起來以後,最終仍是要在業務項目中去獲取server端的配置信息

咱們新建一個模塊名爲config-client,建立模塊請參考教程第一章。

pom中添加依賴:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <!--沒法引入:spring-cloud-config-server-->
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
</dependencies>
複製代碼

配置文件:

爲了方便起見,咱們迴歸使用properties文件

須要配置兩個配置文件:

  • application.properties
  • bootstrap.properties

application.properties以下:

server.port = 8771
spring.application.name = spring-cloud-config-client
複製代碼

bootstrap.properties以下:

spring.cloud.config.name = spring-cloud-config
spring.cloud.config.profile = dev
spring.cloud.config.uri = http://localhost:8769/
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。

啓動類:

啓動類不用附加註解,直接能夠開始了。

測試類HelloController:

爲了測試客戶端可否正確獲取參數,咱們建立一個測試類HelloController

最終測試:

啓動項目後訪問:http://localhost:8769/hello ,返回:

在這裏插入圖片描述

如圖所示,拿到了配置參數,咱們完成了客戶端的測試。

但實際中,如更改了配置並將其push到了git服務器上,咱們經過客戶端訪問,仍然會獲取舊的參數。這是由於springboot項目只有在啓動的時候纔會獲取配置文件的值,修改git信息後,client端並無再次去獲取。下一章咱們的目標就是使client實現刷新機制。

本章代碼:

github.com/qqxx6661/sp…

參考

springcloud(六):配置中心git示例

www.ityouknow.com/springcloud…

-----正文結束-----

菜鳥SpringCloud實戰入門專欄全導航:經過如下兩種途徑查看

關注我

我是蠻三刀把刀,後端開發。主要關注後端開發,數據安全,爬蟲等方向。

來微信和我聊聊:yangzd1102

Github我的主頁:

github.com/qqxx6661

原創博客主要內容

  • Java知識點複習全手冊
  • Leetcode算法題解析
  • 劍指offer算法題解析
  • Python爬蟲相關技術實戰
  • 後端開發相關技術實戰
  • SpringCloud實戰

同步更新公衆號及如下所有博客:

1. Csdn

blog.csdn.net/qqxx6661

2. 知乎

www.zhihu.com/people/yang…

3. 掘金

juejin.im/user/5b4801…

4. 簡書

www.jianshu.com/u/b5f225ca2…

我的公衆號:Rude3Knife

我的公衆號:Rude3Knife

若是文章對你有幫助,不妨收藏起來並轉發給您的朋友們~

相關文章
相關標籤/搜索