SpringCloud微服务系列(4): 服务发现与消费及客户端负载均衡Ribbon

来源:互联网 发布:linux 密码修改 编辑:程序博客网 时间:2024/05/14 04:20
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@LoadBalancedRestTemplate 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;@RestControllerpublic class ConsumerController {@AutowiredRestTemplate 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-consumerserver.port=9000eureka.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注册中心的时候应该怎么配置? 目前我配置一个,貌似两个都注册了 

参考资料:
[1] http://start.spring.io/
[2] http://projects.spring.io/spring-cloud/
阅读全文
0 0