现在我们通过插件的方式添加新的一种策略。

package com.zhuyang.config;  

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.RandomRule; /**
*
* Here, we override the IPing and IRule used by the default load balancer. The
* default IPing is a NoOpPing (which doesn’t actually ping server instances,
* instead always reporting that they’re stable), and the default IRule is a
* ZoneAvoidanceRule (which avoids the Amazon EC2 zone that has the most
* malfunctioning servers, and might thus be a bit difficult to try out in our
* local environment).
*
*/
@Configuration
public class RibbonConfiguration {
@Bean
public IClientConfig ribbonPing(){
IClientConfig config = new DefaultClientConfigImpl();
return config;
} @Bean
public IRule ribbonRule(IClientConfig config) { return new MyRule();
}
}
 

MyRule.java是自己定义的个算法,大概算法是随机选中能被2整除的server

这样我们自己写的策略算法就可以正常工作了。 所有的请求都只会到8003或者8000折两台server去。

springcloud Ribbon自定义负载均衡插件的更多相关文章

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

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

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

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

  3. springcloud ribbon 客户端负载均衡用法

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...

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

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

  5. Srping cloud Ribbon 自定义负载均衡

    IRule 默认提供有7种方式,使用轮询方式 如何自定义 1:主启动类加@RibbonClient @RibbonClient(name="微服务名", configuration ...

  6. Spring-Cloud-Ribbon学习笔记(二):自定义负载均衡规则

    Ribbon自定义负载均衡策略有两种方式,一是JavaConfig,一是通过配置文件(yml或properties文件). 需求 假设我有包含A和B服务在内的多个微服务,它们均注册在一个Eureka上 ...

  7. SpringCloud全家桶学习之客户端负载均衡及自定义负载均衡算法----Ribbon(三)

    一.Ribbon是什么? Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端  负载均衡的工具(这里区别于nginx的负载均衡).简单来说,Ribbon是Netf ...

  8. SpringCloud Netflix Ribbon(负载均衡)

    ⒈Ribbon是什么? Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具. Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负 ...

  9. java框架之SpringCloud(4)-Ribbon&Feign负载均衡

    在上一章节已经学习了 Eureka 的使用,SpringCloud 也提供了基于 Eureka 负载均衡的两种方案:Ribbon 和 Feign. Ribbon负载均衡 介绍 SpringCloud ...

随机推荐

  1. java代码在开始事务后,先做了一个查询,再insert,此时会报: java.sql.SQLException: could not retrieve transation read-only status server

    解决过程: 查看mysql的事物隔离级别 SHOW VARIABLES LIKE '%iso%'; 返回结果: REPEATABLE-READ 把这个改成:READ-COMMITTED 就好了: SE ...

  2. 【nodejs】使用response输出中文但页面中文乱码的处置

    两点要确认: 1.head里有<meta charset="utf-8"/> 2.js文件编码为utf-8格式. 第二点往往容易被忽略,所以出现乱码. 附上代码: 'u ...

  3. web开发学习之旅

    过段时间要去实习,提前问了下老师我要准备哪些知识. 2015年3月19日,老师告诉我的,ionic Framework,Yii Framework,AngularJS,还有一些前端开发知识. 我除了听 ...

  4. js-取值&赋值-获取某标签某属性的值

      js 取值&赋值-获取某标签某属性的值 CreateTime--2016年10月16日16:35:34 Author:Marydon 1.取值 //方法一 //自定义属性必须用getAtt ...

  5. 【DB2】db2常用SQL语句集合

    持续更新中 1.修改日志模式为不记录 alter table table_name  activate not logged initially; 2.清空表 alter table t1 activ ...

  6. python之函数用法__setattr__

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__setattr__ #http://www.cnblogs.com/hongfei ...

  7. servlet ServletConfig ServletContext

    ServletConfig对象 在Servlet的配置文件中,可以使用一个或者多个<init-param>标签为servlet配置一些初始化参数. 当servlet配置了初始化参数后,we ...

  8. JavaScript-实现滚动条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 在linux下安装并使用websocket

    前言 首先,对websocket要有一个简要的了解与认识 websocket是HTML5开始提供的一种浏览器与服务器进行全双工通讯的网络技术,属于应用层协议. 它基于TCP传输协议,并复用HTTP的握 ...

  10. HDUOJ---2082

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...