1. 程式人生 > >Spring Cloud構建微服務架構 分散式配置中心(高可用與動態重新整理)【Dalston版】

Spring Cloud構建微服務架構 分散式配置中心(高可用與動態重新整理)【Dalston版】

高可用問題

傳統作法

通常在生產環境,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註冊到上面配置的服務註冊中心上去。
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
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=didispace
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服務,利用其來載入應用配置
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

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

}
  • 沿用之前我們建立的Controller來載入Git中的配置資訊
@RefreshScope
@RestController
public 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”。

配置重新整理

有時候,我們需要對配置內容做一些實時更新的場景,那麼Spring Cloud Config是否可以實現呢?答案顯然是可以的。下面,我們看看如何進行改造來實現配置內容的實時更新。

在改造程式之前,我們先將config-server和config-client都啟動起來,並訪問客戶端提供的REST APIhttp://localhost:7002/from來獲取配置資訊,可以獲得返回內容為:git-dev-1.0。接著,我們可以嘗試使用Git工具修改當前配置的內容,比如,將config-repo/didispace-dev.properties中的from的值從from=git-dev-1.0修改為from=git-dev-2.0,再訪問http://localhost:7002/from,可以看到其返回內容還是git-dev-1.0

下面,我們將在config-client端增加一些內容和操作以實現配置的重新整理:

  • 在config-clinet的pom.xml中新增spring-boot-starter-actuator監控模組,其中包含了/refresh重新整理API。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 重新啟動config-clinet,訪問一次http://localhost:7002/from,可以看到當前的配置值
  • 修改Git倉庫config-repo/didispace-dev.properties檔案中from的值
  • 再次訪問一次http://localhost:7002/from,可以看到配置值沒有改變
  • 通過POST請求傳送到http://localhost:7002/refresh,我們可以看到返回內容如下,代表from引數的配置內容被更新了
[
  "from"
]
  • 再次訪問一次http://localhost:7002/from,可以看到配置值已經是更新後的值了

通過上面的介紹,大家不難想到,該功能還可以同Git倉庫的Web Hook功能進行關聯,當有Git提交變化時,就給對應的配置主機發送/refresh請求來實現配置資訊的實時更新。但是,當我們的系統發展壯大之後,維護這樣的重新整理清單也將成為一個非常大的負擔,而且很容易犯錯,那麼有什麼辦法可以解決這個複雜度呢?後續我們將繼續介紹如何通過Spring Cloud Bus來實現以訊息匯流排的方式進行通知配置資訊的變化,完成叢集上的自動化更新。

相關推薦

Spring Cloud構建服務架構 分散式配置中心可用動態重新整理Dalston

高可用問題 傳統作法 通常在生產環境,Config Server與服務註冊中心一樣,我們也需要將其擴充套件為高可用的叢集。在之前實現的config-server基礎上來實現高可用非常簡單,不需要我們為這些服務端做任何額外的配置,只需要遵守一個配置規則:將所有的Config Server都指向同一

Spring Cloud構建服務架構 分散式配置中心加密解密

在微服務架構中,我們通常都會採用DevOps的組織方式來降低因團隊間溝通造成的巨大成本,以加速微服務應用的交付能力。這就使得原本由運維團隊控制的線上資訊將交由微服務所屬組織的成員自行維護,其中將會包括大量的敏感資訊,比如:資料庫的賬戶與密碼等。很顯然,如果我們直接將敏感資訊以明文的方式儲存於微服務應用的

Spring Cloud構建服務架構分散式配置中心

先來回顧一下,在前文中我們完成了什麼: 構建了config-server,連線到Git倉庫 在Git上建立了一個config-repo目錄,用來儲存配置資訊 構建了config-client,來獲取Git中的配置資訊 在本文中,我們繼續來看看Spring Cloud

Spring Cloud構建服務架構分散式配置中心

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <versio

Spring Cloud構建服務架構分布式配置中心

post ast github 構造 clas mas files cli .class 在本文中,我們將學習如何構建一個基於Git存儲的分布式配置中心,並對客戶端進行改造,並讓其能夠從配置中心獲取配置信息並綁定到代碼中的整個過程。 準備配置倉庫 準備一個git倉庫,可

Spring Cloud構建服務架構分散式服務跟蹤收集原理Dalston

                在本節內容之前,我們已經對如何引入Sleuth跟蹤資訊和搭建Zipkin服務端分析跟蹤延遲的過程做了詳細的介紹,相信大家對於Sleuth和Zipkin已經有了一定的感性認識。接下來,我們介紹一下關於Zipkin收集跟蹤資訊的過程細節,以幫助我們更好地理解Sleuth生產跟蹤資訊

Spring Cloud構建服務架構 分布式配置中心加密解密

pac 控制臺 數字簽名 rsa 運維 space strong secret 分布 在微服務架構中,我們通常都會采用DevOps的組織方式來降低因團隊間溝通造成的巨大成本,以加速微服務應用的交付能力。這就使得原本由運維團隊控制的線上信息將交由微服務所屬組織的成員自行維護

Spring Cloud構建服務架構分散式服務跟蹤跟蹤原理

通過上一篇《分散式服務跟蹤(入門)》的例子,我們已經通過Spring Cloud Sleuth往微服務應用中添加了實現分散式跟蹤具備的基本要素。下面通過本文來詳細說說實現分散式服務跟蹤的一些要點。分散式系統中的服務跟蹤在理論上並不複雜,它主要包括下面兩個關鍵點:為了實現請求跟

Spring-Boot:Spring Cloud構建服務架構

xmlns art 超時 客戶 微服務架構 cover lns created 搭建 概述:   從上一篇博客《Spring-boot:5分鐘整合Dubbo構建分布式服務》 過度到Spring Cloud,我們將開始學習如何使用Spring Cloud 來搭建微服務。繼續采

Spring Cloud構建服務架構—創建“服務註冊中心

springboot springcloud mybatis eureka config 創建一個基礎的Spring Boot工程,命名為eureka-server,並在pom.xml中引入需要的依賴內容: <parent> <groupId>org.springf

Spring Cloud構建服務架構服務註冊發現

springboot springcloud mybatis eureka config Spring Cloud簡介Spring Cloud是一個基於Spring Boot實現的雲應用開發工具,它為基於JVM的雲應用開發中涉及的配置管理、服務發現、斷路器、智能路由、微代理、控制總線、全局

Spring Cloud構建服務架構-創建“服務提供方”

spring Spring Cloud Spring Boot config 下面我們創建提供服務的客戶端,並向服務註冊中心註冊自己。本文我們主要介紹服務的註冊與發現,所以我們不妨在服務提供方中嘗試著提供一個接口來獲取當前所有的服務信息。 首先,創建一個基本的Spring Boot應用。命名為

Spring Cloud構建服務架構—服務網關過濾器

Spring Cloud Spring Boot mybatis 過濾器作用 我們的微服務應用提供的接口就可以通過統一的API網關入口被客戶端訪問到了。但是,每個客戶端用戶請求微服務應用提供的接口時,它們的訪問權限往往都需要有一定的限制,系統並不會將所有的微服務接口都對它們開放。然而,目前的服務路

Spring Cloud構建服務架構Hystrix監控面板

Spring Cloud Spring Boot mybatis 在Spring Cloud中構建一個Hystrix Dashboard非常簡單,只需要下面四步: 創建一個標準的Spring Boot工程,命名為:hystrix-dashboard。編輯pom.xml,具體依賴內容如下: <

Spring Cloud構建服務架構服務消費Ribbon

ble DG 沒有 客戶 BE pla cati str 主類 Spring Cloud RibbonSpring Cloud Ribbon是基於Netflix Ribbon實現的一套客戶端負載均衡的工具。它是一個基於HTTP和TCP的客戶端負載均衡器。它可以通過在客戶端中

Spring Cloud構建服務架構服務消費基礎

消費 ring str frame emp default class a template pom.xml 使用LoadBalancerClient在Spring Cloud Commons中提供了大量的與服務治理相關的抽象接口,包括DiscoveryClient、這裏我

Spring Cloud構建服務架構—Hystrix斷路器

能夠 電路 處理 觸發 就會 熔斷器 邏輯 響應 保險絲 斷路器模式源於Martin Fowler的Circuit Breaker一文。“斷路器”本身是一種開關裝置,用於在電路上保護線路過載,當線路中有電器發生短路時,“斷路器”能夠及時的切斷故障電路,防止發生過載、發熱、甚

Spring Cloud構建服務架構-Hystrix服務降級

static 原因 架構 一個個 policy 消費者 兩個 comm 以及 在微服務架構中,我們將系統拆分成了一個個的服務單元,各單元應用間通過服務註冊與訂閱的方式互相依賴。由於每個單元都在不同的進程中運行,依賴通過遠程調用的方式執行,這樣就有可能因為網絡原因或是依賴服務

Spring Cloud構建服務架構服務註冊發現 Eureka

Spring Cloud構建微服務架構:服務註冊與發現Eureka 【Dalston版】 原創   2018-04-10  宗野   Spring Cloud 已經

Spring Cloud構建服務架構服務容錯保護Hystrix服務降級

tro sco load 服務架構 延遲 正常 map ati href 動手試一試 在開始使用Spring Cloud Hystrix實現斷路器之前,我們先拿之前實現的一些內容作為基礎,其中包括: eureka-server工程:服務註冊中心,端口:1001 eurek