【ribbon】Ribbon的使用与原理

负载均衡介绍

负载均衡(Load Balance),其含义就是指将负载(工作任务)进行平衡、分摊到多个操作单元上进行运行,例如FTP服务器、Web服务器、企业核心应用服务器和其它主要任务服务器等,从而协同完成工作任务。

思考: 如果有多个provider实例,consumer应该如何调用呢?

目前主流的负载均衡方案分为以下两种:

  • 服务端的负载均衡:集中式负载均衡,在消费者和服务提供方中间使用独立的代理方式进行负载,有基于硬件的(比如F5),也有基于软件的(比如Nginx、LVS)。
  • 客户端的负载均衡:客户端根据自己的请求情况做负载均衡,Ribbon就属于客户端自己做负载均衡。

客户端的负载均衡

例如spring cloud中的ribbon,客户端会有一个服务器地址列表,在发送请求前通过负载均衡算法选择一个服务器,然后进行访问,这是客户端负载均衡;即在客户端就进行负载均衡算法分配。

服务端的负载均衡

例如Nginx,通过Nginx进行负载均衡,先发送请求,然后通过负载均衡算法,在多个服务器之间选择一个进行访问;即在服务器端再进行负载均衡算法分配。

常见负载均衡算法

  • 随机:通过随机选择一个服务进行执行,一般这种方式使用较少;
  • 轮询:负载均衡默认实现方式,请求来之后排队处理;
  • 加权轮询:通过对服务器性能的分型,给高配置,低负载的服务器分配更高的权重,均衡各个服务器的压力;
  • 地址hash:通过客户端请求的地址的hash值取模映射进行服务器调度,可尽量保证同一个客户的请求都到同一个服务器
  • 最小连接数:即使请求均衡了,压力不一定会均衡,最小连接数法就是根据服务器的情况,比如请求积压数等参数,将请求分配到当前压力最小的服务器上。

Spring Cloud Alibaba整合Ribbon快速开始

Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端的负载均衡工具,Ribbon客户端组件提供一系列的完善的配置,如超时,重试等。通过Load Balancer获取到服务提供的所有机器实例,Ribbon会自动基于某种规则(轮询,随机)去调用这些服务。Ribbon也可以实现我们自己的负载均衡算法。

由于spring-cloud-starter-alibaba-nacos-discovery依赖了ribbon的依赖,所以我们不再需要单独引入ribbon的依赖了,如下面:

<!--添加ribbon的依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

注入RestTemplate时需要添加@LoadBalanced注解,让RestTemplate在请求时拥有客户端负载均衡的能力

package com.morris.user.config;

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 RestConfig {

    /**
     * 默认的RestTemplate,不带负载均衡
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     * 有负责均衡能力的RestTemplate
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate2() {
        return new RestTemplate();
    }
}

RestTemplate的使用:

package com.morris.user.controller;

import com.morris.user.entity.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("user")
public class RestTemplateController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private RestTemplate restTemplate2;

    @GetMapping("findOrderByUserId")
    public List<Order> findOrderByUserId(Long userId) {
        Order[] orders = restTemplate.getForObject("http://127.0.0.1:8020/order/findOrderByUserId?userId=", Order[].class, userId);
        return Arrays.asList(orders);
    }

    @GetMapping("findOrderByUserId2")
    public List<Order> findOrderByUserId2(Long userId) {
        Order[] orders = restTemplate2.getForObject("http://order-service/order/findOrderByUserId?userId=", Order[].class, userId);
        return Arrays.asList(orders);
    }

}

RestTemplate使用时,如果不带负载均衡时URL中的host直接填的是IP或者域名,使用负载均衡时host填的服务提供者在注册中心注册的微服务名。

模拟Ribbon实现负载均衡

为RestTemplate添加ClientHttpRequestInterceptor,拦截器负责从注册中心注册的微服务中轮询选取一个服务,改写URL中的服务名为具体的IP和端口。

package com.morris.user.config;

import lombok.SneakyThrows;
import org.checkerframework.checker.units.qual.A;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.web.client.RestTemplate;

import javax.annotation.RegEx;
import javax.annotation.Resource;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

@Configuration
public class MyRibbonConfig {

    @Resource
    private DiscoveryClient discoveryClient;

    private final AtomicInteger counter = new AtomicInteger();

    /**
     * 默认的RestTemplate,不带负载均衡
     * @return
     */
    @Bean
    public RestTemplate restTemplate3() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(Collections.singletonList((request, body, execution) -> {
            String host = request.getURI().getHost();
            List<ServiceInstance> instances = discoveryClient.getInstances(host);
            int count = counter.incrementAndGet();
            int index = count % instances.size();
            ServiceInstance serviceInstance = instances.get(index);
            RequestWrapper newRequest = new RequestWrapper(request, serviceInstance.getHost(), serviceInstance.getPort());
            return execution.execute(newRequest, body);
        }));
        return restTemplate;
    }

    private static class RequestWrapper extends HttpRequestWrapper {

        private final String host;

        private final int port;

        public RequestWrapper(HttpRequest request, String host, int port) {
            super(request);
            this.host = host;
            this.port = port;
        }

        @SneakyThrows
        @Override
        public URI getURI() {
            URI oldUri = super.getURI();
            return new URI(oldUri.getScheme(), oldUri.getUserInfo(), host, port, oldUri.getPath(), oldUri.getQuery(), oldUri.getFragment());
        }
    }

}

Ribbon内核源码分析

@LoadBalanced注解原理

主要关注@LoadBalanced注解上有个@Qualifier注解,使用了Spring中限定符。

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Qualifier
public @interface LoadBalanced {
}

LoadBalancerAutoConfiguration

LoadBalancerAutoConfiguration注入了2个重要的Bean:

  • SmartInitializingSingleton
  • LoadBalancerInterceptor

@LoadBalanced使用了@Qualifier,Spring中@Qualifier用于筛选限定注入Bean。@LoadBalanced利用@Qualifier作为restTemplates注入的筛选条件,筛选出具有负载均衡标识的RestTemplate。

被@LoadBalanced注解的restTemplate会被定制,添加LoadBalancerInterceptor拦截器。

SmartInitializingSingleton是在所有的bean都实例化完成之后才会调用的,所以在bean的实例化期间使用@LoadBalanced修饰的restTemplate是不具备负载均衡作用的,比如在项目的启动过程中要使用调用某个微服务,此时RestTemplate是不具备负载均衡作用的。

public class LoadBalancerAutoConfiguration {

	@LoadBalanced
	@Autowired(required = false)
	private List<RestTemplate> restTemplates = Collections.emptyList();

	@Autowired(required = false)
	private List<LoadBalancerRequestTransformer> transformers = Collections.emptyList();

	@Bean
	public SmartInitializingSingleton loadBalancedRestTemplateInitializerDeprecated(
			final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) {
		return () -> restTemplateCustomizers.ifAvailable(customizers -> {
			for (RestTemplate restTemplate : LoadBalancerAutoConfiguration.this.restTemplates) {
				for (RestTemplateCustomizer customizer : customizers) {
					customizer.customize(restTemplate);
				}
			}
		});
	}

	@Configuration(proxyBeanMethods = false)
	@ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
	static class LoadBalancerInterceptorConfig {

		@Bean
		public LoadBalancerInterceptor loadBalancerInterceptor(
				LoadBalancerClient loadBalancerClient,
				LoadBalancerRequestFactory requestFactory) {
			return new LoadBalancerInterceptor(loadBalancerClient, requestFactory);
		}

		@Bean
		@ConditionalOnMissingBean
		public RestTemplateCustomizer restTemplateCustomizer(
				final LoadBalancerInterceptor loadBalancerInterceptor) {
			return restTemplate -> {
				List<ClientHttpRequestInterceptor> list = new ArrayList<>(
						restTemplate.getInterceptors());
				list.add(loadBalancerInterceptor);
				restTemplate.setInterceptors(list);
			};
		}

	}
}

SmartInitializingSingleton会获取容器中所有加了@LoadBalanced注解的RestTemplate,然后为这些RestTemplate逐个添加LoadBalancerInterceptor。

如果不使用@LoadBalanced注解,也可以通过添加LoadBalancerInterceptor拦截器让restTemplate起到负载均衡器的作用,这种方式就可以在启动的过程中使用RestTemplate的负载均衡功能。

@Bean
public RestTemplate restTemplate(LoadBalancerInterceptor loadBalancerInterceptor) {
    RestTemplate restTemplate = new RestTemplate();
    //注入loadBalancerInterceptor拦截器
    restTemplate.setInterceptors(Arrays.asList(loadBalancerInterceptor));
    return restTemplate;
}

LoadBalancerInterceptor拦截器

LoadBalancerInterceptor主要使用RibbonLoadBalancerClient来根据不同的负载均衡算法选择实例,然后使用LoadBalancerRequestFactory来对请求进行改写,与我们手写的Ribbon功能类似。

org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor#intercept

public ClientHttpResponse intercept(final HttpRequest request, final byte[] body,
        final ClientHttpRequestExecution execution) throws IOException {
    final URI originalUri = request.getURI();
    String serviceName = originalUri.getHost();
    Assert.state(serviceName != null,
            "Request URI does not contain a valid hostname: " + originalUri);
    return this.loadBalancer.execute(serviceName,
            this.requestFactory.createRequest(request, body, execution));
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/44653.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

[Cotex-M3学习教程]-0.1-Cortex-M3概述

目录 1 Cortex-M3概述 1.1 ARM 处理器 1.2 cortex-M3介绍 1.3 cortex-M3结构概览图 1.4 cortex-M3组件 1.4.1 内核系统 1.4.2 NVIC 1.4.3 寄存器组 控制寄存器&#xff08;CONTROL&#xff09; 程序计数寄存器&#xff08;PC:R15&#xff09; 堆栈指针寄存器&#xf…

Raki的读paper小记:LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS

Abstract&Introduction&Related Work 研究任务 对大模型进行部分微调 已有方法和相关工作 现有技术通常通过扩展模型深度引入推理延迟&#xff08;Houlsby 等人&#xff0c;2019&#xff1b;Rebuffi 等人&#xff0c;2017&#xff09;&#xff0c;或通过减少模型可用序…

28.JavaWeb-Elasticsearch

1.Elasticsearch概述 Elasticsearch 是一个分布式的全文检索引擎。采用Java语言开发&#xff0c;基于Apache协议的开源项目&#xff0c;具有实时搜索&#xff0c;稳定&#xff0c;可靠&#xff0c;快速的特点。 1.1 全文检索引擎 分为通用搜索引擎&#xff08;百度、谷歌&…

基于Java+SpringBoot+vue前后端分离在线商城系统设计实现

博主介绍&#xff1a;✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专…

java项目之人事管理系统(ssm+mysql+jsp)

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于ssm的人事管理系统。技术交流和部署相关看文章末尾&#xff01; 开发环境&#xff1a; 后端&#xff1a; 开发语言&#xff1a;Java 框架&…

【MySQL进阶(一)】MySQL在Linux中的配置信息和数据备份工具

MySQL在Linux中安装的话可以看这篇博客&#xff1a;MySQL在Linux中的安装&#xff0c;我觉得总结的很好。 my.cnf 中的配置信息 当 MySQL 启动的时候&#xff0c;会从安装目录中加载软件数据&#xff0c;即使用 mysqld 工具初始化设置的 --basedir&#xff0c;会从数据目录中…

【iOS】—— block,KVC,KVO,Category等问题解答

文章目录 block1.block的原理是怎样的&#xff1f;本质是什么&#xff1f;2.__block的作用是什么&#xff1f;有什么使用注意点&#xff1f;3.block的属性修饰词为什么是copy&#xff1f;使用block有哪些使用注意&#xff1f;4.block在修改NSMutableArray&#xff0c;需不需要添…

不止工具:音视频开发「利器」的新机遇

Boxing的制胜关键是快、准、稳&#xff0c;与“音视频开发”有异曲同工之妙。 数字化浪潮席卷、视频化形态加速、终端性能挑战加剧、端侧算力遭遇瓶颈...... 是否存在一种可能性&#xff0c;让所有企业从复杂的音视频开发工程中抽身&#xff0c;重新回归业务本身&#xff1f; …

消息队列总结(3)- RabbitMQ Kafka RocketMQ高可用方案

目录 1. 什么是高可用&#xff1f; 1.1 常见的高可用方法 1.2 消息队列的高可用 2. RabbitMQ的高可用方案 2.1 镜像队列 2.2 消息生产的确认机制 2.3 消息的持久化 3. Kafka的高可用方案 3.1 消息备份 3.2 ISR & IEO & HW 3.3 消息生产的确认机制 4. Rocke…

微信小程序开发使用echarts报错Cannot read property ‘getAttribute‘ of undefined

如图&#xff0c;我在小程序圈中的区域渲染echarts图标报错了&#xff0c;报错提示Cannot read property getAttribute of undefined 这里的canvas &#xff0c;width ,height,dpr获取为 undefined 分析问题&#xff1a; 初始化图表时传递错误的参数&#xff1a; 在 onShow 生…

Vue第四篇:html和js基础知识查漏补缺

1、a标签 定义超链接&#xff0c;用于从一个页面链接到另一个页面 target属性&#xff1a;打开目标URL的方式&#xff0c;_top为再当前窗口打开&#xff0c;_blank为新窗口打开 2、span标签 对文档中的行内元素进行组合&#xff0c;它提供了一种将文本的一部分或者文档的一部分…

MySQL 数据抽稀 每分钟取一条

假如原始数据为每5秒一个数据&#xff0c;现在想展示为每4分钟一条数据&#xff0c;先按照分钟数把除以4余数为0的行选出来&#xff0c;在按照 年月日 时分&#xff0c;做组内排序&#xff08;窗函数ROW_NUMBER&#xff09;&#xff0c;最后再拿出序号为1的行。 WITH data_01 …

了解Unity编辑器之组件篇UI(一)

UI组件&#xff1a;提供了用户交互&#xff0c;信息展示&#xff0c;用户导航等功能 一、Button&#xff1a;用于响应用户的点击事件 1.Interactable&#xff08;可交互&#xff09;&#xff1a;该属性控制按钮是否可以与用户交互&#xff0c;如果禁用则按钮无法被点击。可以通…

go使用gin结合jwt做登录功能

1、安装gin go get -u github.com/gin-gonic/gin 2、安装session go get github.com/gin-contrib/sessions 3、安装JWT鉴权 go get "github.com/golang-jwt/jwt/v4" 4、创建一个jwt的工具文件 package utilsimport ("errors""github.com/golan…

ubuntu挂载ext4文件系统

文章目录 1.虚拟机分配10G磁盘用来挂载ext4文件系统2.磁盘分区3.创建文件系统4.挂载文件系统5.卸载文件系统6.使用ior测试ext4三种日志模式&#xff08;1&#xff09;ordered&#xff08;2&#xff09;journal&#xff08;3&#xff09;writeback 1.虚拟机分配10G磁盘用来挂载e…

Flutter动画库:animations(路由过渡动画或者页面切换动画)

animations animations 是一个 Flutter 库&#xff0c;它提供了一组用于创建动画效果的工具和组件。这个库的核心重点是路由过渡动画或者页面切换动画 地址 https://pub-web.flutter-io.cn/packages/animations 安装 flutter pub add animations看了下官方文档和官方例子&a…

【深度学习】【Image Inpainting】Free-Form Image Inpainting with Gated Convolution

模型&#xff1a;DeepFillv2 (CVPR’2019) 论文&#xff1a;https://arxiv.org/abs/1806.03589 代码&#xff1a;https://github.com/JiahuiYu/generative_inpainting 文章目录 效果AbstractIntroductionRelated WorkAutomatic Image InpaintingGuided Image Inpainting and Sy…

第1章 获取数据库中的数据

CoreShop源程序是以数据库优先进行定义的&#xff0c;所以其本身不包含代码优先的定义&#xff0c;但本从更习惯于代码优先&#xff0c;所以为其定义了代码优先的定义。 1 CoreCms.Net.Model.Entities.SysRole using SqlSugar; using System.ComponentModel.DataAnnotations…

三十章:Segmenter:Transformer for Semantic Segmentation ——分割器:用于语义分割的Transformer

0.摘要 图像分割在单个图像块的级别上经常存在歧义&#xff0c;并需要上下文信息来达到标签一致性。在本文中&#xff0c;我们介绍了一种用于语义分割的Transformer模型- Segmenter。与基于卷积的方法相比&#xff0c;我们的方法允许在第一层和整个网络中对全局上下文进行建模。…

《PyTorch深度学习实践》

文章目录 1.线性模型2.梯度下降算法3.反向传播3.1原理3.2Tensor in PyTorch 4.用PyTorch实现线性模型 1.线性模型 2.梯度下降算法 # 梯度下降x_data [1.0,2.0,3.0] y_data [2.0,4.0,6.0]w 3.0def forward(x):return x*w# 损失函数 def cost(xs,ys):cost 0for x,y in zip(x…