spring 网关:如何根据请求头和路径动态设置URI和路径

spring gateway: how to dynamically set URI and path based on request headers and path

假设我有传入请求: 主机:my-XXX.domain.com 和路径 /YYY/ZZZ,我想将该请求路由到 uri XXX.YYY.internal.domain.com/ZZZ。我怎样才能实现它?

标准 api 似乎不允许任何类型的模式提取

builder.routes()
    .route{ it
            .header(xxx,xxx)
            .path("/*/**")
            .uri("i can't use here anything captured in header or path function")
    }

有一个功能可以让我访问请求并允许 return 任何 URI

                        .filters{
                            it.changeRequestUri {
                                val service = it.request....
                                Optional.of(URI("http://...."))
                            }
                        }
                        .uri("https://this will be ignored")

但我无法在那里设置路径。

是否有任何现有的 api 可以简单地实现它,或者我是否必须编写自定义过滤器?如何正确操作?

这对我有用

.filters{
    it.changeRequestUri {
        val (service, environment, newPath) = computeUrlParts(it)
        Optional.of(URI("http://$environment$service.internal.my-company.com/$newPath"))
    }
}
.uri("http://postman-echo.com") //ignored