网关GateWay-源码分析
0. 环境
nacos版本:1.4.1
Spring Cloud : 2020.0.2
Spring Boot :2.4.4
Spring Cloud alibaba: 2.2.5.RELEASE
1. GateWay的自动配置
springboot 在引入一个新的组件时,一般都会有对应的XxxAutoConfiguration
类来对该组件进行配置,GateWay也不例外,在引入了以下配置后,就会生成对应的GatewayAutoConfiguration
自动配置类
<!-- gateway网关 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> 复制代码
GatewayAutoConfiguration
自动配置类信息如下:
@Configuration(proxyBeanMethods = false) //spring.cloud.gateway.enabled配置项必须为true,自动配置才生效,默认为true @ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true) @EnableConfigurationProperties //自动配置前置条件:引入了WebFlux 和 HttpHandler 组件 @AutoConfigureBefore({ HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class }) //自动配置后置组件:负载均衡组件 @AutoConfigureAfter({ GatewayLoadBalancerClientAutoConfiguration.class, GatewayClassPathWarningAutoConfiguration.class }) @ConditionalOnClass(DispatcherHandler.class) public class GatewayAutoConfiguration { ..... // 初始化各种bean } 复制代码
从配置类上的注解,可以了解到
spring.cloud.gateway.enabled
配置项必须为true,自动配置才生效,默认为true在使用Gataway之前,必须存在
WebFlux
和HttpHandler
组件注入
Gataway
之后,需要对请求负载到某一台服务器上,所以后置组件为负载均衡组件
自动配置类GatewayAutoConfiguration
在内部初始化了很多bean,列举几个重要的如下:
PropertiesRouteDefinitionLocator
:用于从配置文件(yml/properties)中读取路由配置信息!RouteDefinitionLocator
:把 RouteDefinition 转化为 RouteRoutePredicateHandlerMapping
:类似于 mvc 的HandlerMapping,不过这里是 Gateway实现的。用于匹配对应的请求routeGatewayProperties
:yml配置信息封装在 GatewayProperties 对象中AfterRoutePredicateFactory
:各种路由断言工厂,正是这些断言工厂在启动时已经生成对应的bean,我们才可以在 yml 中配置一下,即可生效RetryGatewayFilterFactory
:各种 Gateway 过滤器,正是这些过滤器在启动时已经生成对应的bean,我们才可以在 yml 中配置一下,即可生效GlobalFilter
实现类:全局过滤器
2. GateWay的源码执行流程
GateWay采用的是webFlux
的响应式编程,其整个流程与spring mvc 类似
框架 | Gateway | spring mvc |
---|---|---|
请求分发 | DispatcherHandler | DispatcherServlet |
请求映射 | HandlerMapping | HandlerMapping |
请求适配 | HanderAdaper | HanderAdaper |
请求处理 | WebHander | Hander |
所有请求都会经过 gateway 的DispatcherHandler中的handle
方法!可以看到该方法使用的就是webFlux
的响应式编程
@Override public Mono<Void> handle(ServerWebExchange exchange) { if (this.handlerMappings == null) { return createNotFoundError(); } if (CorsUtils.isPreFlightRequest(exchange.getRequest())) { return handlePreFlight(exchange); } return Flux // 1.遍历所有的 handlerMapping .fromIterable(this.handlerMappings) // 2.获取对应的handlerMapping ,比如常用的 RequestMappingHandlerMapping、RoutePredicateHandlerMapping .concatMap(mapping -> mapping.getHandler(exchange)) .next() .switchIfEmpty(createNotFoundError()) // 3.获取对应的适配器,调用对应的处理器 .flatMap(handler -> invokeHandler(exchange, handler)) // 4.返回处理结果 .flatMap(result -> handleResult(exchange, result)); } 复制代码
其实这就是 gateway的核心逻辑
2.1 路由断言HandlerMapping
进入路由断言HandlerMapping,扫描yml文件,匹配路由信息
上文核心逻辑代码中getHandler(exchange)
方法是获取对应的HandlerMapping
。由于是网关组件,当请求进入时,会先判断路由,所以会进入实现类RoutePredicateHandlerMapping
中。 org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping # getHandlerInternal 方法如下:
@Override protected Mono<?> getHandlerInternal(ServerWebExchange exchange) { if (this.managementPortType == DIFFERENT && this.managementPort != null && exchange.getRequest().getURI().getPort() == this.managementPort) { return Mono.empty(); } exchange.getAttributes().put(GATEWAY_HANDLER_MAPPER_ATTR, getSimpleName()); //寻找并匹配路由 return lookupRoute(exchange) .flatMap((Function<Route, Mono<?>>) r -> { //移除上下文中旧的属性 exchange.getAttributes().remove(GATEWAY_PREDICATE_ROUTE_ATTR); if (logger.isDebugEnabled()) { logger.debug( "Mapping [" + getExchangeDesc(exchange) + "] to " + r); } //把该路由与上下文绑定,后续负载均衡会用 exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, r); //返回 webHandler return Mono.just(webHandler); }).switchIfEmpty(Mono.empty().then(Mono.fromRunnable(() -> { exchange.getAttributes().remove(GATEWAY_PREDICATE_ROUTE_ATTR); if (logger.isTraceEnabled()) { logger.trace("No RouteDefinition found for [" + getExchangeDesc(exchange) + "]"); } }))); } 复制代码
其中lookupRoute
方法会找到yml中配置的所有的路由断言工厂(Before、After、Path等等
),并执行apply方法,进行路由匹配,判断是否允许请求通过!执行顺序由springboot自动配置时自己制定
protected Mono<Route> lookupRoute(ServerWebExchange exchange) { // getRoutes 获取所有的断言工厂 return this.routeLocator.getRoutes() .concatMap(route -> Mono.just(route).filterWhen(r -> { exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, r.getId()); // 先获取Route内部的predicate属性 //然后调用apply方法 执行断言!判断请求是否通过 return r.getPredicate().apply(exchange); }) } 复制代码
其中getRoutes()
方法就是通过RouteDefinitionRouteLocator
从配置文件中获取所有路由的,然后把找到的路由转换成Route
:
public Flux<Route> getRoutes() { // getRouteDefinitions() 从配置文件中获取所有路由 Flux<Route> routes = this.routeDefinitionLocator.getRouteDefinitions() // convertToRoute():把找到的路由转换成Route .map(this::convertToRoute); } 复制代码
Route
内部结构如下
public class Route implements Ordered { //路由id private final String id; //请求URI private final URI uri; //排序 private final int order; //断言 private final AsyncPredicate<ServerWebExchange> predicate; //过滤器 private final List<GatewayFilter> gatewayFilters; //元数据 private final Map<String, Object> metadata; } 复制代码
2.2 找到对应的适配器HandlerAdaptor,执行过滤器链
Gateway由于在第2.1步匹配路由后返回的是webHandler
类型的,所以也需要找到对应的HandlerAdaptor
,进入获取对应的适配器方法 invokeHandler(exchange, handler)
中
SimpleHandlerAdapter
中的handle
方法如下
public class SimpleHandlerAdapter implements HandlerAdapter { @Override public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) { //处理WebHandler 类型 WebHandler webHandler = (WebHandler) handler; Mono<Void> mono = webHandler.handle(exchange); return mono.then(Mono.empty()); } } 复制代码
其中webHandler.handle
方法就是处理所有过滤器链的方法,该过滤器链包括globalFilters
和gatewayFilters
:
@Override public Mono<Void> handle(ServerWebExchange exchange) { // 1. 根据路由与上下文绑定关系,获取对应的路由Route Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR); List<GatewayFilter> gatewayFilters = route.getFilters(); // 2. 收集所有的 globalFilters 并放入List<GatewayFilter> //注意这里使用了适配器模式 List<GatewayFilter> combined = new ArrayList<>(this.globalFilters); // 3. 把 gatewayFilters 也放入List<GatewayFilter>,形成一条过滤器立案 combined.addAll(gatewayFilters); // 4. 根据order排序 AnnotationAwareOrderComparator.sort(combined); if (logger.isDebugEnabled()) { logger.debug("Sorted gatewayFilterFactories: " + combined); } // 5. 执行过滤器链中的每一个过滤器方法! return new DefaultGatewayFilterChain(combined).filter(exchange); } 复制代码
注意:在组装过滤器链的时候,是把globalFilters
和gatewayFilters
两种过滤器都放进了List<GatewayFilter>
中,这是怎么做的呢?
这其实用到了一种 适配器
的设计模式!
如果放入的是
globalFilters
,会先把globalFilters
转化成GatewayFilterAdapter
。GatewayFilterAdapter
在内部集成了GlobalFilter
,同时也实现了GatewayFilter
,使globalFilters
和gatewayFilters
在 适配器 类GatewayFilterAdapter
中共存!如果放入的是
gatewayFilters
,直接放入即可!
//使用适配器类GatewayFilterAdapter 解决了 globalFilters想要放入List<GatewayFilter>中的类型不一致问题 private static class GatewayFilterAdapter implements GatewayFilter { //集成了`GlobalFilter` private final GlobalFilter delegate; GatewayFilterAdapter(GlobalFilter delegate) { this.delegate = delegate; } //调用filter时,调的是globalFilters的filter方法! @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { return this.delegate.filter(exchange, chain); } @Override public String toString() { final StringBuilder sb = new StringBuilder("GatewayFilterAdapter{"); sb.append("delegate=").append(delegate); sb.append('}'); return sb.toString(); } } 复制代码
然后在执行过滤器链中的globalFilters
和gatewayFilters
的filter方法时,就会为请求加上请求头、请求参数等扩展点!
3. Gateway的负载均衡是如何实现的?
Gateway的负载均衡只需要在yml中配置 uri: lb://mall-order
即可实现负载均衡
,底层是由全局过滤器LoadBalancerClientFilter
的filter
方法去做的!
以提供者服务的http://localhost:7700/depart-provider/provider/depart/get/1
为例!7700为网关Gateway的端口
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // 1. 根据路由与上下文绑定关系 // 获取原始的url:http://localhost:7700/depart-provider/provider/depart/get/1 URI url = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR); String schemePrefix = exchange.getAttribute(GATEWAY_SCHEME_PREFIX_ATTR); if (url == null || (!"lb".equals(url.getScheme()) && !"lb".equals(schemePrefix))) { return chain.filter(exchange); } addOriginalRequestUrl(exchange, url); if (log.isTraceEnabled()) { log.trace("LoadBalancerClientFilter url before: " + url); } // 2. 通过ribbon的负载均衡算法,根据服务名 去nacos选择一个实例! // 该实例就有order服务真正的 url 地址:http://localhost:9001/order/findOrderById/1 final ServiceInstance instance = choose(exchange); if (instance == null) { throw NotFoundException.create(properties.isUse404(), "Unable to find instance for " + url.getHost()); } // 3. 拿到原生的 uri :http://localhost:8888/order/findOrderById/1 URI uri = exchange.getRequest().getURI(); String overrideScheme = instance.isSecure() ? "https" : "http"; if (schemePrefix != null) { overrideScheme = url.getScheme(); } // 4. 拿服务实例instance的uri替换原生的uri地址 得到 新的url // 新的url: http://localhost:8888/order/findOrderById/1 URI requestUrl = loadBalancer.reconstructURI( new DelegatingServiceInstance(instance, overrideScheme), uri); if (log.isTraceEnabled()) { log.trace("LoadBalancerClientFilter url chosen: " + requestUrl); } // 5. 再次记录上下文关系 exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl); // 6. 执行过滤器链中的其他过滤请求 return chain.filter(exchange); }
作者:hsfxuebao
链接:https://juejin.cn/post/7171253354537386015