SpringCloud微服務系列(4): 服務發現與消費及客戶端負載均衡Ribbon

gobitan發表於2017-08-07
SpringCloud微服務系列(4): 服務發現與消費及客戶端負載均衡Ribbon
作者:家輝,日期:2017-08-07 CSDN部落格: http://blog.csdn.net/gobitan
摘要:在本系列的前三篇分別建立了一個Eureka微服務註冊中心,一個hello服務以及為註冊中心增加高可用。本文介紹如何發現與消費服務以及客戶端負載均衡Ribbon。

概述
服務的發現由Eureka客戶端完成,而服務的消費由Ribbon來實現。Ribbon是一個基於HTTP和TCP的客戶端負載均衡器。

第一步:建立支援Web,Eureka Discovery和Ribbon的Spring Boot工程
通過http://start.spring.io/建立一個Spring Boot工程,具體引數如下:
Generate a "Maven Project" with "Java" and Spring Boot"1.5.6",
ProjectMetadata Group: cn.dennishucd
Artifact: ribbonconsumer
Dependencies: Web, Eureka Discovery, Ribbon
然後點選"Generate Project"即可得到一個包含Web,Eureka Discovery和Ribbon的Spring boot工程。
pom.xml中包含如下核心依賴:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

第二步:開啟Eureka服務發現功能
在主類RibbonconsumerApplication中新增註解@EnableDiscoveryClient,讓該應用註冊為Eureka客戶端應用,以獲得服務發現的能力。

第三步:開啟Ribbon客戶端負載均衡功能
在主類中建立RestTemplate的Bean例項,並通過新增@LoadBalanced開啟Ribbon客戶端負載均衡,如下所示:
@Bean
@LoadBalanced
RestTemplate restTemplate() {
    return new RestTemplate();
}

第四步:實現ribbon-consumer服務消費介面
新增ConsumerController類如下:
package cn.dennishucd.ribbonconsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
    return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
}
}

第五步:配置Eureka註冊中心及ribbon-consumer消費者啟動埠
注意:做這一步時,需要確保之前的註冊中心是啟動的。
application.properties配置檔案如下:
spring.application.name=ribbon-consumer
server.port=9000

eureka.client.serviceUrl.defaultZone=http://eureka1:1111/eureka/

第六步:啟動ribbon-consumer服務消費者
前置條件:需要確保啟動的服務列表如下:
[1] 啟動兩個eureka註冊中心:eureka1和eureka2; 啟動方法參考本系列文章的第3篇.
[2] 啟動兩個hello-service服務註冊到註冊中心;
java -jar springboot-0.0.1-SNAPSHOT.jar --server.port=8081
java -jar springboot-0.0.1-SNAPSHOT.jar --server.port=8082
啟動ribbon-consumer後,看到註冊中心註冊了兩個hello服務和一個服務消費者服務,執行http://localhost:9000/ribbon-consumer,成功返回“Hello World”。並檢視日誌輸出,可以看到服務列表情況等。如:
2017-08-07 20:51:07.177  INFO 3638 --- [nio-9000-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client hello-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=hello-service,current list of Servers=[10.191.30.30:8082, 10.191.30.30:8081],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;        Instance count:2;       Active connections count: 0;    Circuit breaker tripped count: 0;       Active connections per server: 0.0;]

嘗試多次請求該服務,可以看到啟動在8081和8082埠的服務依次被輪詢到,實現了負載均衡。

疑問:
[1] 在客戶端陪住註冊中心的時候,對於有HA註冊中心的時候應該怎麼配置? 目前我配置一個,貌似兩個都註冊了 

參考資料:

相關文章