Spring Cloud Gateway - 代理/转发 URL 的整个子部分

新手上路,请多包涵

我正在使用 Spring Cloud Gateway 2.0.0.M6 测试一个简单的网关。我只想用 ** 正则表达式将 URL 转发到另一个 URL

示例 1:/integration/sbl/foo/bar => localhost:4178/a-integration/sbl/foo/bar

示例 2:/integration/sbl/baz/bad => localhost:4178/a-integration/sbl/baz/bad

到目前为止,我已经写了以下内容,但它只转发到 http://localhost:4178/a-integration/

  @Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    String test = "http://localhost:4178/a-integration/";

    return builder.routes()
            .route("integration-test",
                    r -> r.path("/integration/sbl/**")
                            .uri(test)
            )
            .build();
}

如何修复上述代码以启用此行为?

编辑

我根据以下响应尝试了以下操作

String samtykke = "http://localhost:4178/";

return builder.routes()

        .route("samtykke", r -> r
                .path("/gb-integration/sbl/**")
                .filters(f -> f.rewritePath("/gb-integration/sbl/(?<segment>.*)", "/gb-samtykke-integration/${segment}"))
                .uri(samtykke))
        .build();

我尝试了 GET http://localhost:4177/gb-integration/sbl/api/sbl/income/ 并期望 http://localhost:4178/gb-samtykke-integration/api/sbl/income/ 回来但它没用。

输出说:

 2018-02-23 09:46:35.197 TRACE 6364 --- [ctor-http-nio-2] o.s.c.g.h.p.RoutePredicateFactory        : Pattern "/gb-integration/sbl/**" matches against value "[path='/gb-integration/sbl/api/sbl/income/']"
2018-02-23 09:46:35.198 DEBUG 6364 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping   : Route matched: samtykke
2018-02-23 09:46:35.198 DEBUG 6364 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping   : Mapping [Exchange: GET http://localhost:4177/gb-integration/sbl/api/sbl/income/] to Route{id='samtykke', uri=http://localhost:4178/, order=0, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$245/1803714790@1d0042df, gatewayFilters=[OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$247/485237151@77da026a, order=0}]}
2018-02-23 09:46:35.200 DEBUG 6364 --- [ctor-http-nio-2] o.s.c.g.handler.FilteringWebHandler      : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@5c534b5b}, order=-1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$247/485237151@77da026a, order=0}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@396639b}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@a18649a}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@2b22a1cc}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@62573c86}, order=2147483647}]
2018-02-23 09:46:35.232 TRACE 6364 --- [ctor-http-nio-2] o.s.c.g.filter.RouteToRequestUrlFilter   : RouteToRequestUrlFilter start
2018-02-23 09:46:35.314 TRACE 6364 --- [ctor-http-nio-1] o.s.c.g.filter.NettyWriteResponseFilter  : NettyWriteResponseFilter start

原文由 Shervin Asgari 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 737
1 个回答

您可以在路径过滤器中使用 rewritePath 功能,如此处找到的文档所指定:

https://cloud.spring.io/spring-cloud-gateway/reference/html/#rewritepath-gatewayfilter-factory

相关部分:

5.12 RewritePath GatewayFilter 工厂

RewritePath GatewayFilter Factory 采用路径正则表达式参数和替换参数。这使用 Java 正则表达式以灵活的方式重写请求路径。

 spring:
  cloud:
     gateway:
       routes:
       - id: rewritepath_route
         uri: http://example.org
         predicates:
         - Path=/foo/**
         filters:
         - RewritePath=/foo/(?<segment>.*), /$\{segment}

对于 /foo/bar 的请求路径,这将在发出下游请求之前将路径设置为 /bar。注意 \(\ 由于 YAML 规范,它被替换为 \)

在您的示例中,这看起来像:

 @Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    String test = "http://localhost:4178";

    return builder.routes()
            .route("integration-test", r -> r
                    .path("/integration/sbl/**")
                    .filters(f->f.rewritePath("/integration/(?<segment>.*)","/a-integration/${segment}"))
                    .uri(test)
                  )
            .build();
}

原文由 Boyen 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题