IRule 默认提供有7种方式,使用轮询方式

如何自定义

1:主启动类加@RibbonClient

@RibbonClient(name="微服务名", configuration=MySelfRule.class)

也就是说MySelfRule.java不能和@SpringBootApplication注释的类在同一包下.

MySelfRule.java

package com.atguigu.myrule;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule; @Configuration
public class MySelfRule
{
@Bean
public IRule myRule()
{
//return new RandomRule();// Ribbon默认是轮询,我自定义为随机
//return new RoundRobinRule();// Ribbon默认是轮询,我自定义为随机 return new RandomRule_ZY();// 我自定义为每台机器5次
}
}
package com.atguigu.myrule;

import java.util.List;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server; public class RandomRule_ZY extends AbstractLoadBalancerRule
{ // total = 0 // 当total==5以后,我们指针才能往下走,
// index = 0 // 当前对外提供服务的服务器地址,
// total需要重新置为零,但是已经达到过一个5次,我们的index = 1
// 分析:我们5次,但是微服务只有8001 8002 8003 三台,OK?
// private int total = ; // 总共被调用的次数,目前要求每台被调用5次
private int currentIndex = ; // 当前提供服务的机器号 public Server choose(ILoadBalancer lb, Object key)
{
if (lb == null) {
return null;
}
Server server = null; while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers(); int serverCount = allList.size();
if (serverCount == ) {
/*
* No servers. End regardless of pass, because subsequent passes only get more
* restrictive.
*/
return null;
} // int index = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
// server = upList.get(index); // private int total = 0; // 总共被调用的次数,目前要求每台被调用5次
// private int currentIndex = 0; // 当前提供服务的机器号
if(total < )
{
server = upList.get(currentIndex);
total++;
}else {
total = ;
currentIndex++;
if(currentIndex >= upList.size())
{
currentIndex = ;
}
} if (server == null) {
/*
* The only time this should happen is if the server list were somehow trimmed.
* This is a transient condition. Retry after yielding.
*/
Thread.yield();
continue;
} if (server.isAlive()) {
return (server);
} // Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
} return server; } @Override
public Server choose(Object key)
{
return choose(getLoadBalancer(), key);
} @Override
public void initWithNiwsConfig(IClientConfig clientConfig)
{
// TODO Auto-generated method stub } }

Srping cloud Ribbon 自定义负载均衡的更多相关文章

  1. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...

  2. Spring Cloud Ribbon——客户端负载均衡

    一.负载均衡负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意思 ...

  3. Spring Cloud Ribbon客户端负载均衡(四)

    序言 Ribbon 是一个客户端负载均衡器(Nginx 为服务端负载均衡),它赋予了应用一些支配 HTTP 与 TCP 行为的能力,可以得知,这里的客户端负载均衡也是进程内负载均衡的一种.它在 Spr ...

  4. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得【无熔断器】

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 了解了 eureka 服务注册与发现 的3大角色 ,会使用RestTem ...

  5. Spring Cloud Gateway Ribbon 自定义负载均衡

    在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...

  6. SpringCloud的Ribbon自定义负载均衡算法

    1.Ribbon默认使用RoundRobinRule策略轮询选择server 策略名 策略声明 策略描述 实现说明 BestAvailableRule public class BestAvailab ...

  7. 笔记:Spring Cloud Ribbon 客户端负载均衡

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

  8. Spring Cloud Ribbon 客户端负载均衡 4.3

      在分布式架构中,服务器端负载均衡通常是由Nginx实现分发请求的,而客户端的同一个实例部署在多个应用上时,也需要实现负载均衡.那么Spring Cloud中是否提供了这种负载均衡的功能呢?答案是肯 ...

  9. Ribbon自定义负载均衡策略,在网关实现类似Ip_hash的负载均衡,ribbon给单个服务配置属性

    背景: 我需要在网关实现一种功能,某个用户的请求永远打在后台指定的服务,也就是根据ip地址进行负载均衡 原理: 在ribbon的配置类下: 那我们自己创建一个IRule的实现类,模仿ZoneAvoid ...

随机推荐

  1. P2837晚餐队列安排

    传送 特写此篇,纪念不用dp做dp题 洛谷说这是个dp,但我不信(其实就是不会dp),因此我们考虑用另一种思路.修改后的队列每一个 数a[i]一定满足a[i]<=a[i+1],那修改后的顺序就是 ...

  2. 『Hi,我是易建科技eKing Cloud!』

    写在前面:这是我的第一篇自我介绍式文章,是对易建科技我所在云服务事业群的云平台产品和业务的总结和介绍.本文始发于 Linux宝库 公众号,这是原文链接.感谢公众号主人陈绪总,感谢公众号的编辑们!感谢易 ...

  3. base_基础

    目录 A B C D E F G H I J K L M N S: Sqlite: 1;orhanobut/hawk; A: Adapter: 图片处理 Android中自定义布局中加载图片Bitma ...

  4. java的几种引用

    强引用:无论内存是否足够,不会回收. 软引用:内存不足时,回收该引用关联的对象. 弱引用:垃圾回收时,无论内存是否足够,都会回收. 虚引用:任何时候都可能被垃圾回收器回收. 在Java中,虽然不需要程 ...

  5. 本地IP,掩码,网关,DNS设置

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. day4作业(基本运算流程if for)

    #coding:utf-8'''默写: 循环嵌套 必做: 1. 求1-100的所有数的和 2. 输出 1-100 内的所有奇数 3. 输出 1-100 内的所有偶数 5. 求1-2+3-4+5 ... ...

  7. linux安装python3 ,安装IPython ,安装jupyter notebook

    安装python3    下载到 /opt/中 1.下载python3源码,选择3.6.7因为ipython依赖于>3.6的python环境wget https://www.python.org ...

  8. Java远程通讯技术及原理分析

    在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MINA.ESB.Burlap.Hessian.SOAP.EJB和JMS等,这些 ...

  9. nodejs开篇基础<①>

    1.安装相关 //安装brew Homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/insta ...

  10. (转)使用Flexible实现手淘H5页面的终端适配

    原文链接 曾几何时为了兼容IE低版本浏览器而头痛,以为到Mobile时代可以跟这些麻烦说拜拜.可没想到到了移动时代,为了处理各终端的适配而乱了手脚.对于混迹各社区的偶,时常发现大家拿手机淘宝的H5页面 ...