SpringCloud-Ribbon

1.Ribbon是什么

Spring Cloud Ribbon是基于NetFlix Ribbon实现的一套客户端,负载均衡的工具。

简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法和服务调用。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在篇日志文件中列出Load Balancer(BL)后面的所有机器,Ribbon会自动的帮助你基于某种规则(如轮询,随机等等)去连接这些机器。我们很容易使用Ribbon实现定义的负载均衡算法。

ribbon目前进入了维护期

2.负载均衡是什么

简单的说就是讲用户的请求平摊的分配到多个服务上,从而达到系统的HA(高可用)。
常见的负载均衡软件Nginx,LVS,硬件F5等

3.Ribbon本地负载均衡客户端 VS Nginx服务端负载均衡区别

Nginx是服务器负载均衡,客户端所有请求都会交给nginx,然后由nginx实现请求转发。即负载均衡是由服务端实现的。
Ribbon本地负载均衡,在调用微服务接口时候,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术。

3.1 **进程内LB**
	将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选出一个合适的服务器。
	Ribbon就属于进程内LB,它是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址。
3.2 **集中式LB**
	即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5,也可以是软件,如nginx),由该设施负责把访问请求通过某种策略转发至服务的提供方。

4.Ribbon架构说名

在这里插入图片描述
Ribbon 在工作时分成两步
第一步先选择EurekaServer,它优先选择在同一个区域内负载较少的server.
第二步再根据用户指定的策略,在从server取到的服务注册列表中选择一个地址。
其中Ribbon提供多种策略:如轮询,随机和根据响应时间加权。

Ribbon其实就是一个负载均衡的客户端组件,他可以和其他所需请求的客户端结合使用,和eureka结合知识其中的一个实例。

Ribbon依赖

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

Eureka-client自带ribbon
在这里插入图片描述
4.1 RestTemplate使用
getForEntity(String url,Class responseType)
在这里插入图片描述

4.2 Ribbon的核心组件IRule
IRule根据特定算法从服务列表选取一个要访问的服务
在这里插入图片描述

4.3 如何自定义算法规则

4.3.1 注意警告(官方)
官方文档明确警告:自定义配置类不能放在@ComponentScan所扫描的当前包下及其子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,达不到特殊化定制的目的。
1.建一个自己的算法规则类

package com.atguigu.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();//定义为随机
    }
}

2.添加@RibbonClient

package com.atguigu.springcloud;

import com.atguigu.myrule.MySelfRule;
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;

@EnableEurekaClient
@SpringBootApplication
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }

}

3.测试

5.Ribbon负载均衡算法

5.1 原理
在这里插入图片描述

package com.atguigu.springcloud.lb.impl;

import com.atguigu.springcloud.lb.LoadBalancer;
import com.netflix.loadbalancer.IRule;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class LoadBalancerImpl implements LoadBalancer {

    private AtomicInteger atomicInteger = new AtomicInteger(0);

    public final int getAndIncrement() {
        int current;
        int next;
        do {
            current = this.atomicInteger.get();
            next = current >= 2147483647 ? 0 : current + 1;
        } while (!this.atomicInteger.compareAndSet(current, next));
        System.out.println("当前是第几次调用:" + next);
        return next;
    }

    @Override
    public ServiceInstance instance(List<ServiceInstance> serviceInstanceList) {
        int index = getAndIncrement() % serviceInstanceList.size();
        return serviceInstanceList.get(index);
    }
}

需要自己创建接口LoadBalancer,不赘述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值