Spring Cloud-Config Server 分散式配置中心
隨著我們接觸微服務、分散式系統這些概念和應用後,由於每個功能模組都能拆分為一個獨立的服務,服務數量不斷增加。一次業務可能需要多個服務來共同協調才能完成,為了方便服務配置檔案統一管理以及維護,我們就需要用到分散式配置中心元件,Spring Cloud提供了Spring Cloud Config Server分散式配置中心元件,如果大家使用過zookeeper去做分散式配置檔案或者disconf之類的就變得十分容易理解了。
Spring Cloud Config Server支援配置放在配置服務的記憶體中,也支援將其放在遠端Git倉庫中,而在Spring Cloud提供的分散式配置服務中主要包含兩個角色Config Server和Config Client。我們可以將公用的外部配置檔案集中放在一個Git倉庫中,然後建立一個Config Server來管理所有的配置檔案,維護配置檔案的時候只需要修改該Git倉庫中的配置檔案,其他所有服務都可以作為Config Client通過Config Server來獲取所需配置檔案,當然所有服務都通過同一個服務中心獲取配置檔案,那麼依賴性就相對增大,若服務中心掛了之後其他服務都不能正常使用,所以這裡是支援叢集化部署的。
在查閱資料的時候看到一張架構圖,大家可以參考理解下
1.Config Server基本使用
首先新建一個專案用於做配置中心服務使用,新增所需依賴
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.4.3.RELEASE</version>
</dependency >
我們只要在主程式上新增@EnableConfigServer註解,就可以使該服務變為配置服務
ConfigServerApp.java
package com.ithzk.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp
{
public static void main( String[] args )
{
SpringApplication.run(ConfigServerApp.class);
}
}
這裡我們把配置檔案放在遠端Git上,為了方便這裡我暫用碼雲,然後將專案地址配置到我們該服務中
application.yml
server:
port: 12900
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/onethree/spring-cloud-config #配置檔案的倉庫地址
我們在碼雲上新建一個專案,新增一個application.yml的配置檔案
完成以上步驟後啟動服務,訪問localhost:12900/abc-default.properties
或者localhost:12900/abc-default.yml,由於abc-default.yml檔案不存在,config server會預設去獲取
application.yml的配置
我們再新增一個config-dev.yml檔案
無需重啟,訪問localhost:12900/abc-default.yml
我們可以發現如果你獲取的配置檔案不存在則會自動獲取預設的配置檔案
如果存在則會將預設的和本身存在的一起獲取,如果屬性名稱相同,指定檔案中的屬性會覆蓋預設檔案中的屬性,例如此處abc
我們還可以通過訪問localhost:12900/配置檔名稱/default/master檢視該配置檔名可獲取對應的配置檔案
2.Config Client基本使用
上面我們搭建了一個Config server配置中心,當我們服務需要使用配置中心裡面的配置時該服務就可以被看作是一個config
client,下面給一個小示例來簡單介紹下如何是使用config client去獲取config server裡我們所需的配置檔案
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
application.yml
server:
port: 13900
bootstrap.yml
spring:
cloud:
config:
uri: http://localhost:12900 # config-server地址,配置server相關的屬性應該放到 bootstrap.yml 中,bootstrap.yml會在載入application.yml之前載入
profile: dev
label: master #當使用 git 的時候 lable 預設是 master
application:
name: config #獲取name-profile對應的檔案,會獲以config開頭dev的檔案
ConfigClientApp.java
package com.ithzk.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class ConfigClientApp
{
public static void main( String[] args )
{
SpringApplication.run(ConfigClientApp.class);
}
}
ConfigClientController .java
package com.ithzk.spring.cloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author hzk
* @date 2018/7/4
*/
@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("getProfile")
public String getProfile(){
return profile;
}
}
訪問介面localhost:13900/getProfile會從config server配置的遠端倉庫中獲取對應名稱的配置檔案
3.Config萬用字元模式
之前我們配合的config server是從指定的spring-cloud-config倉庫中獲取配置檔案,當我們需要從多個倉庫中獲取的時候,
我們可以利用Config Server提供的萬用字元配置模式去配置,這裡我們建好三多個倉庫並放好配置檔案
如何讓我們Config Server同時可以從多個倉庫中獲取配置檔案呢,這裡需要修改一下配置,將之前指定倉庫改為{application}
application.yml
server:
port: 12900
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/onethree/{application}
通過ip:port/倉庫名稱-dev.properties(yml)即可從獲取指定倉庫中所有配置屬性
4.Config模式匹配
上面我們使用萬用字元模式可以使一個Config Server同時連線多個倉庫獲取配置檔案,我們還可以設定模式匹配
使特定倉庫配置檔案具有特定規則才可以獲取
application.yml
server:
port: 12900
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/onethree/spring-cloud-config # 預設配置
repos:
simple:
uri: https://gitee.com/onethree/simple # 當訪問的是 simple 的時候執行
special:
uri: https://gitee.com/onethree/special # 當訪問的是 special 並且符合以下規則的時候請求的 uri
pattern: special*/dev*,special*/test*
這裡我們可以看到只要符合simple字首都可以從simple倉庫中獲取配置檔案
但是我們special配置了特定規則訪問,只有符合規則才可以獲取special倉庫內容,否則獲取預設配置倉庫內容
5.Config搜尋路徑配置
如果我們想在倉庫裡面建不同資料夾去管理不同配置檔案,可以實現後獲取嗎
這裡,我們先建一個searchabc資料夾然後新建一個配置檔案
通過訪問路徑獲取可以發現獲取的是預設的配置檔案,也就是說無法獲取到指定資料夾裡配置檔案內容
那麼我們需要怎麼去獲取指定目錄下配置檔案呢,只需增加配置
bootstrap.yml
server:
port: 12900
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/onethree/spring-cloud-config # 預設配置
search-paths:
- searchabc # 以 searchabc 開頭的會被轉到 searchabc 目錄
clone-on-start: true # 啟動就載入檔案
#username: 讀時可以不設但是寫時建議設定
#password:
通過以上配置,我們就可以達到從指定目錄下獲取配置檔案的效果,這裡還可以配置使用者名稱和密碼控制安全性
6.Config Server整合認證
我們可以整合spring-boot提供的安全配置達到config server實現認證
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.yml
server:
port: 12900
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/onethree/spring-cloud-config # 預設配置
search-paths:
- searchabc # 以 searchabc 開頭的會被轉到 searchabc 目錄
clone-on-start: true
security:
user:
name: configserver
password: configserver
basic:
enabled: true
ConfigServerAuthApp.java
package com.ithzk.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerAuthApp
{
public static void main( String[] args )
{
SpringApplication.run(ConfigServerAuthApp.class);
}
}
啟動服務之後訪問發現需要認證才可獲取配置