Spring Cloud微服架構之分佈式配置中心(續)

本文接之前的《Spring Cloud微服架構之分佈式配置中心》,繼續來說說Spring Cloud Config的使用。

先來回顧一下,在前文中我們完成了什麼:

  • 構建了config-server,連接到Git倉庫
  • 在Git上創建了一個config-repo目錄,用來存儲配置信息
  • 構建了config-client,來獲取Git中的配置信息

在本文中,我們繼續來看看Spring Cloud Config的一些其他能力。

高可用問題

傳統作法

通常在生產環境,Config Server與服務註冊中心一樣,我們也需要將其擴展爲高可用的集羣。在之前實現的config-server基礎上來實現高可用非常簡單,不需要我們爲這些服務端做任何額外的配置,只需要遵守一個配置規則:將所有的Config Server都指向同一個Git倉庫,這樣所有的配置內容就通過統一的共享文件系統來維護,而客戶端在指定Config Server位置時,只要配置Config Server外的均衡負載即可,就像如下圖所示的結構:


註冊爲服務

雖然通過服務端負載均衡已經能夠實現,但是作爲架構內的配置管理,本身其實也是可以看作架構中的一個微服務。所以,另外一種方式更爲簡單的方法就是把config-server也註冊爲服務,這樣所有客戶端就能以服務的方式進行訪問。通過這種方法,只需要啓動多個指向同一Git倉庫位置的config-server就能實現高可用了。

配置過程也非常簡單,具體如下:

config-server配置

  • pom.xml的dependencies節點中引入如下依賴,相比之前的config-server就,加入了spring-cloud-starter-eureka,用來註冊服務
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
</dependencies>

  • application.properties中配置參數eureka.client.serviceUrl.defaultZone以指定服務註冊中心的位置,詳細內容如下:
spring.application.name=config-server
server.port=7001
# 配置服務註冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
# git倉庫配置
spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringCloud-Learning/
spring.cloud.config.server.git.searchPaths=Chapter1-1-8/config-repo
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
  • 在應用主類中,新增@EnableDiscoveryClient註解,用來將config-server註冊到上面配置的服務註冊中心上去。
package com.wys;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
public class Application {
	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}
}
  • 啓動該應用,並訪問http://localhost:1111/,可以在Eureka Server的信息面板中看到config-server已經被註冊了。


config-client配置

  • 同config-server一樣,在pom.xml的dependencies節點中新增spring-cloud-starter-eureka依賴,用來註冊服務:
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
</dependencies>

  • bootstrap.properties中,按如下配置:
spring.application.name=config
server.port=7002
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.profile=dev
其中,通過eureka.client.serviceUrl.defaultZone參數指定服務註冊中心,用於服務的註冊與發現,再將spring.cloud.config.discovery.enabled參數設置爲true,開啓通過服務來訪問Config Server的功能,最後利用spring.cloud.config.discovery.serviceId參數來指定Config Server註冊的服務名。這裏的spring.application.namespring.cloud.config.profile如之前通過URI的方式訪問時候一樣,用來定位Git中的資源。
  • 在應用主類中,增加@EnableDiscoveryClient註解,用來發現config-server服務,利用其來加載應用配置
package com.wys;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}
}

  • 沿用之前我們創建的Controller來加載Git中的配置信息
package com.wys.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
class TestController {
	@Value("${from}")
    private String from;
	
    @RequestMapping("/from")
    public String from() {
        return this.from;
    }
}
  • 完成了上述配置之後,我們啓動該客戶端應用。若啓動成功,訪問http://localhost:1111/,可以在Eureka Server的信息面板中看到該應用已經被註冊成功了。


  • 訪問客戶端應用提供的服務:http://localhost:7002/from,此時,我們會返回在Git倉庫中didispace-dev.properties文件配置的from屬性內容:”git-dev-1.0”。


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