@Slf4j
@Component
@RequiredArgsConstructor
public class PreRequestFilter implements GlobalFilter, Ordered {
private final GridinterRequestProperties gridinterRequestProperties;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String clientIp = IPUtil.getIpAddr(exchange.getRequest());
ServerHttpRequest.Builder mutate = exchange.getRequest().mutate();
mutate.headers(h->h.add(IPUtil.IP_KEY,clientIp));
// 是否开启traceId追踪
if (gridinterRequestProperties.getTrace()) {
// ID生成
String traceId = UUIDUtil.shortUuid();
MDC.put(GridinterConstant.LOG_TRACE_ID, traceId);
mutate.headers(h -> h.add(GridinterConstant.GRIDINTER_TRACE_ID, traceId));
}
ServerHttpRequest serverHttpRequest = mutate.build();
ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build();
return chain.filter(build);
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
gateway网关配置优先请求到本机的服务
于 2023-06-02 16:19:31 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)