SpringCloud Gateway 源码流程
2023-07-23 18:45:58 11 举报
SpringCloud Gateway源码流程
作者其他创作
大纲/内容
2
获取处理网关请求的Handlermapping.getHandler(exchange)
Spring Cloud Gateway
返回处理路由请求的webHandler类型为FilteringWebHandlerreturn Mono.just(webHandler);
Mono.just(route).filterWhen过滤
Gateway Handler Mapping
拿到路由配置的过滤器List<GatewayFilter> gatewayFilters = route.getFilters();
Client
将找到的font color=\"#e74f4c\
SpringCloud Gateway 采用的是Netty+webflux+reactor实现,通过DispatcherHandler的handle方法处理请求
处理请求org.springframework.web.reactive.DispatcherHandler#handle
拿到之前匹配好放入Attribute的路由Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR);
getHandlerInternal(exchange)
选择目标实例,使用默认轮询。choose详细路径见链接final ServiceInstance instance = choose(exchange);
post
Gateway Web Handler
使用路由/断言器匹配HandlerRoutePredicateHandlerMapping#getHandlerInternal()
负载均衡调用过滤器LoadBalancerClientFilter
handle(ServerWebExchange exchange)
6
查找路由逻辑
这里两个过滤器举例
动态注册中心(Nacos/Eureka)
开始请求
webHandle
1. Gateway Handler Mapping
return webHandler
3. 执行GatewayFilter
webHandler.handle
合并过滤器并按order排序combined.addAll(gatewayFilters); AnnotationAwareOrderComparator.sort(combined);
3
调用下一个过滤器return chain.filter(exchange);
源RUIURI uri = exchange.getRequest().getURI();
http://127.0.0.1:8100/cz-provider/gateway
2. Gateway Web Handler
return webHandler
拿到所有路由信息this.routeLocator.getRoutes()
处理过滤器链new DefaultGatewayFilterChain(combined).filter(exchange)
静态配置文件
filter
pre
1
Gateway RouteDefinitionLocator
拿到全局过滤器List<GatewayFilter> combined = new ArrayList<>(this.globalFilters);
5
找路由(获取所有配置文件的route)lookupRoute(exchange)
4
Proxied Service
将请求发送到目标服务NettyRoutingFilter
1、客户端向 Spring Cloud Gateway 发出请求2、span style=\
经过过滤器链处理网关请求WebHandler webHandler = (WebHandler) handler; Mono<Void> mono = webHandler.handle(exchange);
GatewayFilter
0 条评论
下一页