GateWay检查接口耗时

添加gateway依赖

java 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

创建一个LogTimeGateWayFilterFactory类,可以不是这个名字但是后面必须是xxxGateWayFilterFactory

然后继承AbstractNameValueGatewayFilterFactory

java 复制代码
@Component
@Slf4j
public class LogTimeGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange,GatewayFilterChain chain) {
                //获取配置的超时时间
                String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
                Integer timespan = Integer.valueOf(value);
                //获取开始时间
                long start = System.currentTimeMillis();
                // 往下执行完并返回后   操作.then对执行结果操作完之后在返回
                return chain.filter(exchange).then(Mono.fromRunnable(()->{
                    //获取结束时间
                    long end = System.currentTimeMillis();
                    //获取请求耗时
                    long time = end - start;
                    //判断是否超时
                    if(time > timespan*1000){
                        log.debug("请求耗时:{}ms",time);
                    }
                }));
            }

            @Override
            public String toString() {
                return filterToStringCreator(LogTimeGatewayFilterFactory.this)
                        .append(config.getName(), config.getValue()).toString();
            }
        };
    }
}

配置文件里配置,LogTime就是类名,但是不能带着GateWayFilterFactory

相关推荐
angushine10 天前
Gateway获取下游最终响应码
java·开发语言·gateway
鬼布10 天前
post请求在gateway打印日志内存遗漏
java·gateway
TE-茶叶蛋10 天前
2025-Gateway架构
架构·gateway
小样vvv11 天前
【微服务管理】深入理解 Gateway 网关:原理与实现
微服务·gateway
sky.fly11 天前
在思科模拟器show IP route 发现Gateway of last resort is not set没有设置最后的通道
网络协议·tcp/ip·gateway
angushine12 天前
Gateway统一修改响应内容
gateway
曾经的三心草12 天前
Gateway-网关-分布式服务部署
gateway·分布式服务部署
云攀登者-望正茂14 天前
通过AWS WAF Rate limit rule来保护API Gateway
网络·gateway·aws
小怪瘦7916 天前
IDEA :物联网ThingsBoard-gateway配置,运行Python版本,连接thingsboard,接入 MQTT 设备
python·物联网·gateway·idea
字节源流18 天前
【Spring Cloud Netflix】GateWay服务网关
java·运维·gateway