SpringCloud Config簡介
SpringCloud Config是SpringCloud團隊創建的一個全新項目,用來爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,它分爲服務端與客戶端兩個部分。其中服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置倉庫併爲客戶端提供獲取配置信息、加密 / 解密信息等訪問接口;而客戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息。SpringCloud Config實現了對服務端和客戶端中環境變量和屬性配置的抽象映射,所以它除了適用於Spring構建的應用程序之外,也可以在任何其他語言運行的應用程序中使用。由於SpringCloud Config實現的配置中心默認採用Git來存儲配置信息,所以使用SpringCloud Config構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,並且可以通過Git客戶端工具來方便的管理和訪問配置內容,當然它也提供了對其他存儲方式的支持,比如:GIT倉庫、SVN 倉庫、本地化文件系統,其架構如下圖:
Config Server端主要和Git/SVN服務器打交道,獲取放在Git/SVN裏的配置信息;通俗點說,SpringCloud Config就是用來統一配置管理維護微服務工程中所有配置文件,包括方便切換環境配置,以及修改配置無需動代碼,省心省力等
如果用上SpringCloud Bus,能實現無需重啓,自動感知配置變化以及應用新配置:
Config Server的基本使用
根據前面SpringCloud Config架構圖,首先要搞個configServer來聯通遠程GIT倉庫,讀取遠程配置;這裏的GIT倉庫,一般選用GitHub,或者碼雲 ,這裏用GitHub進行演示:
先登錄GitHub建個倉庫microservice-config,然後Git下載到本地;再上傳一個配置文件到git倉庫:
注:
application.yml文件要使用utf-8編碼,否則會出現亂碼、解析等各種問題
新建git倉庫時要將類型選爲public,如果選private,會造成config server無法訪問到
然後新建一個microservice-config-server-4001模塊,在pom里加入依賴:
<!--添加配置中心server依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
完整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>com.ue</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-config-server-4001</artifactId>
<dependencies>
<!-- 修改後立即生效,熱部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--添加配置中心server依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在啓動類加上@EnableConfigServer註解:
然後找到倉庫的Http地址進行copy:
microservice-config-server-4001項目的application.yml配置下(主要是要配置一個git請求地址):
server:
port: 4001
spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: https://github.com/java-LJ/microservice-config.git
然後在本地hosts文件加個本地域名映射:
127.0.0.1 configserver.test.com
然後只啓動這個項目,訪問http://configserver.test.com:4001/application-xxx.yml可以讀取到配置文件,結果如下:
至於請求路徑,是有匹配規則的:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
以springcloud-config-dev.yml爲例,它的application是springcloud-config,profile是dev,label是分支的意思,如果只有一個主分支,那可以不寫,因爲默認會訪問master分支,client會根據填寫的參數來選擇讀取對應的配置
Config Client的基本使用
根據前面的springcloud config原理圖,還需要建立client端調用server端,最終才能實現client端獲取遠程git配置信息;爲了後面演示方便,再提交兩個配置文件到遠程git庫:
然後新建一個microservice-config-client-5001模塊,在pom里加下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
因爲項目啓動的時候,就要調用server config端獲取配置信息,所以這裏需要一個bootstrap.yml配置文件(該文件的優先級比application.yml要高):
spring:
application:
name: application-dev
cloud:
config:
name: crm
uri: http://configserver.test.com:4001
profile: test
label: master
fail-fast: true
application.yml如下:
server:
port: 5001
context-path: /
然後再搞一個ConfigClientController類測試顯示端口:
package com.ue.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 {
@Value("${port}")
private String port;
@GetMapping("/getPort")
public String getPort() {
return "測試訪問的yml文件的端口是:【" + port + "】";
}
}
最後在本地hosts加配置:
127.0.0.1 client-config.test.com
然後啓動config server4001項目跟config client5001項目,用瀏覽器訪問http://client-config.test.com:5001/getPort即可獲取遠程端口配置信息,如下: