一、前言
至此微服务网关系列文章已出:
- 【云原生&微服务>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种常用的Filter
- Spring 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版本信息:
<properties>
<spring-boot.version>2.4.2</spring-boot.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
二、调试程序信息
调试程序采用博文(【云原生&微服务>SCG网关篇九】Spring Cloud Gateway集成Nacos详细案例)中的;另外需要对gateway-center
项目的application.yml配置文件做一些调整,如下:
server:
port: 9999
spring:
cloud:
gateway:
routes:
- id: ingredients-fallback
uri: http://127.0.0.1:9001
predicates:
- Path=/fallback
# 通过过滤器将地址重写为:/hello/sayParam
filters:
- SetPath=/hello/sayParam
- id: my_route
uri: http://127.0.0.1:9001
predicates:
# - Path=/**
- Path=/gateway/simple-service/**
filters:
- StripPrefix=2
# 自定义过滤器的名字,即:MyLogGatewayFilterFactory
- name: MyLog
args:
name: Saint_log_name
上述配置中涉及的内置Predicate和自定义filter参考博文:Spring Cloud Gateway内置的11种PredicateFactory如何使用、Spring Cloud Gateway三种自定义Filter、GlobalFilter的方式。
下面基于请求:http://127.0.0.1:9999/gateway/simple-service/hello/sayHello
,做Gateway处理请求的流程分析。
三、Gateway对请求做负载均衡分析
Spring Cloud Gateway对请求的处理流程见博文:源码深度剖析Spring Cloud Gateway如何处理一个请求;
当Spring Cloud Gateway集成服务注册中心(例如Nacos)之后,在第五步:核心过滤器链FilterWebHandler执行的时候获取到的Filter有12个,其中包括:
有一个Filter的命名特别值得关注:ReactiveLoadBalancerClientFilter
,从类的命名推测,八成就是它负责处理负载均衡了;
请求负载均衡 <-- ReactiveLoadBalancerClientFilter
ReactiveLoadBalancerClientFilter#filter()中首先根据要调用的微服务名称从服务注册中心拿到服务的所有实例,然后根据默认的负载均衡策略(轮询)找到一个服务实例;具体代码执行流程如下:
获取到一个可用的服务实例之后,首先获取到服务实例的IP、Port、请求路径,然后判断是否使用https协议,组装出真正要请求的服务地址,交给NettyRoutingFilter通过HttpClient执行请求。执行完请求之后,在then()方法中执行ReactiveLoadBalancerClientFilter的后置逻辑。
总结
感觉Spring Cloud Gateway更像是一个过滤器链执行框架;因为实际的请求转发 / 响应回写都是在过滤器中做的;所以它的负载均衡功能也是通过Filter实现的,ReactiveLoadBalancerClientFilter
正负责处理负载均衡,其默认负载均衡策略为轮询(RoundRobin)。