【Spring Cloud】Config 集中化配置中心

前言

     微服務將單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,由於每個服務都需要配置信息才能運行,所以一套集中式的、動態的配置管理設施必不可少。

     Spring Cloud提供了ConfigServer來解決這個問題。

Spring Cloud Config 是什麼   

 一、概念

     Spring Cloud Config爲分佈式系統中的外部配置提供服務器和客戶端支持,它分爲Config Server和Config Client,Config Server爲所有環境中的應用程序管理其外部屬性,非常適合spring應用,也可以使用在其他語言的應用上。

二、服務端和客戶端

1.服務端也稱爲分佈式配置中心,是一個獨立的微服務應用,用來連接配置服務器並未客戶端提供獲取配置信息,加密/解密信息等訪問接口。

2.客戶端是通過制定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動時從配置中心獲取和加載配置信息。

3.配置服務器默認採用git來存儲配置信息,這樣有助於對環境配置進行版本管理,可以通過git客戶端工具來方便管理和訪問配置內容。

image.png

 

Config做什麼

  • 集中管理配置文件;
  • 不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release;
  • 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置信息;
  • 當配置發生變動時,服務不需要重啓即可感知到配置的變化並應用新的配置;
  • 將配置信息以REST接口形式暴露。

 

Config服務端配置

    一、GitHub網站創建倉庫-microservicecloud-config   

1.創建倉庫名--microservicecloud-config 

2. 複製一下項目的地址,方便clone到本地磁盤

我選擇的是HTTPS,這種方式不用配置密鑰

3.本地磁盤克隆倉庫代碼

git clone https://github.com/FirstComeHere/microservicecloud-config.git

二、在克隆後的代碼文件夾下創建配置文件--application.yml

注意:(縮進使用Tab,冒號後使用空格,否則後面無法獲取git服務器配置文件)否則會報錯

spring:  
    profiles:  
        active:  
        - dev
---
spring: 
    profiles: dev #開發環境
    application: 
        name: microservicecloud-config-atguigu-dev 
---  
spring: 
    profiles: test #測試環境
    application:  
        name: microservicecloud-config-atguigu-test     

三、配置文件上傳gitHub服務器

git add .
git commit -m 'init'
git push origin master

 四、創建Config服務-microservicecloud-config-3344

1.pom.xml文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.atguigu.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservicecloud-config-3344</artifactId>


	<dependencies>
		<!-- springCloud Config -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>		
		<!-- 圖形化監控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 熔斷 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<!-- 熱部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

</project>

2.配置文件application.yml

 uri就是我們配置的GitHub倉庫的地址,我使用的是https。

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri:  https://github.com/FirstComeHere/microservicecloud-config.git #GitHub上面的git倉庫名字
 

3.主啓動類註解

@EnableConfgiServer

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

4.啓動服務,查看效果

輸入地址多種格式:

http://localhost:3344/application-dev.yml
http://localhost:3344/application/dev/master
http://localhost:3344/master/application-dev.yml

動態切換環境dev/test 

 

Config客戶端配置 

一、在本地倉庫代碼新建配置文件--microsevicecloud-config-client.yml

   代碼如下,使用git命令上傳到服務器

spring:  
    profiles:  
        active:  
        - dev
---
server: 
    port: 8203
spring: 
    profiles: dev #開發環境
    application: 
        name: microservicecloud-config-client
eureka: 
    client: 
        service-url: 
            defaultZone: http://eureka-dev.com:7001/eureka/
---
server: 
    port: 8205
spring: 
    profiles: test 
    application: 
        name: microservicecloud-config-client
eureka: 
    client: 
        service-url: 
            defaultZone: http://eureka-test.com:7001/eureka/  

二、新建客戶端服務--microservicecloud-config-client-3355

1.pom文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.atguigu.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservicecloud-config-client-3355</artifactId>


	<dependencies>
		<!-- SpringCloud Config客戶端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>
 

2.兩個配置文件-application.yml 和bootstrap.yml

   application.yml是用戶級的資源配置項,bootstrap.yml是系統級的,優先級更高。

    Spring Cloud會創建一個Boostrap Context,作爲Spring應用的Application Context的父上下文,初始化的時候,Boostrap Context負責從外部源加載配置屬性並解析配置,這兩個上下文共享一個從外部獲取的Environment,BootStrap屬性有高優先級,默認情況下,不會被本地配置覆蓋。

     一個boostrap.yml文件,可以保證Boostrap Context和Application Context配置的分離。

(1)application.yml

spring:
  application:
    name: microservicecloud-config-client

(2)bootstrap.yml

服務啓動時,就會從Config 服務端去獲取配置信息。具體環境文件,是根據profile屬性值決定的。

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要從github上讀取的資源名稱,注意沒有yml後綴名
      profile: test   #本次訪問的配置項
      label: master   
      uri: http://config-3344.com:3344  #本微服務啓動後先去找3344號服務,通過SpringCloudConfig獲取GitHub的服務地址
 

3.添加一個測試接口,驗證獲取配置信息

package com.atguigu.springcloud.rest;

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

@RestController
public class ConfigClientRest
{

	@Value("${spring.application.name}")
	private String applicationName;

	@Value("${eureka.client.service-url.defaultZone}")
	private String eurekaServers;

	@Value("${server.port}")
	private String port;

	@RequestMapping("/config")
	public String getConfig()
	{
		String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
		System.out.println("******str: " + str);
		return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
	}
}

三、 測試客戶端從Config服務端拉取配置信息

測試地址:http://localhost:8205/config

開發地址:http://localhost:8203/config

 

 

                                                                           感謝您的訪問!

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