SpringCloud微服务系列(4): 服务发现与消费及客户端负载均衡Ribbon
2017-08-07 20:29
501 查看
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中包含如下核心依赖:
第二步:开启Eureka服务发现功能
在主类RibbonconsumerApplication中添加注解@EnableDiscoveryClient,让该应用注册为Eureka客户端应用,以获得服务发现的能力。
第三步:开启Ribbon客户端负载均衡功能
在主类中创建RestTemplate的Bean实例,并通过添加@LoadBalanced开启Ribbon客户端负载均衡,如下所示:
第四步:实现ribbon-consumer服务消费接口
新增ConsumerController类如下:
第五步:配置Eureka注册中心及ribbon-consumer消费者启动端口
注意:做这一步时,需要确保之前的注册中心是启动的。
application.properties配置文件如下:
第六步:启动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/
作者:家辉,日期: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注册中心的时候应该怎么配置? 目前我配置一个,貌似两个都注册了
参考资料:
[1] http://start.spring.io/
[2] http://projects.spring.io/spring-cloud/
相关文章推荐
- 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)
- 使用feign作为客户端来消费服务,提供负载均衡和断路器支持
- springCloud学习01之eureka服务发现-提供者-消费者ribbon/feign-负载均衡
- SpringCloud(第 011 篇)电影Ribbon微服务,脱离Eureka使用配置listOfServers进行客户端负载均衡调度
- Mesos&PaaS系列文章之五——负载均衡与服务发现
- spring cloud-使用feign来消费Restful服务同时加入Ribbon来实现负载均衡
- 微服务4 Ribbon实现客户端负载均衡
- SpringCloud(第 006 篇)电影微服务,使用 Ribbon 在客户端进行负载均衡
- 微服务架构模式系列文章之四:客户端服务发现
- SpringCloud(第 006 篇)电影微服务,使用 Ribbon 在客户端进行负载均衡
- SpringCloud(第 008 篇)电影微服务,使用 application.yml 配置文件配置 Ribbon 在客户端进行负载均衡调度算法
- Eureka+ribbon 实现服务注册与发现和负载均衡
- SpringCloud调研系列2.1:服务发现-Ribbon
- 干货实操:微服务Spring Cloud 系列(三) Eureka服务消费(Ribbon使用)
- Spring-cloud 服务发现与消费(以ribbon为例)
- SpringCloud(第 008 篇)电影微服务,使用 application.yml 配置文件配置 Ribbon 在客户端进行负载均衡调度算法
- SpringCloud-----1、Eureka实现服务治理(Ribbon客户端负载均衡调用服务)
- Spring Cloud Eureka与Ribbon——服务发现与消费
- SpringCloud(第 008 篇)电影微服务,使用 application.yml 配置文件配置 Ribbon 在客户端进行负载均衡调度算法
- Spring Cloud微服务(4)之Ribbon客户端负载均衡