Spring Cloud Ribbon源码分析(一)

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
简介: Spring Cloud Ribbon源码分析(一)

一、Ribbon的作用


image.png


1,解析配置中的服务器列表



2,基于负载均衡算法来实现请求的分发


二、Ribbon做负载均衡的两种方式


1,LoadBlancerClient两种方式


image.png


2,注解方式@LoadBlancer


image.png


三、Ribbon做负载均衡的两种方式初始化


image.png


从前台代码的视角LoadBalancerClient作为入口


1,从LoadBalancerClient接口,找到其的实现类RibbonLoadBalancerClient;

2,在RibbonAutoConfiguration中,可以看到用@Bean实现RibbonLoadClient自动装配,

3,在RibbonAutoConfiguration中,同时根据@AutoConfigureBefore({LoadBalancerAutoConfiguration.class, AsyncLoadBalancerAutoConfiguration.class}),说明LoadBalancerAutoConfiguration是在RibbonAutoConfiguration之前加载的。


从@LoadBalanced的视角作为入口


1,从@LoadBalanced注解进入,发现其有@Qualifier,

在LoadBalancerAutoConfiguration中对于 @LoadBalanced private List restTemplates的自动注入,(@LoadBalanced的用法,对于A接口的实现类A1,A2,A3如果都加上@LoadBalanced;那么可以通过对List加上@LoadBalanced以及@Autowired,可以注入所有的实现类;如果对于接口A,只想注入一个类A,那么用@Qualifier(“a”)))


LoadBalancerAutoConfiguration的自动装配过程


1,通过依赖注入导入LoadBalancerInterceptor拦截器

2,RestTemplateCustomizer的作用是对修饰了@LoadBalanced注解的RestTemplate实例添加LoadBalancerInterceptor拦截器

3,SmartInitializingSingleton的作用是遍历每一个RestTemplate,用RestTemplateCustomizer定制所有被@LoadBalancer注解修饰的RestTemplate实例。

四,RestTemplate发起请求,获取IBalance以及获得Server的过程


image.png


RestTemplate发起请求的过程

1,获取负载均衡器,

2,通过负载均衡器中配置的默认负载均衡算法挑选一个合适的Server


RestTemplate.getObject()发起请求之后
1,首先会到达拦截器LoadBalancerInterceptor,调用LoadBalancerInterceptor.intercept
2,LoadBalancerClient.execute();  调用接口的execute
3,RibbonLoadBalancerClient.execute();   调用Ribbon的execute
4,获取负载均衡器 ILoadBalancer loadBalancer = this.getLoadBalancer(serviceId);
5,clientFactory.getLoadBalancer(serviceId);利用工厂模式,根据serverId获取IBalanced
6,RibbonClientConfiguration自动装配中有RibbonClientConfiguration=ZoneAwareLoadBalancer 默认的轮询策略
7,RibbonLoadBalancerClient.getServer(loadBalancer, hint); 获取服务器
8,loadBalancer.chooseServer(hint != null ? hint : "default"); 选择服务器
9,BaseLoadBalancer.chooseServer();
10,rule.choose(key)
11,PredicateBasedRule.choose(key)
12,AbstractServerPredicate.chooseRoundRobinAfterFiltering(key);
13.AbstractServerPredicate.incrementAndGetModulo()默认的轮询算法 


通过上边的步骤已经得到了 server地址,重构URL


image.png


1,request.apply(serviceInstance)
2,AsyncClientHttpRequestExecution.executeAsync()
3,
4,
5,


五,前一阶段部分总结


1,首先请求会有一个拦截器,

2,拦截器怎么做到,讲到了初始化的过程,初始化是基于自动装配进行

3,初始化涉及到两个知识点,一个是@qualifer,其相当于打了个标记,这个标记针对加了LoadBalancer注解的RestTemplate进行拦截,针对需要负载均衡的RestTemplate进行拦截

4,接着拦截器的请求就会进入拦截器的方法intercept()

5,进入拦截方法后顺着调用链往下走,

6,接着获取负载均衡器,利用工厂模式根据ServerID获取ILandbalancer的实例,使用RibbonClientConfiguration自动装配获得ZoneAwareLoadBalancer

7,将获得的ZoneAwareLoadBalancer传入到getServer(loadblance,hint);

getServer(loadblance,hint)会调用LoadBalancer.chooseServer()

LoadBalancer.chooseServer会使用上一步得到的负载均衡器

BaseLoadBalancer.chooseServer()会调用rule.chooseServer(key);

rule.chooseServer(key),rule是在RibbonClientConfiguration自动装配获得ZoneAwareLoadRule

接着在PredicateBasedRule中调用choose,默认采用轮询算法做负载均衡,也就是在 AbstractServerPredicate中调用incrementAndGetModulo()

8,获取到server之后,最后一个阶段就是对URL进行重构

AsyncLoadBalancerInterceptor调用intercept方法


通过一系列的调用链 调用RibbonLoadBalancerClient中reconstructURI重构URL

发起异步远程调用AsyncClientHttpRequestExecution.executeAsync


image.png

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
17天前
|
负载均衡 Java API
Java一分钟之-Spring Cloud OpenFeign:声明式服务调用
【6月更文挑战第9天】Spring Cloud OpenFeign是声明式服务调用库,简化了微服务间调用。通过动态代理,它允许开发者用Java接口调用HTTP服务,支持服务发现、负载均衡。本文介绍了OpenFeign的基本概念,展示了如何添加依赖、开启客户端和定义服务接口。还讨论了接口调用失败、超时重试和日志配置等问题及其解决方案,并提供了自定义Feign配置的代码示例。通过学习,读者可以更好地在微服务架构中使用OpenFeign进行服务通信。
181 4
|
6天前
|
存储 安全 Java
Spring Security 6.x OAuth2登录认证源码分析
上一篇介绍了Spring Security框架中身份认证的架构设计,本篇就OAuth2客户端登录认证的实现源码做一些分析。
31 2
Spring Security 6.x OAuth2登录认证源码分析
|
6天前
|
Java 开发者 Sentinel
Spring Cloud系列——使用Sentinel进行微服务保护
Spring Cloud系列——使用Sentinel进行微服务保护
23 5
|
9天前
|
Java 测试技术 持续交付
Java一分钟之-Spring Cloud Contract:契约测试
【6月更文挑战第16天】Spring Cloud Contract是微服务契约测试框架,通过DSL定义接口行为,使用WireMock生成存根进行独立开发验证。常见问题包括契约编写不清晰、未集成到CI/CD和契约版本控制混乱。例如,定义一个`GET /greeting`返回JSON响应的契约,Spring Cloud Contract会自动生成测试代码,帮助确保服务间接口一致性,提升开发效率和系统稳定性。
32 7
|
5天前
|
安全 Java 数据安全/隐私保护
在Spring Cloud中实现单点登录(Single Sign-On, SSO)
在Spring Cloud中实现单点登录(Single Sign-On, SSO)
32 2
|
5天前
|
监控 Java Sentinel
Spring Cloud微服务架构
Spring Cloud微服务架构
24 1
|
11天前
|
Java Nacos 数据格式
Spring Cloud Nacos 详解:服务注册与发现及配置管理平台
Spring Cloud Nacos 详解:服务注册与发现及配置管理平台
37 3
|
11天前
|
存储 Java Spring
Spring IOC 源码分析之深入理解 IOC
Spring IOC 源码分析之深入理解 IOC
29 2
|
11天前
|
Java 数据库 开发者
深入解析 Spring Cloud Seata:分布式事务的全面指南
深入解析 Spring Cloud Seata:分布式事务的全面指南
30 1
|
11天前
|
监控 Java API
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
20 0
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南