一、Ribbon中的负载均衡策略

1、Ribbon中支持的负载均衡策略

AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值) | 使用一个AvailabilityPredicate来包含过滤server的逻辑,其实就就是检查status里记录的各个server的运行状态

RandomRule:随机选择一个server

BestAvailabl:选择一个最小的并发请求的server,逐个考察Server,如果Server被tripped了,则忽略

RoundRobinRule:roundRobin方式轮询选择, 轮询index,选择index对应位置的server

WeightedResponseTimeRule:根据响应时间分配一个weight(权重),响应时间越长,weight越小,被选中的可能性越低

RetryRule:对选定的负载均衡策略机上重试机制,在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server

ZoneAvoidanceRule:复合判断server所在区域的性能和server的可用性选择server

ResponseTimeWeightedRule:作用同WeightedResponseTimeRule,二者作用是一样的,ResponseTimeWeightedRule后来改名为WeightedResponseTimeRule

二、验证

1、自定义负载均衡策略

  1. # 自定义负载均衡策略
    springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // 自定义使用随机策略,springboot-h2是服务应用名

2、修改调用代码

package com.chhliu.springboot.restful.controller;  

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.chhliu.springboot.restful.vo.User; @RestController
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate; @Autowired
private LoadBalancerClient loadBalancerClient; @GetMapping("/template/{id}")
public User findById(@PathVariable Long id) {
ServiceInstance serviceInstance = this.loadBalancerClient.choose("springboot-h2");
System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
+ serviceInstance.getPort());// 打印当前调用服务的信息
User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class);
System.out.println(u);
return u;
}
}

3、测试

服务调用关系如下:

测试结果如下:

  1. ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7901
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7901
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7901
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7901
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7902
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7901
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    ===:springboot-h2:127.0.0.1:7901
    User [id=2, username=user2, name=李四, age=20, balance=100.00]
    发现选择7901端口服务和7902端口服务确实是随机的!

spring cloud中通过配置文件自定义Ribbon负载均衡策略的更多相关文章

  1. Spring Cloud (3)B Ribbon 负载均衡 IRule

    package com.service.config; import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.Ra ...

  2. SPring cloud (3)A Ribbon 负载均衡 配置初步

    1.引用pom <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...

  3. Spring Cloud入门教程(二):客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  4. spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?

    Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...

  5. 《Spring Cloud》学习(二) 负载均衡!

    第二章 负载均衡 负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.Spring Cloud Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于Netfli ...

  6. Ribbon负载均衡策略与自定义配置new

    Ribbon负载均衡策略 配置 对调用的某个服务启用某种负载策略 1)通过配置文件配置 hello: ribbon: NFLoadBalancerRuleClassName:com.netflix.l ...

  7. Ribbon负载均衡策略与自定义配置

    Ribbon负载均衡策略 配置 对调用的某个服务启用某种负载策略 1)通过配置文件配置 hello: ribbon: NFLoadBalancerRuleClassName:com.netflix.l ...

  8. springcloud(十四)、ribbon负载均衡策略应用案例

    一.eureka-server服务中心项目不再创建 二.eureka-common-empdept公共组件项目不再掩饰 三.创建eureka-client-provider-empdept-one提供 ...

  9. Spring Cloud官方文档中文版-客户端负载均衡:Ribbon

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...

随机推荐

  1. swift学习之元组

    元组在oc中是没有的.在swift中是新加的,学oc数组概念时还在想既然能够存储同样类型的元素,那不同类型的元素有没有东西存储呢,答案非常悲伤,oc没有元组这个概念.只是swift中加入了这个东西,也 ...

  2. 栅格计算器函数之Con

    Con函数是condition(条件)的缩写,其作用 语法是: Con(条件,条件为真执行语句,条件为假执行语句[可选]) 或Con(输入栅格,条件为真执行语句,条件为假执行语句[可选],逻辑表达式) ...

  3. JDBC一(web基础学习笔记七)

    一.JDBC Java数据库的连接技术(Java DataBase Connectivity),能实现Java程序以各种数据库的访问 由一组使用Java语言编写的类和接口(JDBC API)组成,它j ...

  4. java 新创建的类要重写的方法

    重写toString方法,可以控制println打印的结构. 如果需要往hashSet或者HashMap中存,需要重写hashCode和equals方法,因为hashSet执行添加,以对象为参数删除, ...

  5. Start-Sleep 帮助信息

    如下说明是翻译: help Start-Sleep 产生的帮助信息.译者: Edengundam(马涛) Start-Sleep 大纲使shell, 脚本, 或运行空间的活动挂起指定的时间. 语法St ...

  6. js的7个技巧

    http://www.vaikan.com/seven-javascript-things-i-wish-i-knew-much-earlier-in-my-career/

  7. android的download manager(1)

    android 2.3中引入了Download manager.作为一个service来优化长时间下载操作的处理.Download Manager通过处理HTTP链接.监控链接的变化和系统又一次启动来 ...

  8. PHP中的密码加密的解决方案

    层出不穷的类似事件对用户会造成巨大的影响,因为人们往往习惯在不同网站使用相同的密码,一家“暴库”,全部遭殃 一般的解决方案 1.将明文密码做单向hash $password = md5($_POST[ ...

  9. linux显示桌面快捷键设置

    2013-01-06 10:31:52    Ubuntu显示桌面Indicator  IN: LINUX :-) HOT: 1,246 ℃  18十2011   www.2cto.com   大家一 ...

  10. Eclipse 调试器:零距离接触实战技巧

    http://my.oschina.net/willSoft/blog/37784调试的方法虽然千千万万,但归根结底,就是找到引发错误的代码.Eclipse调试器的目标是让程序员能对本地或远程程序进行 ...