java - Spring 网关: how to dynamically set URI and path based on request headers and path

标签 java spring spring-boot spring-cloud spring-cloud-gateway

假设我有传入请求: 主机:my-XXX.domain.com和路径/YYY/ZZZ,我想将该请求路由到uriXXX.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")
    }

有一个函数可以让我访问请求并允许返回任何 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

关于java - Spring 网关: how to dynamically set URI and path based on request headers and path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59865153/

相关文章:

java - 检查linux中哪个线程正在哪个CPU上运行

java xml解析: instant updating of file

mysql - SQL 表达式中存在语法错误 - 无语法错误

java - Jhipster 文件导入到 Eclipse 后抛出多个错误

java - HeaderElement1_1Impl ClassCastException Java 使用 JDK 而不是 saaj-impl

java - Spring MVC 配置 url-pattern

java - Eclipse 未加载 WAR 文件

java - 使用 Predicate 和 Criteria API 进行不区分大小写的搜索

java - 在 Spring Boot 应用程序中加载不同的应用程序上下文

java - 如果存在引用则更新行,否则使用 spring boot 删除