一、背景:

   项目升级改造,老项目使用请求url中特定参数进行服务路由,现使用gateway网关进行路由服务信息

二、根据参数信息修改请求路径Path

@Component
public class RequestFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest str = exchange.getRequest();
        if (str.getQueryParams().containsKey("demo")){
            addOriginalRequestUrl(exchange, str.getURI());
            String newPath =str.getPath()+"demo";
            ServerHttpRequest newRequest = str.mutate()
                    .path(newPath)
                    .build();
            exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
            return chain.filter(exchange.mutate()
                    .request(newRequest).build());
        }
        return chain.filter(exchange);
    }
    @Override
    public int getOrder() {
        return 1;
    }

1、根据url路径是否包含特定参数demo字段进行过滤

2、注意这个只是修改path路径值,并不能修改gateway路由得serverid

3、getOrder方法 执行顺先顺序0-100 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐