首页 > 解决方案 > 为什么spring cloud gateway路由慢?我的配置有什么问题

问题描述

我正在使用 spring can gateway 根据用户访问 api 路由到服务器。很少有人应该通过Wrapper服务——在全部变成实际(平台)API之前做一些额外的工作,其他的是Platform服务——实际工作将在其中完成

我的配置如下

@Bean
public RouteLocator routes(RouteLocatorBuilder builder, WrapperAPIPredicateFactory pf) {
    return builder.routes()
            .route("platform", r -> r.path("/process/**").and().predicate(pf.apply(new Config()))
                    .uri("https://platform.excample.com"))
            .route("wrapper", r -> r.path("/**")
                    .uri("https://wrapper.excample.com"))
            .build();
}

有了这个,我看到使用spring cloud gateway路由到平台url的调用比直接调用平台api花费的时间更长。即直接调用耗时300ms,来自spring cloud gateway路由的相同调用耗时1000ms

注意:在路由谓词中没有 DB 或 Network 调用

根据我的 gradle 配置使用 Spring 和 Spring Cloud Gateway 版本更新

plugins {
    id 'org.springframework.boot' version '2.5.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2020.0.3"
    }
}

标签: javaspringspring-cloudspring-cloud-gateway

解决方案


推荐阅读