自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)注冊(cè)與發(fā)現(xiàn)(Eureka、Consul)

企業(yè)動(dòng)態(tài)
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ā)方式。

已經(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)容:

  1. <parent> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-parent</artifactId> 
  4.     <version>1.5.4.RELEASE</version> 
  5.     <relativePath/> 
  6. </parent> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>org.springframework.cloud</groupId> 
  10.         <artifactId>spring-cloud-starter-eureka-server</artifactId> 
  11.     </dependency> 
  12. </dependencies> 
  13. <dependencyManagement> 
  14.     <dependencies> 
  15.         <dependency> 
  16.            <groupId>org.springframework.cloud</groupId> 
  17.            <artifactId>spring-cloud-dependencies</artifactId> 
  18.            <version>Dalston.SR1</version> 
  19.            <type>pom</type> 
  20.            <scope>import</scope> 
  21.         </dependency> 
  22.     </dependencies> 
  23. </dependencyManagement> 

通過(guò)@EnableEurekaServer注解啟動(dòng)一個(gè)服務(wù)注冊(cè)中心提供給其他應(yīng)用進(jìn)行對(duì)話。這一步非常的簡(jiǎn)單,只需要在一個(gè)普通的Spring Boot應(yīng)用中添加這個(gè)注解就能開啟此功能,比如下面的例子:

  1. @EnableEurekaServer 
  2. @SpringBootApplication 
  3. public class Application { 
  4.     public static void main(String[] args) { 
  5.         new SpringApplicationBuilder(Application.class) 
  6.                     .web(true).run(args); 
  7.     } 

在默認(rèn)設(shè)置下,該服務(wù)注冊(cè)中心也會(huì)將自己作為客戶端來(lái)嘗試注冊(cè)它自己,所以我們需要禁用它的客戶端注冊(cè)行為,只需要在application.properties配置文件中增加如下信息:

  1. spring.application.name=eureka-server 
  2. server.port=1001 
  3. eureka.instance.hostname=localhost 
  4. eureka.client.register-with-eureka=false 
  5. 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中,加入如下配置:

  1. <parent>  
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-parent</artifactId> 
  4.     <version>1.5.4.RELEASE</version> 
  5.     <relativePath/> <!-- lookup parent from repository --> 
  6. </parent> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>org.springframework.cloud</groupId> 
  10.         <artifactId>spring-cloud-starter-eureka</artifactId> 
  11.     </dependency> 
  12.     <dependency> 
  13.         <groupId>org.springframework.boot</groupId> 
  14.         <artifactId>spring-boot-starter-web</artifactId> 
  15.     </dependency> 
  16. </dependencies> 
  17. <dependencyManagement> 
  18.     <dependencies> 
  19.         <dependency> 
  20.            <groupId>org.springframework.cloud</groupId> 
  21.            <artifactId>spring-cloud-dependencies</artifactId> 
  22.            <version>Dalston.SR1</version> 
  23.            <type>pom</type> 
  24.            <scope>import</scope> 
  25.         </dependency> 
  26.     </dependencies> 
  27. </dependencyManagement> 

其次,實(shí)現(xiàn)/dc請(qǐng)求處理接口,通過(guò)DiscoveryClient對(duì)象,在日志中打印出服務(wù)實(shí)例的相關(guān)內(nèi)容。

  1. @RestController 
  2. public class DcController { 
  3.     @Autowired 
  4.     DiscoveryClient discoveryClient; 
  5.     @GetMapping("/dc"
  6.     public String dc() { 
  7.         String services = "Services: " + discoveryClient.getServices(); 
  8.         System.out.println(services); 
  9.         return services; 
  10.     } 

最后在應(yīng)用主類中通過(guò)加上@EnableDiscoveryClient注解,該注解能激活Eureka中的DiscoveryClient實(shí)現(xiàn),這樣才能實(shí)現(xiàn)Controller中對(duì)服務(wù)信息的輸出。

  1. @EnableDiscoveryClient 
  2. @SpringBootApplication 
  3. public class Application { 
  4.     public static void main(String[] args) { 
  5.         new SpringApplicationBuilder( 
  6.             ComputeServiceApplication.class) 
  7.             .web(true).run(args); 
  8.     } 

我們?cè)谕瓿闪朔?wù)內(nèi)容的實(shí)現(xiàn)之后,再繼續(xù)對(duì)application.properties做一些配置工作,具體如下:

  1. spring.application.name=eureka-client 
  2. server.port=2001 
  3. 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,我們可以得到如下輸出返回:

  1. 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的依賴修改為如下依賴:

  1. <dependency> 
  2.   <groupId>org.springframework.cloud</groupId> 
  3.   <artifactId>spring-cloud-starter-consul-discovery</artifactId> 
  4. </dependency> 

接下來(lái)再修改一下application.properites,將consul需要的配置信息加入即可,比如:(下面配置是默認(rèn)值)

  1. spring.cloud.consul.host=localhost 
  2. 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ā)模式:

  1. $consul agent -dev 
  2. ==> Starting Consul agent... 
  3. ==> Starting Consul agent RPC... 
  4. ==> Consul agent running! 
  5.            Version: 'v0.7.2' 
  6.          Node name'Lenovo-zhaiyc' 
  7.         Datacenter: 'dc1' 
  8.             Server: true (bootstrap: false
  9.        Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400) 
  10.       Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302) 
  11.     Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false 
  12.              Atlas: <disabled> 
  13. ==> Log data will now stream in as it occurs: 
  14.     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}] 
  15.     2017/06/22 07:50:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: ""
  16.     2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc 127.0.0.1 
  17.     2017/06/22 07:50:54 [INFO] consul: Adding LAN server Lenovo-zhaiyc (Addr: tcp/127.0.0.1:8300) (DC: dc1) 
  18.     2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc.dc1 127.0.0.1 
  19.     2017/06/22 07:50:54 [INFO] consul: Adding WAN server Lenovo-zhaiyc.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1) 
  20.     2017/06/22 07:51:01 [ERR] agent: failed to sync remote state: No cluster leader 
  21.     2017/06/22 07:51:02 [WARN] raft: Heartbeat timeout from "" reached, starting election 
  22.     2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2 
  23.     2017/06/22 07:51:02 [DEBUG] raft: Votes needed: 1 
  24.     2017/06/22 07:51:02 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1 
  25.     2017/06/22 07:51:02 [INFO] raft: Election won. Tally: 1 
  26.     2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state 
  27.     2017/06/22 07:51:02 [INFO] consul: cluster leadership acquired 
  28.     2017/06/22 07:51:02 [INFO] consul: New leader elected: Lenovo-zhaiyc 
  29.     2017/06/22 07:51:02 [DEBUG] consul: reset tombstone GC to index 3 
  30.     2017/06/22 07:51:02 [INFO] consul: member 'Lenovo-zhaiyc' joined, marking health alive 
  31.     2017/06/22 07:51:02 [INFO] agent: Synced service 'consul' 
  32.     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的示例。

具體工程說(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)】

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來(lái)源: 51CTO專欄
相關(guān)推薦

2022-01-16 23:10:40

語(yǔ)言服務(wù)注冊(cè)

2023-06-02 08:33:43

微服務(wù)架構(gòu)服務(wù)注冊(cè)

2025-03-31 08:35:00

Eureka微服務(wù)架構(gòu)

2017-06-26 09:06:10

Spring Clou微服務(wù)架構(gòu)

2020-06-29 07:58:18

ZooKeeperConsul 注冊(cè)中心

2017-09-04 16:15:44

服務(wù)網(wǎng)關(guān)架構(gòu)

2021-04-20 17:20:59

SpringColud EurekaNetflix開發(fā)

2017-07-03 09:50:07

Spring Clou微服務(wù)架構(gòu)

2017-08-10 11:15:05

Spring Clou微服務(wù)架構(gòu)

2017-08-09 15:50:47

Spring Clou微服務(wù)架構(gòu)

2024-07-10 10:51:39

SpringEureka數(shù)據(jù)中心

2022-02-07 07:10:32

服務(wù)注冊(cè)功能

2019-08-23 10:34:05

微服務(wù)Eureka架構(gòu)

2017-07-03 08:29:42

Spring Clou服務(wù)詳解

2017-07-04 17:35:46

微服務(wù)架構(gòu)Spring Clou

2018-03-02 16:11:29

Spring Clou分布式服務(wù)跟蹤

2025-01-09 08:32:50

2017-12-20 15:37:39

Spring Clou微服務(wù)架構(gòu)

2018-07-09 09:27:10

Spring Clou微服務(wù)架構(gòu)

2018-04-09 13:56:13

微服務(wù)架構(gòu)分布式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)