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


前言

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

SpringCloud Config

Config 介紹

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

開發准備

開發環境

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

注:不一定非要用上述的版本,可以根據情況進行相應的調整。需要注意的是SpringBoot2.x以后,jdk的版本必須是1.8以上!

確認了開發環境之后,我們再來添加相關的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/ 來讀取。

服務端

首先是服務端這塊,首先創建一個注冊中心,為了進行區分,創建一個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/

配置說明:

  • 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注解就可以了,該注解表示此服務是一個服務注冊中心服務。

代碼示例:


	@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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM