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. poj1308【并查集】

    = =.如果输入的两个数相等.就不是一颗树啊,不能自己指向自己. 水.(瞎开的数组). //#include <bits/stdc++.h> #include<iostream> ...

  2. 基于 CODING 轻松搞定持续集成

    点击观看视频教程 带你一步一步搞定 CODING 持续集成 持续集成加速软件交付 持续集成这个概念是由 Grady Booch 在 1991 年首次提出,随后成为了 DevOps 的核心实践之一.持续 ...

  3. Luogu P1156 垃圾陷阱 【dp】By cellur925

    题目传送门 这题...看上去浓浓的背包气息...但是并不好设计状态啊emmm. 我们考虑可能成为状态的量:高度.血量.时间.物品.看数据范围也猜到应该大概是个二维dp了w. 正确的状态设计之一:设$f ...

  4. hdu1875 畅通工程再续 暴力+基础最小生成树

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ; ; ; in ...

  5. [转]Java中Date转换大全,返回yyyy-MM-dd的Date类型

    /** * 获取现在时间,这个好用 * * @return返回长时间格式 yyyy-MM-dd HH:mm:ss */ public static Date getSqlDate() { Date s ...

  6. 【学习笔记】深入理解js原型和闭包(1)—— 一切都是对象

    “一切都是对象”这句话的重点在于如何去理解“对象”这个概念. ——当然,也不是所有的都是对象,值类型就不是对象. 首先咱们还是先看看javascript中一个常用的运算符——typeof.typeof ...

  7. 【学习笔记】using namespace std 的作用

    C++编程时几乎每次都敲上using namespace std;但这行代码究竟有什么作用呢? C++标准程序库中的所有标识符都被定义于一个名为std的namespace中. 早些的编码将标准库功能定 ...

  8. XDroidMvp 轻量级的Android MVP快速开发框架

    XDroidMvp是XDroidAndroid快速开发框架的MVP版本,其使用方式类似于XDroid,大部分源码也来自XDroid. XDroidMvp主要会有这些特性: 无需写Contract! 无 ...

  9. 详解 Handler 消息处理机制(附自整理超全 Q&A)

    Android 为什么要用消息处理机制 如果有多个线程更新 UI,并且没有加锁处理,会导致界面更新的错乱,而如果每个更新操作都进行加锁处理,那么必然会造成性能的下降.所以在 Android 开发中,为 ...

  10. SQL Server 2012使用OFFSET/FETCH NEXT分页及性能测试

    最近在网上看到不少文章介绍使用SQL Server 2012的新特性:OFFSET/FETCH NEXT 实现分页.多数文章都是引用或者翻译的这一篇<SQL Server 2012 - Serv ...