• 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. }

  • 相关阅读:
    基于神经网络的偏微分方程求解器再度取得突破,北大&字节的研究成果入选Nature子刊
    看动画,学Java基础教程12:关键字
    Qt|多个窗口共有一个提示框类
    CSS布局秘籍(2)-6脉神剑
    spark的启动测试失败
    数据结构之线性表
    谷粒商城项目-环境配置
    java计算机毕业设计基于springboo+vue的学生活动组织管理系统
    图解 LeetCode 算法汇总——双指针
    PCIe实用调试工具MindShare Arbor增加试用天数
  • 原文地址:https://blog.csdn.net/caicongyang/article/details/126395670