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
- SpringCloud微服务系列(4): 服务发现与消费及客户端负载均衡Ribbon
- SpringCloud---Ribbon客户端负载均衡
- SpringCloud(第 006 篇)电影微服务,使用 Ribbon 在客户端进行负载均衡
- SpringCloud(第 007 篇)电影微服务,使用定制化 Ribbon 在客户端进行负载均衡,使用 RibbonClient 不同服务不同配置策略
- springcloud使用ribbon实现客户端负载均衡
- SpringCloud(第 011 篇)电影Ribbon微服务,脱离Eureka使用配置listOfServers进行客户端负载均衡调度
- springcloud微服务三:Eureka服务治理之注册服务提供者及服务的发现和消费
- Eureka+ribbon 实现服务注册与发现和负载均衡
- springCloud学习01之eureka服务发现-提供者-消费者ribbon/feign-负载均衡
- SpringCloud-----1、Eureka实现服务治理(Ribbon客户端负载均衡调用服务)
- Spring Cloud微服务(4)之Ribbon客户端负载均衡
- springcloud服务调用其它服务接口实例及ribbon均衡负载
- SpringCloud(第 008 篇)电影微服务,使用 application.yml 配置文件配置 Ribbon 在客户端进行负载均衡调度算法
- 【SpringCloud】(六):Ribbon实现客户端负载均衡
- SpringCloud 笔记 (六)---- 客户端负载均衡Ribbon
- SpringCloud(三)Ribbon负载均衡
- SpringCloud的Ribbon负载均衡
- SpringCloud微服务化(一)-服务注册与发现
- HDU-2680-Choose the best route
- stm32中关于读取状态标志位函数的自我理解
- css三角形的IE兼容写法
- 我的第二篇博客
- java 递归实现汉诺塔问题
- SpringCloud微服务系列(4): 服务发现与消费及客户端负载均衡Ribbon
- GPU编程自学7 —— 常量内存与事件
- 数组、指针、引用、与链表
- Coder HDU
- Java设计模式之抽象工厂模式
- 递归和非递归分别实现求第n个斐波那契数(C语言)
- HDU 3001 Travelling (三进制状压DP)
- nodejs 实践:express 最佳实践 (一) 项目结构
- Pow(x, n)