Spring 云网关 API - 路由上的上下文路径不起作用

Spring Cloud Gateway API - Context-path on routes not working

我在 application.yml

中设置了上下文路径
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:4177/gb-integration/sbl/** 访问 API 网关 但是它只适用于 localhost:4177/sbl/**

我的上下文路径似乎被忽略了。 有什么想法可以让我的上下文路径在我的所有路线上工作吗?

你可能已经自己弄明白了,但这是对我有用的:

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

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

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

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

您实际上可以在 spring cloud documentation 中看到它。

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

像这样使用 yaml 文件

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

关于@sendon1982 回答的详细信息

如果您的服务在 localhost:8080/color/red 公开并且您希望它可以作为 localhost:9090/gateway/color/red 从网关访问,在谓词的路径参数中,在 /gateway 前面添加 /gateway,然后添加 StripPrefix 作为过滤器中的 1,基本上转换为

take the requested path which matches Path, strip/remove out the prefix paths till the number mentioned and route using given uri and the stripped path

my-app-gateway: /gateway
spring:  
  cloud:
    gateway:
      routes:
        - id: color-service
        uri: http://localhost:8080
        predicates:
          - Path=${my-app-gateway}/color/**
        filters:
          - StripPrefix=1
fixed :

application.yaml:

gateway:
  discovery:
    locator:
      enabled: true
      lower-case-service-id: true
      filters:
        # 去掉 /ierp/[serviceId] 进行转发
        - StripPath=2
      predicates:
        - name: Path
          # 路由匹配 /ierp/[serviceId]
          # org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator#getRouteDefinitions
          args[pattern]: "'/ierp/'+serviceId+'/**'"

过滤器:

@Component
public class StripPathGatewayFilterFactory extends 
AbstractGatewayFilterFactory<StripPathGatewayFilterFactory.Config> {
/**
 * Parts key.
 */
public static final String PARTS_KEY = "parts";

public StripPathGatewayFilterFactory() {
    super(StripPathGatewayFilterFactory.Config.class);
}

@Override
public List<String> shortcutFieldOrder() {
    return Arrays.asList(PARTS_KEY);
}

@Override
public GatewayFilter apply(Config config) {
    return (exchange, chain) -> {
        ServerHttpRequest request = exchange.getRequest();
        ServerWebExchangeUtils.addOriginalRequestUrl(exchange, request.getURI());
        String path = request.getURI().getRawPath();
        String[] originalParts = StringUtils.tokenizeToStringArray(path, "/");

        // all new paths start with /
        StringBuilder newPath = new StringBuilder("/");
        for (int i = 0; i < originalParts.length; i++) {
            if (i >= config.getParts()) {
                // only append slash if this is the second part or greater
                if (newPath.length() > 1) {
                    newPath.append('/');
                }
                newPath.append(originalParts[i]);
            }
        }
        if (newPath.length() > 1 && path.endsWith("/")) {
            newPath.append('/');
        }

        ServerHttpRequest newRequest = request.mutate().path(newPath.toString()).contextPath(null).build();

        exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());

        return chain.filter(exchange.mutate().request(newRequest).build());
    };
}

public static class Config {

    private int parts;

    public int getParts() {
        return parts;
    }

    public void setParts(int parts) {
        this.parts = parts;
    }

}

}