Ribbon简介:
Ribbon:客户端 ,负载均衡、服务调用工具(Load Balance)
Ribbon=负载均衡+RestTemplate调用
工作步骤:
- 第一步先选择EurekaServer,它优先选择在同一个区域内负载较少的Server
- 第二步再根据用户的策略,再从Server取到服务注册列表中选择一个地址。其中Ribbon提供了多中策略,比如轮询、随机和根据响应时间加权。
在80端口Eureka依赖包含的有Ribbon依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
RestTemplate中:getForObject与getForEntity区别:【在80端口的OrderController.java中】
/*postForObject 返回json 与postForEntity 返回ResponseEntity对象 区别*/
@GetMapping("/consumer/payment/postForEntity/create")
public CommonResult<Payment> create1(Payment payment){
return restTemplate.postForEntity(PAYMENT_URL+"/payment/create",payment,CommonResult.class).getBody();
}
@GetMapping("/consumer/payment/getForEntity/{id}")
public CommonResult<Payment> getPayment1(@PathVariable("id") Long id){
ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
if (entity.getStatusCode().is2xxSuccessful()){
return entity.getBody();
}else {
return new CommonResult<Payment>(444,"操作失败");
}
}
Ribbon核心组件IRule
根据特定算法中从服务列表中获取一个要访问的服务。
- 轮询:com.netflix.loadbalancer.RoundRibbonRule【默认】
- 随机:com.netflix.loadbalancer.RandomRule
- 先按照轮询的策略获取服务,如果获得服务失败则在指定时间内会进行重试,获取可用的服务:com.netflix.loadbalancer.RetryRule
- 对轮询的扩展,响应速度越快的实例选择权重越大,越容易被选择:WeightedResponseTimeRule
- 先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务:BestAvailableRule
- 先过滤掉故障实例,再选择并发较小的实例:AvailabilityFilteringRule
- 默认规则,复合判断server所在区域的性能和server的可用性选择服务器
如何替换:【不能与@ComponentScan共用】
(1)修改cloud-consumer-order80
(2)新建package,并新建MyselfRule类
package com.jiao.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyselfRule {
@Bean
public IRule myRule(){
return new RandomRule(); //定义为随机
}
}
(3)主启动类加上注解@RibbonClient
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MyselfRule.class)
(4)测试
Ribbon负载均衡算法
原理
负载均衡算法:rest接口第几次请求数 % 服务器集群总数量 = 实际调用服务器位置下标,每次服务重启后rest接口计数从1开始。
原码
package com.netflix.loadbalancer;
public interface IRule{
public Server choose(Object key);
public void setLoadBalancer(ILoadBalancer lb);
public ILoadBalancer getLoadBalancer();
}
手写:原理+JUC(CAS+自旋锁复习)
(1)7001/7002集群启动、
(2)8001/8002微服务改造、【Controller】
...
@GetMapping(value = "/payment/lb")
public String getPaymentLB(){
return serverPort;
}
...
(3)80客户端改造、【Controller】
- ApplicationContextConfig去掉注解@LoadBalance
- LoadBalancer接口
package com.jiao.springcloud.lb;
import org.springframework.cloud.client.ServiceInstance;
import java.util.List;
public interface LoadBalancer {
ServiceInstance instances(List<ServiceInstance> serviceInstances); //通过serverName找serverPort
}
- MyLb【也就是LoadBalancerImpl】
package com.jiao.springcloud.lb;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Component
public class MyLb implements LoadBalancer{
private AtomicInteger atomicInteger = new AtomicInteger(0); //初始为0
private final int getAndIncrement(){
int current;
int next;
do {
current = this.atomicInteger.get(); //0
next = current >= 2147483647 ? 0 : current + 1;
}while (!this.atomicInteger.compareAndSet(current,next)); //自旋:当前值=期望值:修改值
System.out.println("*****第几次访问next:"+next);
return next;
}
public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
int index = getAndIncrement() % serviceInstances.size();
return serviceInstances.get(index);
}
}
- OrderController
...
@Resource
private LoadBalancer loadBalancer; //自己写的负载均衡算法
@Resource
private DiscoveryClient discoveryClient;
...
@GetMapping(value = "/consumer/payment/lb")
public String getPaymentLB(){
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
if (instances == null || instances.size() <=0){
return null;
}
ServiceInstance serviceInstance = loadBalancer.instances(instances);
URI uri = serviceInstance.getUri();
return restTemplate.getForObject(uri+"payment/lb",String.class);
}
- 测试【http:localhost/consumer/payment/lb】