繁体   English   中英

spring 云网关只转发到 url 的基本路径

[英]spring cloud gateway only forwards to base path of url

我正在尝试使用 spring-cloud-gateway 构建一个简单的 api-gateway。 到目前为止,我了解了基本原理,但我遇到了一个特定的问题:

我转发请求的目标 url 可能包含零个、一个或多个路径段。 不幸的是,这些路径段被忽略了。


private final String routingTargetWithPath = "http://hapi.fhir.org/baseR4";

  @Bean
  public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("patient", r -> r
            .path("/Patient", "/Patient/*")
            .and()
            .method(GET)
            .uri(routingTargetWithPath)
        )
        .build();
  }

使用 curl 将请求发送到我的 api 网关:

curl http://localhost:8080/Patient
  and accordingly
curl http://localhost:8080/Patient/2069748

我假设请求将被路由到:

http://hapi.fhir.org/baseR4/Patient
  and accordingly
http://hapi.fhir.org/baseR4/Patient/2069748

但是他们被路由到:

http://hapi.fhir.org/Patient
  and accordingly
http://hapi.fhir.org/Patient/2069748

因此,忽略配置路由 url 的路径。 不幸的是,我不能在这里进行手动重写,因为在生产中将配置“routingTarget”,但我不知道它是否包含以及包含多少路径段。

如何实现路由到完整配置的路由目标?

好的,我找到了答案:根据这里的说法,uri 的路径被忽略是有意的。 因此,就我而言,设置路径过滤器可以解决问题:

private final URI routingTargetWithPath = URI.create("http://hapi.fhir.org/baseR4");

  @Bean
  public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("patient", r -> r
            .path("/Patient", "/Patient/*")
            .and()
            .method(GET)
            .filters(f -> f.prefixPath(routingTargetWithPath.getPath()))
            .uri(routingTargetWithPath)
        )
        .build();
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM