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

相关推荐
Ribou21 小时前
Cilium Gateway API 配置手册
gateway
YJlio2 天前
OpenClaw v2026.4.8 更新解析:扩展加载修复、通道配置优化、Slack 代理支持与升级避坑
gateway·自动化运维·版本更新·ai agent·openclaw·slack·插件兼容
weixin_lizhao3 天前
50天独立打造企业级API网关(二):安全防护体系与弹性设计
java·spring boot·安全·spring cloud·gateway
YJlio3 天前
OpenClaw v2026.4.9 更新解析:Memory Dreaming、Control UI、安全修复、插件依赖与升级避坑
gateway·memory·自动化运维·版本更新·ai agent·openclaw·dreaming
code_pgf4 天前
openclaw的gateway详解
人工智能·gateway
zx2859634005 天前
Laravel5.x版本革新特性全解析
mysql·gateway·智能路由器
nvd116 天前
企业级全场景 API 网关实践:基于 Kong Hybrid 模式的跨 VPC 部署与 GitOps 治理
gateway·kong
nvd116 天前
深度解析:Kong Hybrid 模式与 KIC (Gateway API) 架构演进与核心异同
架构·gateway·kong
zx2859634006 天前
Laravel10.x重磅升级:核心特性全解析
mysql·gateway·智能路由器
暗夜猎手-大魔王7 天前
转载--AI Agent 架构设计:Gateway 架构设计(OpenClaw、Claude Code、Hermes Agent 对比)
gateway