SpringCloud的服務註冊與發現Eureka

Eurekejava

Eureka是Netflix開源的一款提供服務註冊和發現的產品,它提供了完整的Service Registry(註冊登記)和Service Discovery(發現)實現。也是springcloud體系中最重要最核心的組件之一。web

服務中心:spring

服務中心又稱註冊中心,管理各類服務功能包括服務的註冊、發現、熔斷、負載、降級等。apache

有了服務中心調用關係會有什麼變化,畫幾個簡圖來幫忙理解springboot

項目A調用項目B服務器

正常調用項目A請求項目B架構

 

 

有了服務中心以後,任何一個服務都不能直接去掉用,都須要經過服務中心來調用app

 

 

項目A調用項目B,項目B在調用項目C負載均衡

 

 

這時候調用的步驟就會爲兩步:第一步,項目A首先從服務中心請求項目B服務器,而後項目B在從服務中心請求項目C服務。maven

 

 

上面的項目只是兩三個相互之間的簡單調用,可是若是項目超過20個30個呢,畫一張圖來描述幾十個項目之間的相互調用關係全是線條,任何其中的一個項目改動,就會牽連好幾個項目跟着重啓,巨麻煩並且容易出錯。經過服務中心來獲取服務你不須要關注你調用的項目IP地址,由幾臺服務器組成,每次直接去服務中心獲取可使用的服務去調用既可。

因爲各類服務都註冊到了服務中心,就有了去作不少高級功能條件。好比幾臺服務提供相同服務來作均衡負載;監控服務器調用成功率來作熔斷,移除服務列表中的故障點;監控服務調用時間來對不一樣的服務器設置不一樣的權重等等。

Eureka由兩個組件組成:Eureka服務器和Eureka客戶端。Eureka服務器用做服務註冊服務器。Eureka客戶端是一個java客戶端,用來簡化與服務器的交互、做爲輪詢負載均衡器,並提供服務的故障切換支持。Netflix在其生產環境中使用的是另外的客戶端,它提供基於流量、資源利用率以及出錯狀態的加權負載均衡。

 

 

上圖簡要描述了Eureka的基本架構,由3個角色組成:

一、Eureka Server

  • 提供服務註冊和發現

二、Service Provider

  • 服務提供方
  • 將自身服務註冊到Eureka,從而使服務消費方可以找到

三、Service Consumer

  • 服務消費方
  • 從Eureka獲取註冊服務列表,從而可以消費服務

建立註冊中心 Eureka Server

建立一個簡單的maven springboot項目

pom裏面添加以下依賴:

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

經過@EnableEurekaServer註解啓動一個服務註冊中心提供給其餘應用進行對話。這一步很是的簡單,只須要在一個普通的Spring Boot應用中添加這個註解就能開啓此功能,好比下面的例子:

 
1 package com; 2 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 import org.springframework.boot.builder.SpringApplicationBuilder; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @EnableEurekaServer//服務註冊中心開啓註解 8 @SpringBootApplication 9 public class Application { 10 11 public static void main(String[] args) { 12 new SpringApplicationBuilder(Application.class).web(true).run(args); 13 } 14 15 }

在默認設置下,該服務註冊中心也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要禁用它的客戶端註冊行爲,只須要在application.properties中問增長以下配置:

 
1 #啓動端口 2 server.port=1111 3  4 #關閉掉本身往服務中心註冊的機制 5 eureka.client.register-with-eureka=false 6 #是否檢索服務 7 eureka.client.fetch-registry=false 8 #服務中心地址 9 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

項目結構:

 

 

啓動項目訪問 http://localhost:1111/

界面以下

 

 

 

建立服務提供者

下面咱們建立提供服務的客戶端,並向服務註冊中心註冊本身。

假設咱們有一個提供計算功能的微服務模塊,咱們實現一個RESTful API,經過傳入兩個參數a和b,最後返回a + b的結果。

首先,建立一個基本的Spring Boot應用,在pom.xml中,加入以下配置:

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

建立咱們的業務訪問控制器

 
1 package com; 2 3 import org.apache.log4j.Logger; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.cloud.client.ServiceInstance; 6 import org.springframework.cloud.client.discovery.DiscoveryClient; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.RequestParam; 10 import org.springframework.web.bind.annotation.RestController; 11 12 @RestController 13 public class ComputeController { 14 15 private final Logger logger = Logger.getLogger(getClass()); 16 17 @Autowired 18 private DiscoveryClient client; 19 20 @RequestMapping(value = "/add" ,method = RequestMethod.GET) 21 public Integer add(@RequestParam Integer a, @RequestParam Integer b) { 22 ServiceInstance instance = client.getLocalServiceInstance(); 23 Integer r = a + b; 24 logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); 25 return r; 26 } 27 28 }

建立啓動器

EnableDiscoveryClient註解,該註解能激活Eureka中的DiscoveryClient實現,才能實現Controller中對服務信息的輸出。

 
package com; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient//標識客戶端,並掃描控制器裏面的DiscoveryClient @SpringBootApplication public class ComputeServiceApplication { public static void main(String[] args) { new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args); } }

application.properties

 
1 #指定微服務的名稱後續在調用的時候只須要使用該名稱就能夠進行服務的訪問。 2 spring.application.name=compute-service 3 server.port=2222 4 #屬性對應服務註冊中心的配置內容,指定服務註冊中心的位置。 5 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

啓動本項目,而後再訪問http://localhost:1111/ 就能夠看到服務已經註冊到服務中心

建立服務的消費者

使用ribbon實現負載均衡的消費者,構建一個基本Spring Boot項目,並在pom.xml中加入以下內容:

 
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.3.5.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 8 <dependencies> 9 <dependency> 10 <groupId>org.springframework.cloud</groupId> 11 <artifactId>spring-cloud-starter-ribbon</artifactId> 12 </dependency> 13 <dependency> 14 <groupId>org.springframework.cloud</groupId> 15 <artifactId>spring-cloud-starter-eureka</artifactId> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-web</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-test</artifactId> 24 <scope>test</scope> 25 </dependency> 26 </dependencies> 27 28 <dependencyManagement> 29 <dependencies> 30 <dependency> 31 <groupId>org.springframework.cloud</groupId> 32 <artifactId>spring-cloud-dependencies</artifactId> 33 <version>Brixton.RELEASE</version> 34 <type>pom</type> 35 <scope>import</scope> 36 </dependency> 37 </dependencies> 38 </dependencyManagement>

經過@LoadBalanced註解實現負載均衡的開啓

 
1 package com; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.web.client.RestTemplate; 9 10 @SpringBootApplication 11 @EnableDiscoveryClient 12 public class RibbonApplication { 13 14 @Bean 15 @LoadBalanced//負載均衡的開啓 16 RestTemplate restTemplate() { 17 return new RestTemplate(); 18 } 19 20 public static void main(String[] args) { 21 SpringApplication.run(RibbonApplication.class, args); 22 } 23 24 }

建立ConsumerController來消費COMPUTE-SERVICE的add服務。經過直接RestTemplate來調用服務,計算10 + 20的值。

 
1 package com; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.RestController; 7 import org.springframework.web.client.RestTemplate; 8 9 @RestController 10 public class ConsumerController { 11 12 @Autowired 13 RestTemplate restTemplate; 14 15 @RequestMapping(value = "/add", method = RequestMethod.GET) 16 public String add() { 17 return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody(); 18 } 19 // 20 }

application.properties中配置eureka服務註冊中心

 
1 spring.application.name=ribbon-consumer 2 server.port=3333 3 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

這個時候修改服務提供方把端口改成2223,再啓動一個服務提供

可一看到服務中心已經被註冊了兩個服務。

 

 

啓動服務消費方,並訪問五次:http://localhost:3333/add

而後,打開compute-service的兩個服務提供方,分別輸出了相似下面的日誌內容:

端口2222的 

 

 

端口2223的

 

 

 

能夠看到,以前啓動的兩個compute-service服務端分別被調用了三次,兩次。到這裏,咱們已經經過Ribbon在客戶端已經實現了對服務調用的均衡負載。

有興趣的能夠加一下854630135這個羣去交流一下噢

相關文章
相關標籤/搜索