当前位置:   article > 正文

SpringCloud Gateway 路由转发性能优化_routepredicatehandlermapping调优

routepredicatehandlermapping调优

对源码做简单修改,比如,Path 匹配 /mock/** 则对路由查找结果进行缓存(注意这里缓存策略和方式仅仅是举例,根据实际需求情况来做)

public static final String MOCK_PATCH = "/mock/**";private Map<String, Route> hashCache = new ConcurrentHashMap<>(1024);protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
    String path = exchange.getRequest().getPath().subPath(0).value();  //符合Path规则,优先从缓存Map获取,时间复杂度近似于O(1)  if (pathMatcher.match(MOCK_PATCH, path)) {
      return Mono.justOrEmpty(hashCache.get(path))               .switchIfEmpty(getRouteMono(exchange, path));  }  return getRouteMono(exchange, path);}private Mono<Route> getRouteMono(ServerWebExchange exchange, String path) {
    return this.routeLocator.getRoutes()      //... 略过      .map(route -> {
          if (logger.isDebugEnabled()) {
            logger.debug("Route matched: " + route.getId());        }        validateRoute(route, exchange);        //符合Path规则,缓存路由        if (pathMatcher.match(MOCK_PATCH, path)) {
            hashCache.put(path, route);        }        return route;      });}

继续查阅源码,找到RoutePredicateHandlerMapping 是如何装配的。在GatewayAutoConfiguration 中实现了 SpringCloud Gateway 内部组件的自动装配RoutePredicateHandlerMapping 也在其中,代码入下:

@Beanpublic RoutePredicateHandlerMapping routePredicateHandlerMapping(FilteringWebHandler webHandler,    RouteLocator routeLocator, GlobalCorsProperties globalCorsProperties, Environment environment) {
    return new RoutePredicateHandlerMapping(webHandler, routeLocator, globalCorsProperties, environment);}

很遗憾,官方没有给这个自动装配添加条件,我们无法自行装配替代默认装配。

我们只能采取以下步骤:

  1. 在 Springboot 启动类上增加排除 GatewayAutoConfiguration 的自动装配配置;

  2. 继承 GatewayAutoConfiguration 并完全拷贝其装配条件;

  3. 覆盖父类 routePredicateHandlerMapping方法,给装配添加条件;

  4. 继承RoutePredicateHandlerMapping ,覆盖其 lookupRoute方法,符合一定条件的请求,优先从缓存中查找路由。

改造 Gateway

修改启动类,排除自动装配

@SpringBootApplication(exclude = GatewayConfiguration.class)public class GatewayApplication {
    public static void main(String[] args) {
      SpringApplication.run(GatewayApplication.class, args);  }}

继承 GatewayAutoConfiguration

@Configuration(proxyBeanMethods = false)@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)@EnableConfigurationProperties@AutoConfigureBefore({HttpHa
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/830894
推荐阅读
相关标签
  

闽ICP备14008679号