所有代码都在github上:https://github.com/demonruin/cloud2020/tree/master
简单的说,Ribbon 是 Netflix 发布的开源项目,主要功能是提供 客户端的复杂算法和服务调用。Ribbon 客户端组件提供一系列完善的配置项如超时、重试等。简单的说,就是配置文件中列出 load Balancer (简称 LB)后面所有的机器,Ribbon 会自动的帮助你基于某种规则(如简单轮询,随机链接等)去链接这些机器。我们很容易使用 Ribbon 自定义的负载均衡算法。
LB(负载均衡)分为 集中式LB 和 进程内LB
集中式LB
即在服务的消费方和提供方之间使用独立的LB 设施(可以是硬件,如F5, 也可以是软件如 Nginx ), 由该设置负责把访问请求通过某种策略转发至服务的提供方
进程内 LB
将 LB 逻辑集成到消费方,消费方从服务注册中心获取有哪些地址可用,然后自己再从这些地址中选择一个适合的服务器。Ribbon 就属于进程内 LB,它只是一个类库,集成于消费方进程,消费方通过它阿莱获取服务提供方的地址。
总结:Ribbon 其实就是一个软负载均衡的客户端组件,他可以和其他所需请求的客户端结合使用,和eureka结合只是其中的一个实例
Ribbon 在工作时分为两步:
第一步先选择 EurekaServer, 它优先选择在同一个区域呢负载较少的Server
第二步在根据用户执行的策略,在从server 取到的服务注册列表中选择一个地址。
其中 Ribbon 提供了多种策略:比如轮询、随机和更具响应时间加权。
其中策略有以下七种:
com.netflix.loadbalancer.RoundRobinRule | 轮询 |
com.netflix.loadbalancer.RandomRule | 随机 |
com.netflix.loadbalancer.RetryRule | 先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内会进行重试 |
WeightedResponseTimeRule | 对RoundRobinRule 的拓展,响应速度越快的实例选择权重越大,越容易被选择 |
BestAvailableRule | 会先过滤掉由于多次访问故障而处断路器跳闸状态的服务,然后选择一个并发量最小的服务 |
AvailabilityFilteringRule | 先过滤掉故障实例,在选择并发较小的实例 |
ZoneAvoidanceRule | 默认规则,符合判断Server 所在区域的性能和 Server 的可用性选择服务器 |
而IRule可以根据特定算法从中选取一个要方位的服务,下面就举例通过Ribbon的核心组件IRule实现自定义策略选择:
注意:
官方文档明确给出了警告:
这个自定义配置类不能放在 @CommpomentScan 所扫描的当前包下以及子包下,否则我们自定义的这个规则类会被所有的 Ribbon 客户端共享,达不到特殊定制化的目的。
1、新建package--->com.king.myrule与启动类包分开,并新建myRule配置类
package com.king.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* created by king on 2020/4/15 10:56 上午
*/
@Configuration
public class MyRule {
@Bean
public IRule getMyRule(){
return new RandomRule();
}
}
2、主启动类添加@RibbonClient注解,RibbonClient中的name值要和配置中心的服务名保持一致,大小写注意,configuration值为 自定义myrule类
package com.king.springcloud;
import com.king.myrule.MyRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
/**
* created by king on 2020/4/13 3:41 下午
* RibbonClient中的name值要和配置中心的服务名保持一致,大小写注意
*/
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name="CLOUD-PAYMENT-SERVICE",configuration = MyRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
3、然后通过请求测试即可,附一个测试请求demo
package com.king.springcloud.controller;
import com.king.springcloud.entities.CommonResult;
import com.king.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* created by king on 2020/4/13 3:53 下午
*/
@RestController
@Slf4j
public class OrderController {
private static final String PAYMENT_URL_PRE="http://CLOUD-PAYMENT-SERVICE/";//
@Resource
private RestTemplate restTemplate;
@GetMapping(value="/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL_PRE+"/payment/get/"+id,CommonResult.class);
}
@GetMapping(value="/consumer/payment/getEntity/{id}")
public CommonResult<Payment> getPaymentEntity(@PathVariable("id") Long id){
ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_URL_PRE + "/payment/get/" + id, CommonResult.class);
log.info("forEntity实体内容:"+forEntity.toString());
if(forEntity.getStatusCode().is2xxSuccessful()){
return forEntity.getBody();
}else{
return new CommonResult<Payment>(444,"请求失败");
}
}
}