• SpringCloud Gateway源码跟踪解析


    1. Springcloud Gateway 

    https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/

    2. 核心组件:

    Predicate

    GetewayFilter

    GlobalFilter

    3. high-level overview

    https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/images/spring_cloud_gateway_diagram.png

    3. 与SpringMVC 横向对比

    SpringMVC =====> Spring WebFlux

    DispatchServlet =====> DispatchHandler

    路由匹配:

    HandlerMapping =====>HandlerMapping

    HanderAdaper ====> HanderAdaper

    Hander =====> WebHander

    4. 源码跟踪

    核心类:org.springframework.web.reactive.DispatcherHandler

    1. public Mono handle(ServerWebExchange exchange) {
    2. return this.handlerMappings == null ? this.createNotFoundError() : Flux.fromIterable(this.handlerMappings).concatMap((mapping) -> {
    3. return mapping.getHandler(exchange);
    4. }).next().switchIfEmpty(this.createNotFoundError()).flatMap((handler) -> {
    5. return this.invokeHandler(exchange, handler);
    6. }).flatMap((result) -> {
    7. return this.handleResult(exchange, result);
    8. });
    9. }

    org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping#getHandlerInternal

    在lookupRoute执行所有的 Predicate断言

    1. protected Mono lookupRoute(ServerWebExchange exchange) {
    2. return this.routeLocator.getRoutes().concatMap((route) -> {
    3. return Mono.just(route).filterWhen((r) -> {
    4. exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_PREDICATE_ROUTE_ATTR, r.getId());
    5. return (Publisher)r.getPredicate().apply(exchange);
    6. }).doOnError((e) -> {
    7. this.logger.error("Error applying predicate for route: " + route.getId(), e);
    8. }).onErrorResume((e) -> {
    9. return Mono.empty();
    10. });
    11. }).next().map((route) -> {
    12. if (this.logger.isDebugEnabled()) {
    13. this.logger.debug("Route matched: " + route.getId());
    14. }
    15. this.validateRoute(route, exchange);
    16. return route;
    17. });
    18. }

     org.springframework.cloud.gateway.handler.FilteringWebHandler#handle

    将所有的GlobalFilter 转换成Filter 加入到过滤器链中

    1. public Mono handle(ServerWebExchange exchange) {
    2. Route route = (Route)exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
    3. List gatewayFilters = route.getFilters();
    4. List combined = new ArrayList(this.globalFilters);
    5. combined.addAll(gatewayFilters);
    6. AnnotationAwareOrderComparator.sort(combined);
    7. if (logger.isDebugEnabled()) {
    8. logger.debug("Sorted gatewayFilterFactories: " + combined);
    9. }
    10. return (new FilteringWebHandler.DefaultGatewayFilterChain(combined)).filter(exchange);
    11. }

     所有的filter

    核心类org.springframework.cloud.gateway.filter.LoadBalancerClientFilter

    ribbon 根据serviceId 找到对应服务的ip

    1. public Mono handle(ServerWebExchange exchange) {
    2. Route route = (Route)exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
    3. List gatewayFilters = route.getFilters();
    4. List combined = new ArrayList(this.globalFilters);
    5. combined.addAll(gatewayFilters);
    6. AnnotationAwareOrderComparator.sort(combined);
    7. if (logger.isDebugEnabled()) {
    8. logger.debug("Sorted gatewayFilterFactories: " + combined);
    9. }
    10. return (new FilteringWebHandler.DefaultGatewayFilterChain(combined)).filter(exchange);
    11. }

  • 相关阅读:
    期待未来:辉视校园IPTV系统引领创新与变革
    2023-python-解释器是什么东西?
    【Java小白福利】Java面试、学习教程合集!
    记一次 .NET 某埋线管理系统 崩溃分析
    Linux小总结
    目标检测——【Transformer】Accelerating DETR Convergence via Semantic-Aligned Matching
    ESXI配置免密登录
    人体呼吸存在传感器成品,毫米波雷达探测感知技术,引领智能家居新潮流
    太牛皮啦!阿里P8架构师,呕心48小时整理的分布式架构实战笔记
    大数据错误
  • 原文地址:https://blog.csdn.net/caicongyang/article/details/126395670