SpringCloud中网关拿到的路径(使用nacos做配置中心)

配置中心

 - id: system
   uri: lb://system
   predicates:
     - Path=/system/**
   filters: 
     - StripPrefix=1

这里会消去system这一层的请求

网关代码

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class RequestFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        ServerHttpRequest request = exchange.getRequest();

        System.out.println(request.getURI().getPath());

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

这里就进行全局的拦截然后答应出请求的路径

结论

可以拿到完整的请求的路径的上面的运行的结果是(假设要请求test服务):

/system/test

你可能感兴趣的:(SpringCloud,java实践,spring,cloud,spring,后端)