Spring Cloud Gateway API - 路由不正常的上下文路径

时间:2018-05-14 08:07:28

标签: spring spring-cloud spring-cloud-gateway

我在application.yml中设置了context-path

server:
  port: 4177
  max-http-header-size: 65536
  tomcat.accesslog:
    enabled: true
  servlet:
    context-path: /gb-integration

我已经配置了一些路线

@Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        final String sbl = "http://localhost:4178";

        return builder.routes()
                //gb-sbl-rest
                .route("sbl", r -> r
                        .path("/sbl/**")
                        .filters(f -> f.rewritePath("/sbl/(?<segment>.*)", "/gb-sbl/${segment}"))
                        .uri(sbl)).build();
    }

我希望使用localhost访问API网关:4177 / gb-integration / sbl / ** 但是它只适用于localhost:4177 / sbl / **

似乎我的上下文路径被忽略了。 任何想法如何让我的上下文路径在我的所有路线上工作?

3 个答案:

答案 0 :(得分:1)

你可能已经把自己想象出来了,但这里有什么对我有用:

在阅读了Spring Cloud文档并尝试了很多东西之后,我最终选择了路由配置路由。在你的情况下,它看起来像这样:

.path("/gb-integration/sbl/**")

并为每条路线重复相同的模式。

.path("/gb-integration/abc/**")
...
.path("/gb-integration/def/**")

您实际上可以在spring cloud documentation中看到这一点。

spring clould文档似乎正在进行中。希望我们能找到更好的解决方案。

答案 1 :(得分:0)

像这样使用yaml文件

spring:  
  cloud:
    gateway:
      routes:
        - id: property-search-service-route
        uri: http://localhost:4178
        predicates:
          - Path=/gb-integration/sbl/**

答案 2 :(得分:0)

@sendon1982 答案的详细信息

如果您的服务在 #include <bits/stdc++.h> using namespace std; void printSubsequence(string input, string output) { if (input.empty()) { cout << output << endl; return; } printSubsequence(input.substr(1), output + input[0]); printSubsequence(input.substr(1), output); } int main() { string output = ""; string input = "abcd"; printSubsequence(input, output); return 0; } 处公开,并且您希望它可以作为 localhost:8080/color/red 从网关访问,请在谓词的 Path 参数中,添加 localhost:9090/gateway/color/red,并添加 {{1 }} 作为过滤器中的 1,基本上转化为

<块引用>

获取与/gateway匹配的请求路径,去除/删除前缀路径直到提到的数字并使用给定的uri和去除的路径进行路由

StripPrefix