4

我在 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/**

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

4

4 回答 4

2

您可能已经自己弄清楚了,但这对我有用:

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

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

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

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

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

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

于 2018-06-08T13:30:08.270 回答
0

像这样使用 yaml 文件

spring:  
  cloud:
    gateway:
      routes:
        - id: property-search-service-route
        uri: http://localhost:4178
        predicates:
          - Path=/gb-integration/sbl/**
于 2020-08-21T00:38:26.317 回答
0

详细说明@sendon1982 答案

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

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

my-app-gateway: /gateway
spring:  
  cloud:
    gateway:
      routes:
        - id: color-service
        uri: http://localhost:8080
        predicates:
          - Path=${my-app-gateway}/color/**
        filters:
          - StripPrefix=1
于 2021-07-31T13:26:57.213 回答
0
fixed :

应用程序.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;
    }

}

}

于 2022-01-12T10:23:06.427 回答