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. 《高性能iOS 应用开发》之降低你 APP 的电量消耗

    在编写高性能 代码时, 电量消耗是一个需要重点处理的重要因素, 就执行时间和 CPU 资源的利用而言, 我们不仅要实现高效的数据结构和算法, 还需要考虑其他的因素,如果某个应用是个电池黑洞,那么一定不 ...

  2. python __builtins__ memoryview类 (46)

    46.'memoryview',  返回给定参数的内存查看对象(Momory view).所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问. cla ...

  3. bzoj 3613: [Heoi2014]南园满地堆轻絮【二分+贪心】

    二分答案w,然后判断的时候维护一个mx,扫描序列,先更新mx=max(mx,a[i]-w),然后如果a[i]+w<mx的话就是说这个位置即使升到极限并且前面降到极限也不能符合条件了 #inclu ...

  4. 蒟蒻ACMer回忆录 · 一段弱校ACM的奋斗史

    三年半的ACM生涯终于迎来了终点,退役之时,感慨万分,故写此文以纪念逝去的时光,那些为ACM拼搏的日子,那段弱校ACM的奋斗史. 三年半的ACM生涯,窝见证了CUMT从打铁到铜牌的突破,又见证了从铜牌 ...

  5. javascript E5面向对象和 E6面向对象

    javascript es6之前的面向对象方法: 一般使用构造函数来实现 function Person (name, age) { this.name = name; this.age = age; ...

  6. LuoguP1370 Charlie的云笔记序列 【dp】By cellur925

    题目传送门 题目大意:给你一个序列,求出它所有区间的本质不同的子序列个数.(空序列也算作本质不同),数据范围$1e5$. 我们肯定是不能一个个枚举区间的...而且这个复杂度下,也就大概$O(n)$或$ ...

  7. RobotFramework自动化测试框架(1)- RobotFramework简介

    对于RobotFramework自动化测试框架,我这里会从三个单元进行阐述,希望能对你有帮助. RobotFramework简介 RobotFramework是什么? Robotframework 是 ...

  8. mybaits 连接数据库汉字保存乱码??

    查看数据库连接地址: jdbc.url=jdbc:mysql://localhost:3306/az?useUnicode=true&characterEncoding=utf-8 多了一个a ...

  9. JAVA常用知识总结(一)

    try catch finally 的详细用法: public static int testBasic(){ int i = 1; try{ i++; System.out.println(&quo ...

  10. SpringCloud开发学习总结(七)—— 声明式服务调用Feign(二)

    参数绑定 在上一章的示例中,我们使用Spring Cloud Feign实现的是一个不带参数的REST服务绑定.然而现实系统中的各种业务接口要比它复杂得多,我们有时会在HTTP的各个位置传入各种不同类 ...