一、Ribbon负载均衡

还是先通过一个demo直观的体验一下spring cloud Ribbon负载均衡技能

1、引入pom依赖

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.5.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2、编写服务消费业务类ConsumerController

@RestController
public class ConsumerController {@AutowiredRestTemplate restTemplate;@RequestMapping(value = "/add", method = RequestMethod.GET)public String add() {return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();}
}

3、编写启动类

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(RibbonApplication.class, args);}
}

4、添加properties配置文件

spring.application.name=ribbon-consumer
server.port=3333eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动RibbonApplication main方法后,刷新Eureka监控平台,可见除去上文已注册到Eureka上的两个compute-service实例之外,还添加了当前ribbon-consumer服务。


5、演示Ribbon负载均衡功能

访问http://localhost:3333/add ,调用compute-service服务的add方法,执行http://COMPUTE-SERVICE/add?a=10&b=20,访问两次,可见compute-service服务1控制台打印出1次调用信息,compute-service服务1打印出第二次调用信息,表明两次调用均发到了两个不同的服务实例。这就是ribbon提供的服务智能路由、负载均衡功能。

二、How does Ribbonwork?

在RibbonApplication类中,@LoadBalanced注解就是实现服务智能路由的关键,通过查看@LoadBalanced源码,发现该注解用于标记一个RestTemplate bean,并使用LoadBalancerClient来配置它。LoadBalancerClient便是Ribbon实现负载均衡的关键入口

/*** Annotation to mark a RestTemplate bean to be configured to use a LoadBalancerClient* @author Spencer Gibb*/
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Qualifier
public @interface LoadBalanced {
}

LoadBalancerClient接口源码如下

public interface LoadBalancerClient {/*** Choose a ServiceInstance from the LoadBalancer for the specified service* @param serviceId the service id to look up the LoadBalancer* @return a ServiceInstance that matches the serviceId*/ServiceInstance choose(String serviceId);//根据传入的服务名serviceId,从负载均衡器中挑选一个对应服务的实例。/*** execute request using a ServiceInstance from the LoadBalancer for the specified* service* @param serviceId the service id to look up the LoadBalancer* @param request allows implementations to execute pre and post actions such as* incrementing metrics* @return the result of the LoadBalancerRequest callback on the selected* ServiceInstance*/<T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException;
    //使用从负载均衡器中挑选出的服务实例来执行请求内容。/*** Create a proper URI with a real host and port for systems to utilize.* Some systems use a URI with the logical serivce name as the host,* such as http://myservice/path/to/service.  This will replace the* service name with the host:port from the ServiceInstance.* @param instance* @param original a URI with the host as a logical service name* @return a reconstructed URI*/URI reconstructURI(ServiceInstance instance, URI original);
    //
}

大致流程如下:通过 LoadBalancerInterceptor拦截器对 RestTemplate的请求进行拦截,并利用Spring Cloud的负载均衡器 LoadBalancerClient将以逻辑服务名为host的URI转换成具体的服务实例的过程。

三、Ribbon负载均衡策略

Spring Cloud Ribbon--智能路由相关推荐

  1. java路由器开发_基于spring cloud的智能路由

    smart-route 基于spring cloud的智能路由,功能如下 开发模式:优先调用本地服务, order=0 SIT优先:优先调用指定IP服务, order=100 远程调试:远程调试指定服 ...

  2. Spring Cloud Ribbon(服务消费者)

    Spring Cloud Ribbon 是一个基于Http和TCP的客户端负载均衡工具,基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署,但是它几乎存在于每 ...

  3. 基于Spring cloud Ribbon和Eureka实现客户端负载均衡

    前言 本案例将基于Spring cloud Ribbon和Eureka实现客户端负载均衡,其中Ribbon用于实现客户端负载均衡,Eureka主要是用于服务注册及发现: 传统的服务端负载均衡 常见的服 ...

  4. resttemplate 请求重试_使用Spring Cloud Ribbon重试请求

    使用Spring Cloud Ribbon重试请求 在微服务调用中,一些微服务圈可能调用失败,通过再次调用以达到系统稳定性效果,本文展示如何使用Ribbon和Spring Retry进行请求再次重试调 ...

  5. java B2B2C springmvc mybatis多租户电子商城系统-Spring Cloud Ribbon

    Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现. 通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST ...

  6. Spring Cloud Ribbon –进行安全呼叫

    很简单,但是最近我为此感到困惑 –我必须对安全的远程服务进行基于Netflix Ribbon的客户端调用. 事实证明,使用Netflix Ribbon可以通过两种方式实现这一点,我将通过Spring ...

  7. Spring Cloud微服务笔记(四)客户端负载均衡:Spring Cloud Ribbon

    客户端负载均衡:Spring Cloud Ribbon 一.负载均衡概念 负载均衡在系统架构中是一个非常重要,并且是不得不去实施的内容.因为负载均衡对系统的高可用性. 网络压力的缓解和处理能力的扩容的 ...

  8. 为Spring Cloud Ribbon配置请求重试(Camden.SR2+)

    当我们使用Spring Cloud Ribbon实现客户端负载均衡的时候,通常都会利用@LoadBalanced来让RestTemplate具备客户端负载功能,从而实现面向服务名的接口访问(原理可见& ...

  9. Spring Cloud Ribbon 负载均衡客户端调用示例

    承接 Spring Cloud 最简入门示例 这一篇, 本篇演示如何使用负载均衡客户端 Ribbon 调用在Eureka注册的服务. Ribbon 是什么? Ribbon是Netflix 的开源项目, ...

最新文章

  1. 【Go】Go基础(四):流程控制(控制结构)
  2. 知识图谱如何让“人工智能”更智能?
  3. Java NIO之Selector(选择器)
  4. 练习 hdu 5523 Game
  5. JAVA笔记14__多线程共享数据(同步)/ 线程死锁 / 生产者与消费者应用案例 / 线程池...
  6. django 通过路径传参 视图获取get请求
  7. 非广告--推荐Dynatrace:树立数字化性能管理DPM标杆
  8. 史上最全CSDN中免积分下载攻略
  9. 力天创见客流统计标书制作
  10. 前向断言/前向预查/正向断言/正向预查(lookahead assertions)
  11. AutoCAD打开慢的解决方案
  12. BootStrap富文本编辑器Summernote
  13. Envoy 调试流量的常用技巧直播分享及问答整理
  14. centos7 mysql libssl_centos7.2安装mysql5.7.13及ssl主从复制
  15. HTTPS证书基本概述
  16. LDAP、OLAP、OLTP详细介绍
  17. springmvc配置thymeleaf视图解析器
  18. python10086查询系统_Python数字移动设备取证
  19. 计算机游戏中屏幕上显示的,电脑在玩全屏游戏的时候显示屏老是出现无信号
  20. DCN DCSW-6028-pro 内部portal认证

热门文章

  1. 微信wow服务器排队,怀旧服排队持续蔓延 76个服务器仅剩这18个不排队
  2. SQL数据库安装的时错误号:0x80072F8F
  3. 查询当前电脑安装的Java/JDK版本的方法(查看Java/JRE版本号)
  4. Linux + .net core 开发升讯威在线客服系统:同时支持 SQL Server 和 MySQL 的实现方法
  5. access和filemaker_Filemaker指南
  6. uni-app打包ios的步骤
  7. 【图像压缩】超先验模型 《VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR》
  8. MySQL 备份总结
  9. 某银行数据仓库建模流程和规范
  10. 百度在线语音识别接入经验