From 519b2fb452a0e72105d72a1090d4fcda1b00df0a Mon Sep 17 00:00:00 2001 From: zhangran <371282726@qq.com> Date: Tue, 13 Jul 2021 17:03:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=AF=B7=E6=B1=82=E7=9A=84API?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E8=BF=87=E6=BB=A4=EF=BC=8C=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9A=84=E8=AF=B7=E6=B1=82=E6=97=B6=E9=97=B4?= =?UTF-8?q?=EF=BC=8C=E6=96=B9=E4=BE=BF=E6=97=A5=E5=BF=97=E5=AE=A1=E8=AE=A1?= =?UTF-8?q?=E3=80=81=E5=91=8A=E8=AD=A6=E3=80=81=E5=88=86=E6=9E=90=E7=AD=89?= =?UTF-8?q?=E8=BF=90=E7=BB=B4=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pig/gateway/filter/ApiLoggingFilter.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ApiLoggingFilter.java diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ApiLoggingFilter.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ApiLoggingFilter.java new file mode 100644 index 00000000..8a588685 --- /dev/null +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ApiLoggingFilter.java @@ -0,0 +1,62 @@ +package com.pig4cloud.pig.gateway.filter; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.util.List; + +/** + * @author zhangran + * @date 2021/7/13 + *

+ * 全局拦截器,作用所有的微服务 + *

+ * 1. 对请求的API调用过滤,记录接口的请求时间,方便日志审计、告警、分析等运维操作 + * 2. 后期可以扩展对接其他日志系统 + *

+ */ +@Slf4j +@Component +public class ApiLoggingFilter implements GlobalFilter, Ordered { + + private static final String START_TIME = "startTime"; + private static final String X_REAL_IP = "X-Real-IP";//nginx需要配置 + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + String info = String.format("Method:{%s} Host:{%s} Path:{%s} Query:{%s}", + exchange.getRequest().getMethod().name(), + exchange.getRequest().getURI().getHost(), + exchange.getRequest().getURI().getPath(), + exchange.getRequest().getQueryParams()); + if (log.isDebugEnabled()) { + log.debug(info); + } + exchange.getAttributes().put(START_TIME, System.currentTimeMillis()); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + Long startTime = exchange.getAttribute(START_TIME); + if (startTime != null) { + Long executeTime = (System.currentTimeMillis() - startTime); + List ips = exchange.getRequest().getHeaders().get(X_REAL_IP); + String ip = ips != null ? ips.get(0) : null; + String api = exchange.getRequest().getURI().getRawPath(); + int code = exchange.getResponse().getStatusCode() != null ? exchange.getResponse().getStatusCode().value() : 500; + //当前仅记录日志,后续可以添加日志队列,来过滤请求慢的接口 + if (log.isDebugEnabled()) { + log.debug("来自IP地址:{}的请求接口:{},响应状态码:{},请求耗时:{}ms", ip, api, code, executeTime); + } + } + })); + } + + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE; + } + +}