Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)注冊(cè)與發(fā)現(xiàn)(Eureka、Consul)
已經(jīng)有非常長(zhǎng)的時(shí)間沒(méi)有更新《Spring Cloud構(gòu)建微服務(wù)架構(gòu)》系列文章了,自從開始寫Spring Cloud的專題內(nèi)容開始就獲得了不少的閱讀量和認(rèn)可,當(dāng)然也有一些批評(píng),其中也不乏一些很中肯的意見和深度的問(wèn)題,對(duì)我來(lái)說(shuō)也是進(jìn)一步提高的契機(jī),在此感謝所有關(guān)注我博客的讀者們。
由于之前主要精力都花在的編寫《Spring Cloud微服務(wù)實(shí)戰(zhàn)》一書上,所以該系列文章就沒(méi)有得到持續(xù)的維護(hù)和更新。由于漫長(zhǎng)的寫書過(guò)程和繁瑣的出版流程,在本書一面世的時(shí)候,在版本上已經(jīng)落后于當(dāng)前的最新版本。雖然在書中前前后后加入了一些版本更新的注意事項(xiàng),但是認(rèn)識(shí)過(guò)程不是一蹴而就的,總是隨著實(shí)踐的深入慢慢發(fā)現(xiàn)的。所以,決定重寫一下該系列文章,一方面將Spring Cloud的版本更新到Dalston,另一方面重新組織內(nèi)容并增加一些之前沒(méi)有寫過(guò)的重要組件。希望通過(guò)這個(gè)系列,來(lái)幫助準(zhǔn)備使用Spring Cloud的朋友們快速入門。同時(shí),也是作為《Spring Cloud微服務(wù)實(shí)戰(zhàn)》一書對(duì)最新版本做一些不同內(nèi)容的補(bǔ)充。
Spring Cloud簡(jiǎn)介
Spring Cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的云應(yīng)用開發(fā)工具,它為基于JVM的云應(yīng)用開發(fā)中涉及的配置管理、服務(wù)發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線、全局鎖、決策競(jìng)選、分布式會(huì)話和集群狀態(tài)管理等操作提供了一種簡(jiǎn)單的開發(fā)方式。
Spring Cloud包含了多個(gè)子項(xiàng)目(針對(duì)分布式系統(tǒng)中涉及的多個(gè)不同開源產(chǎn)品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等項(xiàng)目。
微服務(wù)架構(gòu)
“微服務(wù)架構(gòu)”在這幾年非常的火熱,以至于關(guān)于微服務(wù)架構(gòu)相關(guān)的開源產(chǎn)品被反復(fù)的提及(比如:netflix、dubbo),Spring Cloud也因Spring社區(qū)的強(qiáng)大知名度和影響力也被廣大架構(gòu)師與開發(fā)者備受關(guān)注。
那么什么是“微服務(wù)架構(gòu)”呢?簡(jiǎn)單的說(shuō),微服務(wù)架構(gòu)就是將一個(gè)完整的應(yīng)用從數(shù)據(jù)存儲(chǔ)開始垂直拆分成多個(gè)不同的服務(wù),每個(gè)服務(wù)都能獨(dú)立部署、獨(dú)立維護(hù)、獨(dú)立擴(kuò)展,服務(wù)與服務(wù)間通過(guò)諸如RESTful API的方式互相調(diào)用。
對(duì)于“微服務(wù)架構(gòu)”,大家在互聯(lián)網(wǎng)可以搜索到很多相關(guān)的介紹和研究文章來(lái)進(jìn)行學(xué)習(xí)和了解。也可以閱讀始祖Martin Fowler的《Microservices》(中文版翻譯點(diǎn)擊查看),本文不做更多的介紹和描述。
服務(wù)治理
在簡(jiǎn)單介紹了Spring Cloud和微服務(wù)架構(gòu)之后,下面回歸本文的主旨內(nèi)容,如何使用Spring Cloud來(lái)實(shí)現(xiàn)服務(wù)治理。
由于Spring Cloud為服務(wù)治理做了一層抽象接口,所以在Spring Cloud應(yīng)用中可以支持多種不同的服務(wù)治理框架,比如:Netflix Eureka、Consul、Zookeeper。在Spring Cloud服務(wù)治理抽象層的作用下,我們可以無(wú)縫地切換服務(wù)治理實(shí)現(xiàn),并且不影響任何其他的服務(wù)注冊(cè)、服務(wù)發(fā)現(xiàn)、服務(wù)調(diào)用等邏輯。
所以,下面我們通過(guò)介紹兩種服務(wù)治理的實(shí)現(xiàn)來(lái)體會(huì)Spring Cloud這一層抽象所帶來(lái)的好處。
Spring Cloud Eureka
首先,我們來(lái)嘗試使用Spring Cloud Eureka來(lái)實(shí)現(xiàn)服務(wù)治理。
Spring Cloud Eureka是Spring Cloud Netflix項(xiàng)目下的服務(wù)治理模塊。而Spring Cloud Netflix項(xiàng)目是Spring Cloud的子項(xiàng)目之一,主要內(nèi)容是對(duì)Netflix公司一系列開源產(chǎn)品的包裝,它為Spring Boot應(yīng)用提供了自配置的Netflix OSS整合。通過(guò)一些簡(jiǎn)單的注解,開發(fā)者就可以快速的在應(yīng)用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)。它主要提供的模塊包括:服務(wù)發(fā)現(xiàn)(Eureka),斷路器(Hystrix),智能路由(Zuul),客戶端負(fù)載均衡(Ribbon)等。
下面,就來(lái)具體看看如何使用Spring Cloud Eureka實(shí)現(xiàn)服務(wù)治理。
創(chuàng)建“服務(wù)注冊(cè)中心”
創(chuàng)建一個(gè)基礎(chǔ)的Spring Boot工程,命名為eureka-server,并在pom.xml中引入需要的依賴內(nèi)容:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.4.RELEASE</version>
- <relativePath/>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Dalston.SR1</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
通過(guò)@EnableEurekaServer注解啟動(dòng)一個(gè)服務(wù)注冊(cè)中心提供給其他應(yīng)用進(jìn)行對(duì)話。這一步非常的簡(jiǎn)單,只需要在一個(gè)普通的Spring Boot應(yīng)用中添加這個(gè)注解就能開啟此功能,比如下面的例子:
- @EnableEurekaServer
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- new SpringApplicationBuilder(Application.class)
- .web(true).run(args);
- }
- }
在默認(rèn)設(shè)置下,該服務(wù)注冊(cè)中心也會(huì)將自己作為客戶端來(lái)嘗試注冊(cè)它自己,所以我們需要禁用它的客戶端注冊(cè)行為,只需要在application.properties配置文件中增加如下信息:
- spring.application.name=eureka-server
- server.port=1001
- eureka.instance.hostname=localhost
- eureka.client.register-with-eureka=false
- eureka.client.fetch-registry=false
為了與后續(xù)要進(jìn)行注冊(cè)的服務(wù)區(qū)分,這里將服務(wù)注冊(cè)中心的端口通過(guò)server.port屬性設(shè)置為1001。啟動(dòng)工程后,訪問(wèn):http://localhost:1111/,可以看到下面的頁(yè)面,其中還沒(méi)有發(fā)現(xiàn)任何服務(wù)。
創(chuàng)建“服務(wù)提供方”
下面我們創(chuàng)建提供服務(wù)的客戶端,并向服務(wù)注冊(cè)中心注冊(cè)自己。本文我們主要介紹服務(wù)的注冊(cè)與發(fā)現(xiàn),所以我們不妨在服務(wù)提供方中嘗試著提供一個(gè)接口來(lái)獲取當(dāng)前所有的服務(wù)信息。
首先,創(chuàng)建一個(gè)基本的Spring Boot應(yīng)用。命名為eureka-client,在pom.xml中,加入如下配置:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.4.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Dalston.SR1</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
其次,實(shí)現(xiàn)/dc請(qǐng)求處理接口,通過(guò)DiscoveryClient對(duì)象,在日志中打印出服務(wù)實(shí)例的相關(guān)內(nèi)容。
- @RestController
- public class DcController {
- @Autowired
- DiscoveryClient discoveryClient;
- @GetMapping("/dc")
- public String dc() {
- String services = "Services: " + discoveryClient.getServices();
- System.out.println(services);
- return services;
- }
- }
最后在應(yīng)用主類中通過(guò)加上@EnableDiscoveryClient注解,該注解能激活Eureka中的DiscoveryClient實(shí)現(xiàn),這樣才能實(shí)現(xiàn)Controller中對(duì)服務(wù)信息的輸出。
- @EnableDiscoveryClient
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- new SpringApplicationBuilder(
- ComputeServiceApplication.class)
- .web(true).run(args);
- }
- }
我們?cè)谕瓿闪朔?wù)內(nèi)容的實(shí)現(xiàn)之后,再繼續(xù)對(duì)application.properties做一些配置工作,具體如下:
- spring.application.name=eureka-client
- server.port=2001
- eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
通過(guò)spring.application.name屬性,我們可以指定微服務(wù)的名稱后續(xù)在調(diào)用的時(shí)候只需要使用該名稱就可以進(jìn)行服務(wù)的訪問(wèn)。eureka.client.serviceUrl.defaultZone屬性對(duì)應(yīng)服務(wù)注冊(cè)中心的配置內(nèi)容,指定服務(wù)注冊(cè)中心的位置。為了在本機(jī)上測(cè)試區(qū)分服務(wù)提供方和服務(wù)注冊(cè)中心,使用server.port屬性設(shè)置不同的端口。
啟動(dòng)該工程后,再次訪問(wèn):http://localhost:1001/??梢匀缦聢D內(nèi)容,我們定義的服務(wù)被成功注冊(cè)了。
當(dāng)然,我們也可以通過(guò)直接訪問(wèn)eureka-client服務(wù)提供的/dc接口來(lái)獲取當(dāng)前的服務(wù)清單,只需要訪問(wèn):http://localhost:2001/dc,我們可以得到如下輸出返回:
- Services: [eureka-client]
其中,方括號(hào)中的eureka-client就是通過(guò)Spring Cloud定義的DiscoveryClient接口在eureka的實(shí)現(xiàn)中獲取到的所有服務(wù)清單。由于Spring Cloud在服務(wù)發(fā)現(xiàn)這一層做了非常好的抽象,所以,對(duì)于上面的程序,我們可以無(wú)縫的從eureka的服務(wù)治理體系切換到consul的服務(wù)治理體系中區(qū)。
Spring Cloud Consul
Spring Cloud Consul項(xiàng)目是針對(duì)Consul的服務(wù)治理實(shí)現(xiàn)。Consul是一個(gè)分布式高可用的系統(tǒng),它包含多個(gè)組件,但是作為一個(gè)整體,在微服務(wù)架構(gòu)中為我們的基礎(chǔ)設(shè)施提供服務(wù)發(fā)現(xiàn)和服務(wù)配置的工具。它包含了下面幾個(gè)特性:
- 服務(wù)發(fā)現(xiàn)
- 健康檢查
- Key/Value存儲(chǔ)
- 多數(shù)據(jù)中心
由于Spring Cloud Consul項(xiàng)目的實(shí)現(xiàn),我們可以輕松的將基于Spring Boot的微服務(wù)應(yīng)用注冊(cè)到Consul上,并通過(guò)此實(shí)現(xiàn)微服務(wù)架構(gòu)中的服務(wù)治理。
以之前實(shí)現(xiàn)的基于Eureka的示例(eureka-client)為基礎(chǔ),我們?nèi)绾螌⒅皩?shí)現(xiàn)的服務(wù)提供者注冊(cè)到Consul上呢?方法非常簡(jiǎn)單,我們只需要在pom.xml中將eureka的依賴修改為如下依賴:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-consul-discovery</artifactId>
- </dependency>
接下來(lái)再修改一下application.properites,將consul需要的配置信息加入即可,比如:(下面配置是默認(rèn)值)
- spring.cloud.consul.host=localhost
- spring.cloud.consul.port=8500
到此為止,我們將eureka-client轉(zhuǎn)換為基于consul服務(wù)治理的服務(wù)提供者就完成了。前文我們已經(jīng)有提到過(guò)服務(wù)發(fā)現(xiàn)的接口DiscoveryClient是Spring Cloud對(duì)服務(wù)治理做的一層抽象,所以可以屏蔽Eureka和Consul服務(wù)治理的實(shí)現(xiàn)細(xì)節(jié),我們的程序不需要做任何改變,只需要引入不同的服務(wù)治理依賴,并配置相關(guān)的配置屬性就能輕松的將微服務(wù)納入Spring Cloud的各個(gè)服務(wù)治理框架中。
下面可以嘗試讓consul的服務(wù)提供者運(yùn)行起來(lái)。這里可能讀者會(huì)問(wèn),不需要?jiǎng)?chuàng)建類似eureka-server的服務(wù)端嗎?由于Consul自身提供了服務(wù)端,所以我們不需要像之前實(shí)現(xiàn)Eureka的時(shí)候創(chuàng)建服務(wù)注冊(cè)中心,直接通過(guò)下載consul的服務(wù)端程序就可以使用。
我們可以用下面的命令啟動(dòng)consul的開發(fā)模式:
- $consul agent -dev
- ==> Starting Consul agent...
- ==> Starting Consul agent RPC...
- ==> Consul agent running!
- Version: 'v0.7.2'
- Node name: 'Lenovo-zhaiyc'
- Datacenter: 'dc1'
- Server: true (bootstrap: false)
- Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
- Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
- Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
- Atlas: <disabled>
- ==> Log data will now stream in as it occurs:
- 2017/06/22 07:50:54 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]
- 2017/06/22 07:50:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
- 2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc 127.0.0.1
- 2017/06/22 07:50:54 [INFO] consul: Adding LAN server Lenovo-zhaiyc (Addr: tcp/127.0.0.1:8300) (DC: dc1)
- 2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc.dc1 127.0.0.1
- 2017/06/22 07:50:54 [INFO] consul: Adding WAN server Lenovo-zhaiyc.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)
- 2017/06/22 07:51:01 [ERR] agent: failed to sync remote state: No cluster leader
- 2017/06/22 07:51:02 [WARN] raft: Heartbeat timeout from "" reached, starting election
- 2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
- 2017/06/22 07:51:02 [DEBUG] raft: Votes needed: 1
- 2017/06/22 07:51:02 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1
- 2017/06/22 07:51:02 [INFO] raft: Election won. Tally: 1
- 2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
- 2017/06/22 07:51:02 [INFO] consul: cluster leadership acquired
- 2017/06/22 07:51:02 [INFO] consul: New leader elected: Lenovo-zhaiyc
- 2017/06/22 07:51:02 [DEBUG] consul: reset tombstone GC to index 3
- 2017/06/22 07:51:02 [INFO] consul: member 'Lenovo-zhaiyc' joined, marking health alive
- 2017/06/22 07:51:02 [INFO] agent: Synced service 'consul'
- 2017/06/22 07:51:02 [DEBUG] agent: Node info in sync
consul服務(wù)端啟動(dòng)完成之后,我們?cè)賹⒅案脑旌蟮腸onsul服務(wù)提供者啟動(dòng)起來(lái)。consul與eureka一樣,都提供了簡(jiǎn)單的ui界面來(lái)查看服務(wù)的注冊(cè)情況:
更多關(guān)于Consul的使用指南,讀者可查看官方文檔:https://www.consul.io/
代碼示例
樣例工程將沿用之前在碼云和GitHub上創(chuàng)建的SpringCloud-Learning項(xiàng)目,重新做了一下整理。通過(guò)不同目錄來(lái)區(qū)分Brixton和Dalston的示例。
- 碼云:點(diǎn)擊查看
- GitHub:點(diǎn)擊查看
具體工程說(shuō)明如下:
- eureka的服務(wù)注冊(cè)中心:eureka-server
- eureka的服務(wù)提供方:eureka-client
- consul的服務(wù)提供方:consul-client
【本文為51CTO專欄作者“翟永超”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)通過(guò)51CTO聯(lián)系作者獲取授權(quán)】