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

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注册中心的时候应该怎么配置? 目前我配置一个,貌似两个都注册了 

参考资料:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gobitan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值