<Spring Cloud>入门三 Ribbon
1.Ribbon
客户端软负载均衡组件
1.1配置
搭建了三个消费者供客户端调用:

1.修改yml
eureka:
client:
service-url:
defaultZone: http://eureka-server01:8761/eureka/,http://eureka-server02:8762/eureka/
register-with-eureka: false
2.修改配置类
@LoadBalanced ,默认采用RoundRobin
@Configuration
public class ConfigBean { @Bean
@LoadBalanced //开启负载均衡 Ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
} }
3.启动类上标注 eurekaclient
@SpringBootApplication
@EnableEurekaClient
public class App_Consumer_Dept_80 { public static void main(String[] args) {
SpringApplication.run(App_Consumer_Dept_80.class, args);
}
}
1.2 修改负载均衡算法
在配置类中注入需要算法的Bean

可选算法

1.3 自定义负载均衡算法
@RibbonClient
name:服务提供方的application.name
configuration = 自定义配置类的名字

注意自定义配置类不能在spring boot 启动类的同包或子包下,或者使@ComponentScan不扫描该类

package org.rule.define; import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author mapleins
* @Date 2019-01-12 21:45
* @Desc 自定义ribbon 的rule不能再@ComponentScan的包或子包下
**/
@Configuration
public class MySelfRule { /**
* 需求:轮询访问,每个服务访问3次
*/
@Bean
public IRule myRule(){
return new RoundRobin_Maple();
}
}
编写自己的算法,轮询访问,每台服务器访问3次
复制了随机算法的源代码,进行修改
package org.rule.define; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server; import java.util.List;
import java.util.Random; /**
* @author mapleins
* @Date 2019-01-12 22:05
* @Desc 需求:轮询访问,每个服务访问3次
**/
public class RoundRobin_Maple extends AbstractLoadBalancerRule { /**
* 当前该服务器被访问的次数
*/
private int total = 0 ; /**
* 当前是哪台服务器
*/
private int currentIndex = 0 ; public RoundRobin_Maple() {
} public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
} else {
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 == 0) {
return null;
} if(currentIndex < upList.size()){ //当前服务器的index<节点数
if(total < 3){
total++;
}else {
currentIndex++;
total = 0;
continue;
}
}else {
currentIndex = 0;
total = 0;
continue;
} server = (Server)upList.get(currentIndex);
if (server == null) {
Thread.yield();
} else {
if (server.isAlive()) {
return server;
} server = null;
Thread.yield();
}
} return server;
}
} public Server choose(Object key) {
return this.choose(this.getLoadBalancer(), key);
} public void initWithNiwsConfig(IClientConfig clientConfig) {
} }
<Spring Cloud>入门三 Ribbon的更多相关文章
- Spring Cloud 入门 之 Ribbon 篇(二)
原文地址:Spring Cloud 入门 之 Ribbon 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Cloud 入门 之 Eureka ...
- Spring Cloud入门教程-Ribbon实现客户端负载均衡
简介 我们继续以之前博客的代码为基础,增加Ribbon组件来提供客户端负载均衡.负载均衡是实现高并发.高性能.可伸缩服务的重要组成部分,它可以把请求分散到一个集群中不同的服务器中,以减轻每个服务器的负 ...
- spring cloud: zuul(三): ribbon负载均衡配置
zuul的routes配置下path/url组合不支持负载均衡 下面介绍zuul的routes配置下的path/serviceId负载均衡配置 spring-boot-user微服务开启了:7901, ...
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
- Spring Cloud入门教程(二):客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- Spring Cloud 入门教程(三): 配置自动刷新
之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Server也不行. 比如上一单元(Spring Cloud ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
随机推荐
- LuoguP2320/CF1037A 用二进制表示数的奥妙重重方法 By cellur925
题目描述 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政. 有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会,其中有一件宝物引起了 ...
- The 17th Zhejiang University Programming Contest Sponsored by TuSimple A
Marjar Cola Time Limit: 1 Second Memory Limit: 65536 KB Marjar Cola is on sale now! In order to ...
- Codeforces Round #408 (Div. 2) C
Description Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. ...
- Testing Round #12 C
Description For the given sequence with n different elements find the number of increasing subsequen ...
- CentOS6.5下安装Redis2.8.6和phpredis2.2.4扩展
一.版本说明 CentOS版本 [plain]view plaincopyprint? [root@localhost ~]# uname Linux [root@localhost ~]# unam ...
- Apache Cordova
http://cordova.apache.org/ Apache Cordova is a platformfor building native mobile applications using ...
- canvas基础绘制-绚丽倒计时
效果图: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- word打印小册子
使用联想m7250f打印册子,打印时设置该打印机属性为双面打印(手动),打印第一面后,将所有打印出的纸拿出并翻转使对应word中的第2页的打印纸朝外,之后将所有纸放入纸盒,再点击打印第二面即可.
- SQLServer · 最佳实践 · SQL Server 2012 使用OFFSET分页遇到的问题
1. 背景 最近有一个客户遇到一个奇怪的问题,以前使用ROW_NUMBER来分页结果是正确的,但是替换为SQL SERVER 2012的OFFSET...FETCH NEXT来分页出现了问题,因此,这 ...
- qt5.8+vs2015使用Qt5WebEngine搭建环境
转载请注明出处:http://www.cnblogs.com/dachen408/p/7575094.html 1.项目属性,C/C++,所有选项,附加包含目录新增. $(QTDIR)\include ...