【云原生&微服务>SCG网关篇十四】Spring Cloud Gateway如何实现负载均衡

网友投稿 265 2022-08-24

【云原生&微服务>SCG网关篇十四】Spring Cloud Gateway如何实现负载均衡

文章目录

​​一、前言​​​​二、调试程序信息​​​​三、Gateway对请求做负载均衡分析​​

​​请求负载均衡 <-- ReactiveLoadBalancerClientFilter​​

​​总结​​

一、前言

至此微服务网关系列文章已出:

​​【云原生&微服务>SCG网关篇一】为什么要有网关、生产环境如何选择网关​​​​云原生&微服务>SCG网关篇二】生产上那些灰度发布方式​​​​【云原生&微服务>SCG网关篇三】Spring Cloud Gateway是什么、详细使用案例​​​​云原生&微服务>SCG网关篇四】Spring Cloud Gateway内置的11种PredicateFactory如何使用​​​​【云原生&微服务>SCG网关篇五】Spring Cloud Gateway自定义PredicateFactory​​​​【云原生&微服务>SCG网关篇六】Spring Cloud Gateway内置的18种Filter使用姿势​​​​【云原生&微服务>SCG网关篇七】Spring Cloud Gateway基于内置Filter实现限流、熔断、重试​​​​【云原生&微服务>SCG网关篇八】Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式​​​​【云原生&微服务>SCG网关篇九】Spring Cloud Gateway集成Nacos详细案例​​​​【云原生&微服务>SCG网关篇十】Spring Cloud Gateway集成Actuator、Zipkin详细案例​​​​【云原生&微服务>SCG网关篇十一】Spring Cloud Gateway解决跨域问题​​​​【云原生&微服务>SCG网关篇十二】Spring Cloud Gateway集成Sentinel API实现多种限流方式​​​​源码深度剖析Spring Cloud Gateway如何处理一个请求​​

聊了以下问题:

为什么要有网关?网关的作用是什么?网关的分类?网关的技术选型?使用网关时常用的灰度发布方式有哪些?Spring Cloud Gateway是什么?详细使用案例?Spring Cloud Gateway内置的11种PredicateFactory如何自定义PredicateFactory?Spring Cloud Gateway内置的18种常用的FilterSpring Cloud Gateway基于内置Filter实现限流、熔断、重试Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式Spring Cloud Gateway集成Nacos案例Spring Cloud Gateway集成Actuator、Zipkin案例Spring Cloud Gareway如何解决CORS跨域问题Spring Cloud Gateway集成Sentinel API实现限流从源码层面看Spring Cloud Gateway如何处理一个请求

在​​源码深度剖析Spring Cloud Gateway如何处理一个请求​​​一文博主对请求转发到特定的服务进行负载均衡打了一个问号,其中使用的Filter是​​NoLoadBalancerClientFilter​​(GatewayNoLoadBalancerClientAutoConfiguration的内部类)

本文就基于​​源码深度剖析Spring Cloud Gateway如何处理一个请求​​​的请求执行流程,结合​​【云原生&微服务>SCG网关篇九】Spring Cloud Gateway集成Nacos详细案例​​来聊一下当Spring Cloud Gateway集成Nacos之后,如何对服务做负载均衡。

PS:SpringCloud版本信息:

2.4.2 2020.0.1 2021.1 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import

二、调试程序信息

调试程序采用博文(​​【云原生&微服务>SCG网关篇九】Spring Cloud Gateway集成Nacos详细案例​​​)中的;另外需要对​​gateway-center​​ 项目的application.yml配置文件做一些调整,如下:

server: port: 9999spring: cloud: gateway: routes: - id: ingredients-fallback uri: predicates: - Path=/fallback # 通过过滤器将地址重写为:/hello/sayParam filters: - SetPath=/hello/sayParam - id: my_route uri: predicates:# - Path=/** - Path=/gateway/simple-service/** filters: - StripPrefix=2 # 自定义过滤器的名字,即:MyLogGatewayFilterFactory - name: MyLog args: name:

上述配置中涉及的内置Predicate和自定义filter参考博文:​​Spring Cloud Gateway内置的11种PredicateFactory如何使用​​​、​​Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式​​。

下面基于请求:​​Cloud Gateway对请求的处理流程见博文:​​源码深度剖析Spring Cloud Gateway如何处理一个请求​​;

当Spring Cloud Gateway集成服务注册中心(例如Nacos)之后,在第五步:核心过滤器链FilterWebHandler执行的时候获取到的Filter有12个,其中包括:

有一个Filter的命名特别值得关注:​​ReactiveLoadBalancerClientFilter​​,从类的命名推测,八成就是它负责处理负载均衡了;

请求负载均衡 <-- ReactiveLoadBalancerClientFilter

ReactiveLoadBalancerClientFilter#filter()中首先根据要调用的微服务名称从服务注册中心拿到服务的所有实例,然后根据默认的负载均衡策略(轮询)找到一个服务实例;具体代码执行流程如下:

获取到一个可用的服务实例之后,首先获取到服务实例的IP、Port、请求路径,然后判断是否使用Cloud Gateway更像是一个过滤器链执行框架;因为实际的请求转发 / 响应回写都是在过滤器中做的;所以它的负载均衡功能也是通过Filter实现的,​​ReactiveLoadBalancerClientFilter​​正负责处理负载均衡,其默认负载均衡策略为轮询(RoundRobin)。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:这些年遇到的RocketMQ消息消费超时/消费异常重试机制导致的重复消费问题(并发消费和顺序消费)源码分析
下一篇:图文详述Eureka的缓存机制/三级缓存
相关文章

 发表评论

暂时没有评论,来抢沙发吧~