Spring Cloud7-Config配置中心

1.概述

1.1 微服務面臨的配置問題

微服務意味着要將單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,因此係統中會出現大量的服務。
由於每個服務都需要必要的配置信息才能運行,所以一套集中式的、動態的配置管理設施是必不可少的。
SpringCloud提供了ConfigServer來解決這個問題

1.2 SpringCloud Config

1.2.1 概述

SpringCloud Config爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不同微服務應用的所有環境提供了一箇中心化的外部配置。

1.2.2 分類

SpringCloud Config分爲服務端和客戶端兩部分。
服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置服務器併爲客戶端提供獲取配置信息,加密/解密信息等訪問接口.
客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。

1.2.3 優缺點

集中管理配置文件

不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release

運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置自己的信息

當配置發生變動時,服務不需要重啓即可感知到配置的變化並應用新的配置

將配置信息以REST接口的形式暴露

1.2.4 推薦與Git整合

由於SpringCloud Config默認使用Git來存儲配置文件(也有其它方式,比如支持SVN和本地文件),
但最推薦的還是Git,而且使用的是http/https訪問的形式

2.Config服務端

2.1 pom

	<!-- springCloud Config -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>

2.2 yml

spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
         ##gitlab或github的等git倉庫的地址
          uri: [email protected]:mygithub/microservicecloud-config.git 

2.3 main

@SpringBootApplication
@EnableConfigServer
public class Config_8308_StartSpringCloudApp
{
	public static void main(String[] args)
	{
		SpringApplication.run(Config_8308_StartSpringCloudApp.class, args);
	}
}

2.4 訪問格式

2.4.1 /{application}-{profile}.yml

http://config.xxxx.com/application-dev.yml  
http://config.xxxx.com/application-test.yml  
http://config.xxxx.com/application-xxx.yml(不存在的配置)  

2.4.2 /{application}/{profile}[/{label}]

http://config.xxxx.com/application/dev/master  
http://config.xxxx.com/application/test/master  
http://config.xxxx.com/application/xxx/master  

2.4.3 /{label}/{application}-{profile}.yml

http://config.xxxx.com/master/application-dev.yml  
http://config.xxxx.com/master/application-test.yml  

3.Config客戶端

3.1 bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要從github上讀取的資源名稱,注意沒有yml後綴名
      profile: test   #本次訪問的配置項
      label: master   
      #指向的本次config服務的地址
      uri: http://config.xxxx.com/ 
 


– 總結於網絡視頻及網絡文章

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