SpringCloud(八)Spring Cloud Config(1)——基礎入門

前面我們已經知道如何搭建一個Spring Cloud微服務架構的系統。這個架構裏面可能有幾個微服務,可能有幾十個微服務,也可能有上百個微服務甚至更多。假如,有一天我們改下日誌打印級別這個想法,如果只有少量的微服務我們可以一個一個去改,那麼如果有非常多的微服務也要一個一個去改嗎,不僅工作量大而且容器出錯。這該怎麼辦呢?

不要着急我們今天來看下這個東西Spring Cloud Config,它可以幫助我們把這些微服務的配置進行集中管理。除此之外,還可以

不同環境,不同配置,開發環境,測試環境,生產環境各一套配置互不干擾。

運行期間動態調整配置,系統在運行狀態下,也可以動態的更新調整配置從而對整個系統生效。

自動刷新,當源配置發生改變時,各個微服務裏面的配置會自動刷新。

 

當然了,能夠提供配置中心管理組件的不僅是Spring Cloud Config,還有:

Spring系列的Spring Cloud Config,Spring Cloud zookpeeper,Spring Cloud consul,

百度的disconf,阿里的diamond,攜程的apollo

今天呢我們主要來看Spring Config Cloud,請繼續往下看。

實戰項目源碼託管地址:https://github.com/cddofficial/SpringCloudRepo

目錄

1 Spring Cloud Config概述

1.1 簡介

2 Spring Cloud Config Server

2.1 簡介

2.2 實戰

2.2.1 創建遠程git倉庫

2.2.2 複製得到microservice-config-server項目

2.2.3 處理microservice-config-server

2.3.4 測試

3.Spring Cloud Config Client

3.1 簡介

3.2 實戰

3.2.1 複製得到microservice-config-client項目

3.2.2 修改microservice-config-client項目

3.2.3 測試

3.2.4 Spring Cloud Config Client啓動流程

3.2.5 注意事項

4.相關文檔資料


1 Spring Cloud Config概述

1.1 簡介

spring cloud config 爲分佈式系統外部化配置和客戶端的支持,它包括了config server和config client兩部分。由於config server和config client都實現了對spring environment和PropertySource的映射,因此spring cloud config非常適合spring應用程序,當然也可以和其他語言編寫的應用程序配合使用。

config server是一個可橫向擴展,集中式的配置服務器,它用於集中管理應用程序各個環境下的配置,默認使用git存儲配置內容(也可使用subversion,本地文件系統或valut存儲配置),因此可以很方便的實現對配置的版本控制與內容審計。

config client是config server的客戶端,用於操作存儲在config server上的配置屬性。

1.2 原理架構圖

Spring Cloud Config的架構是什麼呢,如下圖:

上圖中:

           Config Server指的是Spring Cloud Config Server,下文中的Config Server同理;

           Config Client指的是Spring Cloud Config Client,下文中的Config Client同理;

服務的配置信息我們存放在遠程git倉庫,Config Server從遠程git倉庫拉取配置信息,然後其他的Conifg Client又從Config Server獲取配置信息,繼而對所在微服務生效。

2 Spring Cloud Config Server

2.1 簡介

Spring Cloud Config Server爲外部配置(名稱-值對或等效的YAML內容)提供了一個基於HTTP資源的API,可以通過註解嵌入到Spring Boot項目裏面。其主要負責從遠程git倉庫拉取配置信息,又爲Spring Config Client提供配置信息源。

2.2 實戰

2.2.1 創建遠程git倉庫

接着在github上新創建一個倉庫config-repo-demo,把該倉庫clone到本地,給裏面添加兩個文件application.yml和

foobar-dev.yml,這兩個文件內容分別如下(用notepad++打開):

application.yml:

foobar-dev.yml:

接下來把這個倉庫所有的修改全部提交到github服務器上去,登錄到github上把這個倉庫的clone地址複製下來,如下:

2.2.2 複製得到microservice-config-server項目

複製eureka-server-service-discover微服務重命名爲microservice-config-server微服務(至於如何複製這裏不再贅述,請看eureka——實戰中的 “2.4.1 創建可樂微服務” 的 “複製得到eureka-client-coke),導入到IDE工具中,導入後,項目結構如下圖:

2.2.3 處理microservice-config-server

1.修改pom文件

刪掉eureka-server依賴,添加config-server依賴,修改後截圖如下:

整個pom文件代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<groupId>com.cdd.cloud</groupId>
	<artifactId>microservice-config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eureka-server-service-discover</name>
	<description>Demo project for Spring Boot</description>
 
	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
 
	<dependencies>
		<!-- config server依賴 -->
		<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-config-server</artifactId>
 		</dependency>
 		
 		<!-- test依賴 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<!-- 引入spring cloud的依賴,不能少,主要用來管理Spring Cloud生態各組件的版本 -->
    <dependencyManagement>
	    <dependencies>
	      <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-dependencies</artifactId>
	        <version>Finchley.SR2</version>
	        <type>pom</type>
	        <scope>import</scope>
	      </dependency>
	    </dependencies>
  	</dependencyManagement>
 
	<!-- 添加spring-boot的maven插件,不能少,打jar包時得用 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
</project>

2 修改啓動類

修改啓動類名稱爲:MicroserviceConfigServerApplication,刪掉@EnableEurekaServer 註解,給啓動類打上@EnableConfigServer    註解,修改後啓動類的代碼如下:

package com.cdd.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
 
@SpringBootApplication
@EnableConfigServer	// 啓用Config server
public class MicroserviceConfigServerApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(MicroserviceConfigServerApplication.class, args);
	}
}

3 修改yml文件

修改yml文件,其中spring.cloud.config.server.git.uri的值就是遠程git倉庫cloud-config-git-repo的clone地址,其中.git後綴可以不要,整個yml文件內容截圖如下:

2.3.4 測試

啓動microservice-config-server項目,可以根據下面的規則去查看剛纔的配置內容(提交到遠程github服務器的)。

項目啓動成功後,在瀏覽器地址欄輸入請求地址,請求地址要和上面的規則匹配上:

瀏覽器輸入:http://localhost:7070/abc-default.yml,響應頁面如下,可以看到確實是剛纔application.yml文件中的內容:

這個請求路徑http://localhost:7070/abc-default.yml中的abc-default.yml,因爲在遠程git倉庫沒有配置文件和它匹配,所以就返回默認的配置文件application.yml的內容,並且請求路徑後面的".yml"後綴也可以是".properties"同樣可以拿到正確的配置信息。

接下來,瀏覽器再訪問:http://localhost:7070/foobar-dev.properties,這個請求路徑用的是“.properties”後綴,應該獲取到遠程git倉庫的foobar-dev.yml配置文件的內容,響應頁面如下:

的確是這樣的展示的是foobar-dev.yml配置文件的內容,說明的我們的Spring Cloud  Config Sever已經搭建成功了。

3.Spring Cloud Config Client

3.1 簡介

Spring Cloud Config Client簡單翻譯就是Spring Cloud Config客戶端,從Spring Cloud Conifg server(Spring Cloud Config服務端)獲取配置信息,這些Spring Cloud Config Server或是Spring Cloud Config Client都可以集成到eureka client上。爲所在微服務

3.2 實戰

3.2.1 複製得到microservice-config-client項目

複製microservice-config-server微服務重命名爲microservice-config-client微服務(至於如何複製這裏不再贅述,請看eureka——實戰中的 “2.4.1 創建可樂微服務” 的 “複製得到eureka-client-coke),導入到IDE工具中,導入後,項目結構如下圖:

3.2.2 修改microservice-config-client項目

1.修改pom文件

添加spring-cloud-starter-config依賴,這個依賴中包含config client依賴,該依賴截圖如下:

等maven更新完成後,我們來看下現在該項目中是否存在spring-cloud-config-client依賴,如下圖,可見

spring-cloud-config-client依賴的確存在了:

整個pom文件內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<groupId>com.cdd.cloud</groupId>
	<artifactId>microservice-config-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eureka-server-service-discover</name>
	<description>Demo project for Spring Boot</description>
 
	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
 
	<dependencies>
		<!-- config client所在的依賴 -->
		<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-starter-config</artifactId>
 		</dependency>
 		
 		<!-- test依賴 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<!-- 引入spring cloud的依賴,不能少,主要用來管理Spring Cloud生態各組件的版本 -->
    <dependencyManagement>
	    <dependencies>
	      <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-dependencies</artifactId>
	        <version>Finchley.SR2</version>
	        <type>pom</type>
	        <scope>import</scope>
	      </dependency>
	    </dependencies>
  	</dependencyManagement>
 
	<!-- 添加spring-boot的maven插件,不能少,打jar包時得用 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
</project>

2. 修改啓動類

修改啓動類的類名爲:MicroserviceConfigClientApplication,移除掉啓動類上的的@EnableConfigServer,修改後整個啓動類代碼如下:

package com.cdd.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class MicroserviceConfigClientApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(MicroserviceConfigClientApplication.class, args);
	}
}

3 處理配置文件

一般在springboot項目中配置文件有這樣的規則:

例如application.yml,application.properties。".yml“配置文件的優先級高於”.properties"配置文件。

例如bootstrap.yml,application.yml文件,bootstrap.yml的優先級高於applicaton.yml配置文件(有一處例外:服務器端口如果在application.yml和bootstrap.yml配置文件中都配置了,那麼以applciaton.yml文件中配置爲準生效,建議如果用bootstrap.yml配置時,就不要在application.yml文件中配置任何內容)。

(1)清空application.yml配置文件裏面的所有內容。

(2)在resources目錄下,新建一個bootstrap.yml配置文件,文件內容如下:

server:
  port: 7071
  
spring:
  application:
    name: foobar  #與遠程配置文件名稱有關,可與下面的profile值,拼成foobar-dev 
  cloud:
    config:
      uri: http://localhost:7070
      profile: dev
      label: master #當config server的後端存儲是git時,默認是master

4 添加ConfigClientController類

在啓動類所在的包下新建一個controller包,在該包下新建一個ConfigClientController類,該類完整代碼如下:

package com.cdd.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {
	
	// 把配置文件中profile屬性值注入到該屬性
	@Value("${profile}")
	private String profile;
	
	@GetMapping("/profile")
	public String getProfile() {
		return profile;
	}

}

3.2.3 測試

接下來啓動microservice-config-server,microservice-config-client項目,啓動成功後,在

瀏覽器中輸入:http://localhost:7071/profile ,響應頁面如下,可以看到獲取到了遠程foobar-dev.yml文件的配置內容:

3.2.4 Spring Cloud Config Client啓動流程

spring cloud config client在啓動的時候,

先加載bootstrap.*裏面的配置;

接着連接 spring cloud server ;

再去加載遠程配置(git裏面的);

最後去加載spring cloud config client裏面的application.*文件

3.2.5 注意事項

1.如果遠程配置的profile: profile-dev ,現在spring cloud config client的appliction.yml中配置的profile: abc,那麼以哪個配置爲準呢?答案是遠程的。

2.如果spring cloud config client項目沒有在bootstrap.yml或application.yml文件中配置應用程序名稱,那麼在請求的時候獲取到的是遠程的默認配置,就是遠程application.yml文件中配置的profile: default-settings

3.如果我們在spring cloud config client的application.yml文件裏面進行配置了(沒有在bootstrap.yml文件中配置),啓動config server後,再啓動config client時會報下面這樣的錯,如下圖:

這是因爲spring cloud config client在啓動的時候先加載bootstrap.*文件裏面的配置,spring cloud config client裏面的bootstrap.yml默認端口配置爲8888。我們重新顯示的寫一個bootstrap.yml文件,在這個文件裏面進行配置就行了。

 

到這裏Spring Cloud Config的一個簡單實例就算完成了。哪裏有不對的地方希望大家批評指正啊!

 

4.相關文檔資料

Spring Cloud Config,官方文檔:https://cloud.spring.io/spring-cloud-static/Finchley.SR4/single/spring-cloud.html#_spring_cloud_config

Spring Cloud zookpeeper,官方文檔:https://cloud.spring.io/spring-cloud-static/Finchley.SR4/single/spring-cloud.html#_spring_cloud_zookeeper

Spring Cloud consul,官方文檔:https://cloud.spring.io/spring-cloud-static/Finchley.SR4/single/spring-cloud.html#_spring_cloud_consul

百度的disconf,官方學習文檔:https://disconf.readthedocs.io/zh_CN/latest/

阿里的diamond,github上文檔:https://github.com/gzllol/diamond

攜程的apollo, github上文檔:https://github.com/ctripcorp/apollo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章