第七节:Ribbon【负载均衡】

Ribbon简介:

Ribbon:客户端 ,负载均衡、服务调用工具(Load Balance)

Ribbon=负载均衡+RestTemplate调用

7.1

工作步骤:

  1. 第一步先选择EurekaServer,它优先选择在同一个区域内负载较少的Server
  2. 第二步再根据用户的策略,再从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,"操作失败");
    }
}

7.2

Ribbon核心组件IRule

根据特定算法中从服务列表中获取一个要访问的服务。

7.3

  1. 轮询:com.netflix.loadbalancer.RoundRibbonRule【默认】
  2. 随机:com.netflix.loadbalancer.RandomRule
  3. 先按照轮询的策略获取服务,如果获得服务失败则在指定时间内会进行重试,获取可用的服务:com.netflix.loadbalancer.RetryRule
  4. 对轮询的扩展,响应速度越快的实例选择权重越大,越容易被选择:WeightedResponseTimeRule
  5. 先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务:BestAvailableRule
  6. 先过滤掉故障实例,再选择并发较小的实例:AvailabilityFilteringRule
  7. 默认规则,复合判断server所在区域的性能和server的可用性选择服务器

如何替换:【不能与@ComponentScan共用】

(1)修改cloud-consumer-order80

(2)新建package,并新建MyselfRule类

7.4

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接口

7.5

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】

7.7

7.6

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值