本文介绍了Spring Cloud Gateway - 代理/转发 URL 的整个子部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Cloud Gateway 2.0.0.M6 测试一个简单的网关.我只想将一个 URL 转发到另一个带有 ** regex

的 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-整合/

 @Bean公共 RouteLocator routeLocator(RouteLocatorBuilder builder) {String test = "http://localhost:4178/a-integration/";返回 builder.routes().route("集成测试",r ->r.path("/integration/sbl/**").uri(测试)).build();}

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

编辑

我根据下面的回复尝试了以下内容

String samtykke = "http://localhost:4178/";返回 builder.routes().route("samtykke", r -> r.path("/gb-integration/sbl/**").filters(f -> f.rewritePath("/gb-integration/sbl/(?.*)", "/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] oscghpRoutePredicateFactory :模式/gb-integration/sbl/**"匹配值[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:路由匹配:samtykke2018-02-23 09:46:35.198 DEBUG 6364 --- [ctor-http-nio-2] oscghRoutePredicateHandlerMapping :映射 [Exchange: GET http://localhost:4177/gb-integration/sbl/api/sbl/收入/]到路由{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] oscghandler.FilteringWebHandler : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframeway.filter.cloud..NettyWriteResponseFilter@5c534b5b}, order=-1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$247/485237151@77da026a, order=0}, OrderedGatewayFilter=GatewayFilterGateway=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@396639b}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@a18649a}, order=2614774, order=2614774{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@2b22a1cc}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRouting过滤器@62573c86},订单=2147483647}]2018-02-23 09:46:35.232 TRACE 6364 --- [ctor-http-nio-2] o.s.c.g.filter.RouteToRequestUrlFilter : RouteToRequestUrlFilter 开始2018-02-23 09:46:35.314 TRACE 6364 --- [ctor-http-nio-1] o.s.c.g.filter.NettyWriteResponseFilter :NettyWriteResponseFilter 开始
解决方案

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

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

相关部分:

5.12 RewritePath 网关过滤器工厂

RewritePath 网关过滤器工厂采用路径正则表达式参数和一个替换参数.这使用 Java 正则表达式来表示灵活的方式重写请求路径.

弹簧:云:网关:路线:- id:rewritepath_routeuri:http://example.org谓词:- 路径=/foo/**过滤器:- RewritePath=/foo/(?.*),/$\{segment}

对于/foo/bar的请求路径,这将在之前设置路径为/bar发出下游请求.注意用 $ 替换的 $\因为 YAML 规范.

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

@Bean公共 RouteLocator routeLocator(RouteLocatorBuilder builder) {String test = "http://localhost:4178";返回 builder.routes().route("集成测试", r -> r.path("/integration/sbl/**").filters(f->f.rewritePath("/integration/(?.*)","/a-integration/${segment}")).uri(测试)).build();}

I am using Spring Cloud Gateway 2.0.0.M6 testing a simple gateway. I just want a URL to be forwarded to another URL with ** regex

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

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

So far I have written the following, but it only forwards to 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();
}

How can I fix the above code to enable this behaviour?

EDIT

I tried the following based on response below

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();

and I tried a GET http://localhost:4177/gb-integration/sbl/api/sbl/income/ and expected http://localhost:4178/gb-samtykke-integration/api/sbl/income/ back but it didn't work.

The output says:

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
解决方案

You can use the rewritePath functionality in your path filters, as specified by the documentation found here :

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

Relevant parts :

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

In your example, that would look like :

@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();
}

这篇关于Spring Cloud Gateway - 代理/转发 URL 的整个子部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 10:28