1. 程式人生 > >【SpringCloud系列】三、配置集中化管理--配置中心(Spring Cloud Config)

【SpringCloud系列】三、配置集中化管理--配置中心(Spring Cloud Config)

1、簡介

      在基於微服務架構下開發,由於服務數量巨多、在高可用性的驅使下要進行叢集時,眾多節點及服務的配置管理,變得苦不堪言,簡直成了體力活,稍微不細心將會出現配置錯誤。為了方便服務配置資訊的統一集中化管理,實時更新,SpringCloud中Spring Cloud Config元件(配置中心),就用來解決這類問題,以達到配置集中化管理,讓你可以把配置集中放到遠端伺服器,集中化管理叢集配置。

      從配置中心Spring Cloud Config的原始碼(spring-cloud-config-server)中,可以看出目前配置中心支援本地儲存、Git倉庫儲存、SVN倉庫儲存、資料庫儲存方式

,其他儲存方式可參考原始碼自行實現即可。

2、配置中心服務端Config Server

        配置中心服務端依賴於spring-cloud-config-server包,建立一個普通的springBoot專案springCloudConfig-git(此處以git方式儲存為例),額外新增如下依賴包:

<!-- config-server配置中心 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
	<version>2.0.0.RELEASE</version>
</dependency>

      在啟動類ConfigServerGitApplication中新增@EnableConfigServer註解,即:開啟支援配置中心的功能,如下:

package com.xcbeyond.springcloud.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * 配置中心。</br>
 * 以git方式儲存配置
 * @author xcbeyond
 * 2018年9月17日下午11:44:10
 */
@SpringBootApplication
//開啟配置伺服器的支援
@EnableConfigServer
//開啟作為Eureka Server的客戶端的支援
@EnableEurekaClient
public class ConfigServerGitApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerGitApplication.class, args);
	}
}

在配置檔案application.yml中進行如下配置:

此處為git倉庫集中管理配置檔案的配置方式。

(本例中配置了註冊中心,如果不需要,則可刪除註冊中心的配置)

#當前配置伺服器埠
server:
  port: 8888
spring:
  application:
    name: config-center

  cloud:
  #配置中心配置
    config:
      server:
        git:
          #配置git倉庫地址
          uri: https://github.com/xcbeyond/springCloudLearning.git
          #配置倉庫路徑
          search-paths: config-repo
          #配置檔案本地臨時儲存目錄
#          basedir: target/config
          #訪問git倉庫的使用者名稱.如果Git倉庫為公開倉庫,可以不填寫使用者名稱和密碼,如果是私有倉庫需要填寫
#          username: xxx
#          password: xxx
      #配置倉庫的分支
      label: master

#Eureka配置 
eureka:
  client:
    serviceUrl:
      #服務註冊中心地址,需按照註冊中心IP進行對應修改
      defaultZone: http://register-center:8761/eureka/
  instance:
    prefer-ip-address: true

配置說明:

  • spring.cloud.config.server.git.uri  配置git倉庫地址
  • spring.cloud.config.server.git.search-paths 配置倉庫路徑
  • spring.cloud.config.server.git.basedir 配置檔案本地臨時儲存目錄。一般無需配置。
  • spring.cloud.config.server.git.username 訪問git倉庫的使用者名稱.
  • spring.cloud.config.server.git.password 訪問git倉庫的密碼
  • spring.cloud.config.label 配置倉庫的分支

     如果Git倉庫為公開倉庫,可以不填寫使用者名稱和密碼,如果是私有倉庫需要填寫。本例中使用的公開倉庫,故未配置使用者名稱和密碼,git倉庫地址為https://github.com/xcbeyond/springCloudLearning.git,在目錄config-repo下用來存放各個服務的配置檔案進行集中管理,其中有一個config-client.yml配置檔案,內容如下:

server:
  port: 8889

eureka:
  client:
    serviceUrl:
      #服務註冊中心地址,需按照註冊中心IP進行對應修改
      defaultZone: http://register-center:8761/eureka/
      
test: this is a test message!

     啟動配置中心springCloudConfig-git服務,訪問http://localhost:8888/config-client/test,則會返回如下結果,即:包含git上config-client.yml配置檔案的內容。

{"name":"config-client","profiles":["test"],"label":null,"version":"46cfa51b88a1f5b08ee69bc67af187b1dc054ddf","state":null,"propertySources":[{"name":"https://github.com/xcbeyond/springCloudLearning.git/config-repo/config-client.yml","source":{"server.port":8889,"eureka.client.serviceUrl.defaultZone":"http://register-center:8761/eureka/","test":"this is a test message!"}}]}

由此證明從配置中心獲取配置資訊成功,說明配置中心搭建成功。

http請求地址和資原始檔對映如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

3、配置中心客戶端ConfigClient

      建立另外一個springboot專案springCloudConfigClient,作為配置中心的客戶端,即從通過配置中心獲取集中配置資訊的服務。客戶端依賴spring-cloud-starter-netflix-eureka-client包,pom.xml中額外新增如下依賴:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-config</artifactId>
	<version>2.0.0.RELEASE</version>
</dependency>

    其配置檔案為bootstrap.yml,注意該檔名是bootstrap,不再是application。應該bootstrap配置檔案優先於application配置檔案載入。bootstrap.ym中只配置配置中心相關資訊,其他配置統一集中存放在git上統一管理,以供該服務使用。

    bootstrap.yml配置檔案內容如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      #配置中心地址
      uri: http://config-center:8888/
      #遠端配置中心配置檔名
      name: config-client
      #遠端倉庫的分支
      label: master
  • spring.cloud.config.uri 配置中心地址,用於明確訪問的是哪個配置中心
  • spring.cloud.config.name 遠端配置中心配置檔名
  • spring.cloud.config.label 遠端倉庫的分支

寫一個測試介面/hi,返回配置檔案中test的值,用來測試從配置中心的配置檔案獲取配置。

package com.xcbeyond.springcloud.configclient.controller;

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

/**
 * 從服務配置中心獲取配置資訊
 * @author xcbeyond
 * 2018年8月7日下午4:11:00
 */
@RestController
public class GetConfigServerController {
	//通過服務配置中心獲取git上配置檔案裡的key
	@Value("${test}")
	private String test;
	
	@RequestMapping("/hi")
	public String hi() {
		return test;
	}
}

啟動springCloudConfigClient,通過訪問http://localhost:8889/hi,則會返回“this is a test message!”