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的更多相关文章

  1. Spring Cloud 入门 之 Ribbon 篇(二)

    原文地址:Spring Cloud 入门 之 Ribbon 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Cloud 入门 之 Eureka ...

  2. Spring Cloud入门教程-Ribbon实现客户端负载均衡

    简介 我们继续以之前博客的代码为基础,增加Ribbon组件来提供客户端负载均衡.负载均衡是实现高并发.高性能.可伸缩服务的重要组成部分,它可以把请求分散到一个集群中不同的服务器中,以减轻每个服务器的负 ...

  3. spring cloud: zuul(三): ribbon负载均衡配置

    zuul的routes配置下path/url组合不支持负载均衡 下面介绍zuul的routes配置下的path/serviceId负载均衡配置 spring-boot-user微服务开启了:7901, ...

  4. Spring Cloud 入门 之 Feign 篇(三)

    原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...

  5. Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

    接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...

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

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

  7. Spring Cloud 入门教程(三): 配置自动刷新

    之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Server也不行. 比如上一单元(Spring Cloud ...

  8. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  9. Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

    首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...

随机推荐

  1. Windows 下openssl安装与配置

    编译thirift失败 网上方法很多,大部分是针对32位机的,自己的电脑因为是win7,64位,摸索了很久才安装成功. 环境 WIN7, 64位, vs2005 下载ActivePerl 配置过程中需 ...

  2. AtCoder Regular Contest 061 DSnuke's Coloring

    http://arc061.contest.atcoder.jp/tasks/arc061_b 题意: H行W列的矩阵中,然后挖了n个洞,输出j(0-9)行,对于第i行输出,有多少个3*3区域中有i个 ...

  3. STL排序和检索

    //参考书是刘汝佳的那本算法书P108 //sort的用法也就是本来是从小到大排序,如果想要从大到小,中间写一个比较函数就可以了: //以下两个检索的东西 //lower_bound找到一个值的最小插 ...

  4. jquery html() 和text()的用法

    html()类似JS中的 innerHTML,首先看一段代码: <!DOCTYPE html> <html lang="en"> <head> ...

  5. 【数据结构(C语言版)系列四】 串

    串类型的定义 串(或字符串)是由零个或多个字符组成的有限序列,一般记为 s = 'a1a2...an',s为串名.子串在主串中的位置以子串的第一个字符在主串中的位置来表示. 串和表示和实现——定长顺序 ...

  6. Educational Codeforces Round 19 A

    Description Given a positive integer n, find k integers (not necessary distinct) such that all these ...

  7. Hibernate通过实体对象对应数据库表信息

    Hibernate通过实体对象对应数据库表信息,包括:数据库表名称.主键列名.非主键列名等. 获取对象映射缓存管理类: AbstractEntityPersister aep = (AbstractE ...

  8. 基于node 搭建http2服务

    1.准备工作:安装node2.安装http2: npm install http2 -g安装完成后,在安装目录中appData/Roaming>npm>node_modules>ht ...

  9. iOS中自定义UITableViewCell的用法

    1.先创建一个View继承 UITableViewCell并使用xib快速建立模型. #import <UIKit/UIKit.h> #import "Score.h" ...

  10. 苹果手机通过Safari浏览器访问web方式安装In-House应用

    需求背景 公司内部员工使用的iOS客户端应用希望对内开放,不需要发布于AppStore直接能够让内部用户获取,对于Android应用来说这个问题很好解决,直接下发安装包然后就能安装了:但是对于苹果生态 ...