SpringCloud學習系列之四-----配置中心(Config)使用詳解

前言

本篇主要介紹的是SpringCloud中的分佈式配置中心(SpringCloud Config)的相關使用教程。git

SpringCloud Config

Config 介紹

Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。github

開發準備

開發環境spring

  • JDK:1.8
  • SpringBoot:2.1.1.RELEASE
  • SpringCloud:Finchley

注:不必定非要用上述的版本,能夠根據狀況進行相應的調整。須要注意的是SpringBoot2.x之後,jdk的版本必須是1.8以上!bootstrap

確認了開發環境以後,咱們再來添加相關的pom依賴。瀏覽器

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

SpringCloud Config 示例

目前SpringCloud Config的使用主要是經過Git/SVN方式作一個配置中心,而後每一個服務從其中獲取自身配置所需的參數。SpringCloud Config也支持本地參數配置的獲取。若是使用本地存儲的方式,在 application.propertiesapplication.yml 文件添加 spring.profiles.active=native 配置便可,它會從項目的 resources路徑下讀取配置文件。若是是讀取指定的配置文件,那麼可使用 spring.cloud.config.server.native.searchLocations = file:D:/properties/ 來讀取。app

服務端

首先是服務端這塊,首先建立一個註冊中心,爲了進行區分,建立一個springcloud-config-eureka的項目。 代碼和配置和以前的基本同樣。
application.properties配置信息:分佈式

配置信息:測試

spring.application.name=springcloud-hystrix-eureka-server
server.port=8005
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/

配置說明:fetch

  • spring.application.name: 這個是指定服務名稱。
  • server.port:服務指定的端口。
  • eureka.client.register-with-eureka:表示是否將本身註冊到Eureka Server,默認是true。
  • eureka.client.fetch-registry:表示是否從Eureka Server獲取註冊信息,默認爲true。
  • eureka.client.serviceUrl.defaultZone: 這個是設置與Eureka Server交互的地址,客戶端的查詢服務和註冊服務都須要依賴這個地址。

服務端這邊只須要在SpringBoot啓動類添加@EnableEurekaServer註解就能夠了,該註解表示此服務是一個服務註冊中心服務。this

代碼示例:

@SpringBootApplication
    @EnableEurekaServer
    public class ConfigEurekaApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigEurekaApplication.class, args);
             System.out.println("config 註冊中心服務啓動...");
        }
    }

建立好了註冊中心以後,咱們再來建立一個配置中心,用於管理配置。
建立一個springcloud-config-server的項目。而後在application.properties配置文件添加以下配置:

配置信息:

spring.application.name=springcloud-config-server
 server.port=9005
 eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
 spring.cloud.config.server.git.uri = https://github.com/xuwujing/springcloud-study/
 spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo
 spring.cloud.config.server.git.username = 
 spring.cloud.config.server.git.password =

配置說明:

  • spring.application.name: 這個是指定服務名稱。
  • server.port:服務指定的端口。
  • eureka.client.serviceUrl.defaultZone: 這個是設置與Eureka Server交互的地址,客戶端的查詢服務和註冊服務都須要依賴這個地址。
  • spring.cloud.config.server.git.uri: 配置的Git長褲的地址。
  • spring.cloud.config.server.git.search-paths: git倉庫地址下的相對地址 多個用逗號","分割。
  • spring.cloud.config.server.git.username:git倉庫的帳號。
  • spring.cloud.config.server.git.password:git倉庫的密碼。

注:若是想使用本地方式讀取配置信息,那麼只需將spring.cloud.config.server.git的配置改爲spring.profiles.active=native,而後在resources路徑下新增一個文件便可。

這裏爲了進行本地配置文件測試,新建一個configtest.properties配置文件,添加以下內容:

word=hello world

代碼這塊也很簡單,在程序主類中,額外添加@EnableConfigServer註解,該註解表示啓用config配置中心功能。代碼以下:

、、、

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
        System.out.println("配置中心服務端啓動成功!");
    }
}

、、、

完成上述代碼以後,咱們的配置中心服務端已經構建完成了。

客戶端

咱們新建一個springcloud-config-client的項目,用於作讀取配置中心的配置。pom依賴仍是和配置中心同樣,不過須要新增一個配置,用於指定配置的讀取。
建立一個bootstrap.properties文件,並添加以下信息:

配置信息:

spring.cloud.config.name=configtest
spring.cloud.config.profile=pro
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=springcloud-config-server
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/

配置說明:

  • spring.cloud.config.name: 獲取配置文件的名稱。
  • spring.cloud.config.profile: 獲取配置的策略。
  • spring.cloud.config.label:獲取配置文件的分支,默認是master。若是是是本地獲取的話,則無用。
  • spring.cloud.config.discovery.enabled: 開啓配置信息發現。
  • spring.cloud.config.discovery.serviceId: 指定配置中心的service-id,便於擴展爲高可用配置集羣。
  • eureka.client.serviceUrl.defaultZone: 這個是設置與Eureka Server交互的地址,客戶端的查詢服務和註冊服務都須要依賴這個地址。

:上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部份內容才能被正確加載。由於bootstrap.properties的相關配置會先於application.properties,而bootstrap.properties的加載也是先於application.properties。須要注意的是eureka.client.serviceUrl.defaultZone要配置在bootstrap.properties,否則客戶端是沒法獲取配置中心參數的,會啓動失敗!

application.properties配置

spring.application.name=springcloud-config-client
server.port=9006

配置說明:

  • spring.application.name: 這個是指定服務名稱。
  • server.port:服務指定的端口。

程序主類代碼,和以前的基本一致。代碼以下:

代碼示例:

@EnableDiscoveryClient
    @SpringBootApplication
    public class ConfigClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApplication.class, args);
            System.out.println("配置中心客戶端啓動成功!");
        }
    }

爲了方便查詢,在控制中進行參數的獲取,並返回。@Value註解是默認是從application.properties配置文件獲取參數,可是這裏咱們在客戶端並無進行配置,該配置在配置中心服務端,咱們只需指定好了配置文件以後便可進行使用。

代碼示例:

@RestController
    public class ClientController {
        
        @Value("${word}")
        private String word;
        
        @RequestMapping("/hello")
        public String index(@RequestParam String name) {
            return name+","+this.word;
        }
    }

到此,客戶端項目也就構建完成了。

功能測試

完成如上的工程開發以後,咱們來進行測試。

本地測試

首先咱們把springcloud-config-server項目的application.properties配置文件添加spring.profiles.active=native配置,註釋掉spring.cloud.config.server.git相關的配置,而後在src/main/resources目錄下新建一個configtest.properties文件,而後在裏面添加一個配置word=hello world
添加完成以後,咱們依次啓動springcloud-config-eurekaspringcloud-config-serverspringcloud-config-client這三個項目。啓動成功以前,先看來看看配置中心服務端的配置文件獲取,在瀏覽器輸入:

http://localhost:9005/configtest-1.properties

查看該文件的配置信息。

:配置文件的名稱是configtest.properties,可是若是直接該名稱的話是獲取不到的,由於在配置文件名須要經過-來進行獲取,若是配置文件名稱沒有-,那麼添加了-以後,會自動進行匹配搜索。

springcloud config 的URL與配置文件的映射關係以下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

上面的url會映射{application}-{profile}.properties對應的配置文件,{label}對應git上不一樣的分支,默認爲master。

界面返回:

word: hello world

而後調用客戶端的接口,查看是否可以獲取配置信息。在瀏覽器上輸入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello world

示例圖:

在這裏插入圖片描述
在這裏插入圖片描述

Git測試

在完成本地測試以後,咱們把這個spring.profiles.active=native配置註釋掉,解除spring.cloud.config.server.git相關的註釋(帳號和密碼要填寫真實的),而後在git倉庫上創建一個config-repo 文件夾,新建configtest-pro.propertiesconfigtest-dev.properties兩個配置,這兩個的配置分別是word=hello world!!word=hello world!, 而後和configtest.properties配置文件一塊兒上傳到config-repo 文件夾中。

首先在瀏覽器輸入:

http://localhost:9005/configtest-dev.properties

瀏覽器返回:

word: hello world!

而後再瀏覽器輸入:

http://localhost:9005/configtest-pro.properties

瀏覽器返回:

word: hello world!!

上傳了configtest.properties文件,可是這個文件名稱沒有-,咱們想獲取其中參數的信息的話,能夠在而後-隨意添加一個參數,它會自動進行匹配,在瀏覽器輸入:

http://localhost:9005/configtest-1.properties

瀏覽器返回:

word: hello world

而後進行客戶端接口調用測試,在瀏覽器輸入:

http://localhost:9006/hello?name=pancm

瀏覽器返回:

pancm,Hello World!!

因爲這裏我配置的前綴是 pro ,因此讀取的是 configtest-pro.properties 文件的數據,想要獲取其餘的配置,修改spring.cloud.config.profile配置便可。

示例圖:

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

其餘

項目地址

基於SpringBoot2.x、SpringCloud的Finchley版本開發的地址:https://github.com/xuwujing/springcloud-study

基於SpringBoot1.x、SpringCloud 的Dalston版本開發的地址: https://github.com/xuwujing/springcloud-study-old

若是感受項目不錯,但願能給個star,謝謝!

音樂推薦

原創不易,若是感受不錯,但願留言推薦!您的支持是我寫做的最大動力! 版權聲明: 做者:虛無境 博客園出處:http://www.cnblogs.com/xuwujing CSDN出處:http://blog.csdn.net/qazwsxpcm     我的博客出處:http://www.panchengming.com

相關文章
相關標籤/搜索