3.Spring Cloud初相识--------Ribbon客户端负载均衡
前言:
在生产环境中,未避免单点故障,每个微服务都会做高可用部署。
通白的说,就是每一个一模一样的服务会根据需求提供多分在多台机器上。
那么在大并发的情况下,如何分配服务可以快速得到响应,就成为了我们要解决的问题。
Ribbon就是一款优秀的客户端负载均衡机制。
什么是客户端负载均衡呢?
就是由服务的消费方来设定负载均衡策略,选择服务。
就像我们去超市买东西进行结账时,选择人少的柜台排队。
我们是消费方,排哪个队有我们自己决定。
配置测试环境:
1.配置三台服务提供者机器
2.修改端口号分别为:8001,8002,8003
3.修改HelloController返回字符串内容
(1)8001:
package com.xm.cloud.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello Spring Cloud! 001号机器";
}
}
(2)8002:
package com.xm.cloud.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello Spring Cloud! 002号机器";
}
}
(3)8003:
package com.xm.cloud.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello Spring Cloud! 003号机器";
}
}
4.修改消费服务HelloController
package com.xm.cloud.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class HelloController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
public List<String> sayHello() {
List<String> list = new ArrayList<>();
for(int i=0;i<30;i++) {
list.add(restTemplate.getForObject("http://CL-HELLO-PRODUCER/hello", String.class));
}
return list;
}
}
实践:
1.测试默认的负载均衡策略(轮询:RoundRobinRule):
(1)默认cfg:
package com.xm.cloud.cfg;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
(2)测试:localhost:8080/hello
| 0 | "Hello Spring Cloud! 002号机器" |
|---|---|
| 1 | "Hello Spring Cloud! 003号机器" |
| 2 | "Hello Spring Cloud! 001号机器" |
| 3 | "Hello Spring Cloud! 002号机器" |
| 4 | "Hello Spring Cloud! 003号机器" |
| 5 | "Hello Spring Cloud! 001号机器" |
| 6 | "Hello Spring Cloud! 002号机器" |
| 7 | "Hello Spring Cloud! 003号机器" |
| 8 | "Hello Spring Cloud! 001号机器" |
| 9 | "Hello Spring Cloud! 002号机器" |
| 10 | "Hello Spring Cloud! 003号机器" |
| 11 | "Hello Spring Cloud! 001号机器" |
| 12 | "Hello Spring Cloud! 002号机器" |
| 13 | "Hello Spring Cloud! 003号机器" |
| 14 | "Hello Spring Cloud! 001号机器" |
| 15 | "Hello Spring Cloud! 002号机器" |
| 16 | "Hello Spring Cloud! 003号机器" |
| 17 | "Hello Spring Cloud! 001号机器" |
| 18 | "Hello Spring Cloud! 002号机器" |
| 19 | "Hello Spring Cloud! 003号机器" |
| 20 | "Hello Spring Cloud! 001号机器" |
| 21 | "Hello Spring Cloud! 002号机器" |
| 22 | "Hello Spring Cloud! 003号机器" |
| 23 | "Hello Spring Cloud! 001号机器" |
| 24 | "Hello Spring Cloud! 002号机器" |
| 25 | "Hello Spring Cloud! 003号机器" |
| 26 | "Hello Spring Cloud! 001号机器" |
| 27 | "Hello Spring Cloud! 002号机器" |
| 28 | "Hello Spring Cloud! 003号机器" |
| 29 | "Hello Spring Cloud! 001号机器" |
2.测试随机策略(RandomRule):
(1)修改cfg:
package com.xm.cloud.cfg;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public IRule myRule() {
return new RandomRule();
}
}
(2)测试结果:
| 0 | "Hello Spring Cloud! 002号机器" |
|---|---|
| 1 | "Hello Spring Cloud! 003号机器" |
| 2 | "Hello Spring Cloud! 003号机器" |
| 3 | "Hello Spring Cloud! 002号机器" |
| 4 | "Hello Spring Cloud! 003号机器" |
| 5 | "Hello Spring Cloud! 001号机器" |
| 6 | "Hello Spring Cloud! 001号机器" |
| 7 | "Hello Spring Cloud! 002号机器" |
| 8 | "Hello Spring Cloud! 002号机器" |
| 9 | "Hello Spring Cloud! 002号机器" |
| 10 | "Hello Spring Cloud! 001号机器" |
| 11 | "Hello Spring Cloud! 003号机器" |
| 12 | "Hello Spring Cloud! 002号机器" |
| 13 | "Hello Spring Cloud! 003号机器" |
| 14 | "Hello Spring Cloud! 003号机器" |
| 15 | "Hello Spring Cloud! 002号机器" |
| 16 | "Hello Spring Cloud! 001号机器" |
| 17 | "Hello Spring Cloud! 001号机器" |
| 18 | "Hello Spring Cloud! 002号机器" |
| 19 | "Hello Spring Cloud! 003号机器" |
| 20 | "Hello Spring Cloud! 001号机器" |
| 21 | "Hello Spring Cloud! 003号机器" |
| 22 | "Hello Spring Cloud! 002号机器" |
| 23 | "Hello Spring Cloud! 002号机器" |
| 24 | "Hello Spring Cloud! 003号机器" |
| 25 | "Hello Spring Cloud! 002号机器" |
| 26 | "Hello Spring Cloud! 001号机器" |
| 27 | "Hello Spring Cloud! 001号机器" |
| 28 | "Hello Spring Cloud! 002号机器" |
| 29 | "Hello Spring Cloud! 001号机器" |
3.测试最佳可用策略(最佳可用):
(1)修改cfg:
package com.xm.cloud.cfg;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public IRule myRule() {
return new BestAvailableRule();
}
}
(2)测试结果:
| 0 | "Hello Spring Cloud! 003号机器" |
|---|---|
| 1 | "Hello Spring Cloud! 003号机器" |
| 2 | "Hello Spring Cloud! 003号机器" |
| 3 | "Hello Spring Cloud! 003号机器" |
| 4 | "Hello Spring Cloud! 003号机器" |
| 5 | "Hello Spring Cloud! 003号机器" |
| 6 | "Hello Spring Cloud! 003号机器" |
| 7 | "Hello Spring Cloud! 003号机器" |
| 8 | "Hello Spring Cloud! 003号机器" |
| 9 | "Hello Spring Cloud! 003号机器" |
| 10 | "Hello Spring Cloud! 003号机器" |
| 11 | "Hello Spring Cloud! 003号机器" |
| 12 | "Hello Spring Cloud! 003号机器" |
| 13 | "Hello Spring Cloud! 003号机器" |
| 14 | "Hello Spring Cloud! 003号机器" |
| 15 | "Hello Spring Cloud! 003号机器" |
| 16 | "Hello Spring Cloud! 003号机器" |
| 17 | "Hello Spring Cloud! 003号机器" |
| 18 | "Hello Spring Cloud! 003号机器" |
| 19 | "Hello Spring Cloud! 003号机器" |
| 20 | "Hello Spring Cloud! 003号机器" |
| 21 | "Hello Spring Cloud! 003号机器" |
| 22 | "Hello Spring Cloud! 003号机器" |
| 23 | "Hello Spring Cloud! 003号机器" |
| 24 | "Hello Spring Cloud! 003号机器" |
| 25 | "Hello Spring Cloud! 003号机器" |
| 26 | "Hello Spring Cloud! 003号机器" |
| 27 | "Hello Spring Cloud! 003号机器" |
| 28 | "Hello Spring Cloud! 003号机器" |
| 29 | "Hello Spring Cloud! 003号机器" |
4.测试重试负载均衡策略(RetryRule)
(1)修改cfg:
package com.xm.cloud.cfg;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public IRule myRule() {
return new RetryRule();
}
}
(2)测试结果:
| 0 | "Hello Spring Cloud! 001号机器" |
|---|---|
| 1 | "Hello Spring Cloud! 002号机器" |
| 2 | "Hello Spring Cloud! 003号机器" |
| 3 | "Hello Spring Cloud! 001号机器" |
| 4 | "Hello Spring Cloud! 002号机器" |
| 5 | "Hello Spring Cloud! 003号机器" |
| 6 | "Hello Spring Cloud! 001号机器" |
| 7 | "Hello Spring Cloud! 002号机器" |
| 8 | "Hello Spring Cloud! 003号机器" |
| 9 | "Hello Spring Cloud! 001号机器" |
| 10 | "Hello Spring Cloud! 002号机器" |
| 11 | "Hello Spring Cloud! 003号机器" |
| 12 | "Hello Spring Cloud! 001号机器" |
| 13 | "Hello Spring Cloud! 002号机器" |
| 14 | "Hello Spring Cloud! 003号机器" |
| 15 | "Hello Spring Cloud! 001号机器" |
| 16 | "Hello Spring Cloud! 002号机器" |
| 17 | "Hello Spring Cloud! 003号机器" |
| 18 | "Hello Spring Cloud! 001号机器" |
| 19 | "Hello Spring Cloud! 002号机器" |
| 20 | "Hello Spring Cloud! 003号机器" |
| 21 | "Hello Spring Cloud! 001号机器" |
| 22 | "Hello Spring Cloud! 002号机器" |
| 23 | "Hello Spring Cloud! 003号机器" |
| 24 | "Hello Spring Cloud! 001号机器" |
| 25 | "Hello Spring Cloud! 002号机器" |
| 26 | "Hello Spring Cloud! 003号机器" |
| 27 | "Hello Spring Cloud! 001号机器" |
| 28 | "Hello Spring Cloud! 002号机器" |
| 29 | "Hello Spring Cloud! 003号机器" |
5.规则比较
| 策略 | 介绍 |
|---|---|
| RoundRobinRule | 简单轮询服务列表来选择服务器。它是Ribbon默认的负载均衡规则。 |
| RandomRule | 随机选择一个可用的服务器。 |
| RetryRule | 默认轮询,重试多次失败的机器从轮询列表中淘汰。 |
| BestAvailableRule | 忽略哪些短路的服务器,并选择并发数较低的服务器。 |
| ZoneAvoidanceRule | 以区域可用的服务器为基础进行服务器的选择。使用Zone对服务器进行分类,这个Zone可以理解为一个机房、一个机架等。 |
| WeightedResponseTimeRule | 为每一个服务器赋予一个权重值。服务器响应时间越长,这个服务器的权重就越小。这个规则会随机选择服务器,这个权重值会影响服务器的选择。 |
| AvailabilityFilteringRule | 该策略继承自上面介绍的抽象策略PredicateBasedRule,所以它也继承了“先过滤清单,再轮询选择”的基本处理逻辑。 |
自定义负载均衡策略步骤
1.实现IRule接口
2.cfg注册覆盖默认负载均衡策略
3.Spring Cloud初相识--------Ribbon客户端负载均衡的更多相关文章
- Spring Cloud:使用Ribbon实现负载均衡详解(下)
在上一篇文章(Spring Cloud:使用Ribbon实现负载均衡详解(上))中,我对 Ribbon 做了一个介绍,Ribbon 可以实现直接通过服务名称对服务进行访问.这一篇文章我详细分析一下如何 ...
- Spring Cloud第四篇 | 客户端负载均衡Ribbon
本文是Spring Cloud专栏的第四篇文章,了解前三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...
- Spring Cloud(Dalston.SR5)--Ribbon 中间层负载均衡
Spring Cloud 集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,使用 @LoadBalanced 修饰的 RestTemplate 类拥有了负载均衡功能,在 Sprin ...
- Spring Cloud:使用Ribbon实现负载均衡详解(上)
1. 什么是 Ribbon? Spring Cloud Ribbon 是一套实现客户端负载均衡的工具.注意是客户端,当然也有服务端的负载均衡工具,我们后面再介绍.可以认为 Ribbon 就是一个负载均 ...
- spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...
- spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得【无熔断器】
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 了解了 eureka 服务注册与发现 的3大角色 ,会使用RestTem ...
- springcloud(十二):Ribbon客户端负载均衡介绍
springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...
- SpringBoot(三) - Ribbon客户端负载均衡,Zuul网关,Config配置中心
1.Ribbon客户端负载均衡 1.1 依赖 1.2 配置信息 # feign默认加载了ribbon负载均衡,默认负载均衡机制是:轮询 # 负载均衡机制是添加在消费端(客户端)的,如果改为随机,指定服 ...
- 笔记:Spring Cloud Ribbon 客户端负载均衡
Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix Ribbon 实现,通过Spring Cloud 的封装,可以让我们轻松的将面向服 ...
随机推荐
- Android BitmapFactory.Options
public Bitmap inBitmap 如果设置,解码选项“对象的方法,采取将尝试重用这个位图加载内容时. public int inDensity 使用的位图的象素密度. public boo ...
- mac下 安装tomcat 后项目无法启动以及 错误 找不到或无法加载主类
按照网上的步骤,在mac上安装tomcat后,写个简单的测试类报错:错误 找不到或无法加载主类 Class JavaLaunchHelper is implemented in both /Libra ...
- DockerFile简介以及使用
DockerFile是用来构建docker镜像的构建文件,是有一系列命令和参数构成的脚本 构建的三步骤:编写dockerfile文件→build构建→docker run dockerfile保留字指 ...
- iOS设计模式 - 装饰
iOS设计模式 - 装饰 原理图 说明 1. cocoa框架本身实现了装饰模式(category的方式实现了装饰模式) 2. 装饰模式指的是动态的给一个对象添加一些额外的职责,相对于继承子类来说,装饰 ...
- Java实例---flappy-bird实例解析
第一天: 实现背景图片和小鸟的动态飞行效果 package com.ftl.flappybird.day2; import java.awt.Color;//颜色 Color.class import ...
- 乘风破浪:LeetCode真题_006_ZigZag Conversion
乘风破浪:LeetCode真题_006_ZigZag Conversion 一.前言 到这里我们对基本的问题有了一定的理解,其中字符串的操作一直是一个比较困难的问题,这一点我们需要认真对待,采用合理的 ...
- openresty及lua的随机函数
我们都知道,所谓的随机都是伪随机,随机的结果是由随机算法和随机种子决定的. 所以,当我们没有初始化的时候,如果直接使用math.random(),那么出来的值肯定是每次都一样,因为种子等于0. 因此, ...
- 51nod 1349 最大值
题目看这里 找到每个元素g[i]作为最大值的区间[L,R],那么以他为最大值的区间数有(i-L+1)*(R-i+1)个. 为了加速,以k为最大值的区间数放入H[k],再以此统计一个前缀和,更新入H.那 ...
- 【NOI2008】假面舞会
题目描述 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会. 今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办方会把此编号 ...
- pwnhub_WTP攻击思路--self-xss高级利用
1.self-xss+302跳转构造csrf的利用: 1.login.php 存在跳转2.http://54.223.108.205:23333/login.php?redirecturl=//vps ...