我正在使用Spring Cloud Gateway 2.0.0.M6测试一个简单的网关。我只想将一个URL转发到另一个带有** regex
的URL示例1:/ integration / sbl / foo / bar =>本地主机:4178 / A-整合/ SBL /富/酒吧
示例2:/ integration / sbl / baz / bad =>本地主机:4178 / A-整合/ SBL /巴兹/坏
到目前为止,我已经写了以下内容,但它只转发到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
答案 0 :(得分:10)
您可以在路径过滤器中使用rewritePath功能,如此处的文档所述:
相关部分:
5.12 RewritePath GatewayFilter Factory
RewritePath GatewayFilter Factory采用路径regexp参数 和替换参数。这使用Java正则表达式 灵活的方式来重写请求路径。
var selectizeRoot = {
jQuery: require('jquery'),
Sifter: require('sifter'),
MicroPlugin: require('microplugin')
};
require('imports-loader?define=>false,exports=>false,this=>selectizeRoot!selectize');
对于/ foo / bar的请求路径,这会将路径设置为/ bar之前 发出下游请求。注意$ \替换为$ 因为YAML规范。
在您的示例中,它看起来像:
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: http://example.org
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
答案 1 :(得分:2)
我们在这里遇到了类似的问题,尽管我同意博伊恩的回答,但指出“ uri”参数忽略URI的“ path”组件可能会很有用。在文档中并不清楚(或者至少我没有找到它),所以希望对其他人有帮助。
假设您要将/ foo处收到的所有请求重定向到http://example.org/bar
例如:/ foo / x / y / z-> http://example.org/bar/x/y/z
例如,此方法按预期运行:
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: http://example.org
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /bar/$\{segment}
尽管此操作无法按预期进行(它忽略了/ bar):
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: http://example.org/bar
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
答案 2 :(得分:0)
请在下面找到具有完整设置的两种配置类型。两种方法都会产生相同的结果。
http://localhost:8090
/context
的基本路径用作网关的入口点my-resources
上运行的名为http://localhost:8091/my-resources
的服务。如果在没有参数的情况下调用/my-resources
,则会返回所有资源。当使用参数调用它时,它将返回具有相应RID
(如果有)配置网关,以便将传输到http://localhost:8090/context/my-resources/
的所有路径变量(可能都没有)转发到uri http://localhost:8091/my-resources/
。
application.yml
spring:
cloud:
gateway:
routes:
- id: route_id
predicates:
- Path=/context/my-resources/**
filters:
- RewritePath=/context/my-resources/(?<RID>.*), /my-resources/$\{RID}
uri: http://localhost:8091
@Bean
public RouteLocator routes(RouteLocatorBuilder routeBuilder) {
return routeBuilder.routes()
.route("route_id",
route -> route
.path("/context/my-resources/**")
.filters(f -> f.rewritePath("/context/my-resources/(?<RID>.*)", "/my-resources/${RID}"))
.uri("http://localhost:8091")
)
.build();
}
答案 3 :(得分:0)
没有提到使用rewritePath时更改contextPath的功能似乎被Spring Boot 2.3.X中的更改破坏了。而且RewritePathGatewayFilterFactory.Config在Hoxton.SR7 +中不包含用于传递新的contextPath的字段。
作为解决方法,我创建了一个自定义RewritePathGatewayFilter并使用:
ServerHttpRequest request = exchange.getRequest().mutate().contextPath(newContextPath).path(newPath).build();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI());
return chain.filter(exchange.mutate().request(request).build());