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

相关推荐
惊鸿一博6 小时前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
hanniuniu1317 小时前
网络安全厂商F5推出AI Gateway,化解大模型应用风险
人工智能·web安全·gateway
stormsha20 小时前
Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统
服务器·网络·网络安全·gateway
背太阳的牧羊人1 天前
backend 服务尝试连接 qdrant 容器,但失败了,返回 502 Bad Gateway 问题排查
docker·gateway·qdrant
曼彻斯特的海边2 天前
RequestRateLimiterGatewayFilterFactory
spring cloud·gateway·限流
·云扬·2 天前
【PmHub面试篇】Gateway全局过滤器统计接口调用耗时面试要点解析
面试·职场和发展·gateway
devil_mf2 天前
gateway 网关 路由新增 (已亲测)
gateway
wmd131643067122 天前
Gateway 搭建
gateway
KubeSphere 云原生4 天前
云原生周刊:探索 Gateway API v1.3.0
云原生·gateway
曼彻斯特的海边5 天前
spring-cloud-alibaba-sentinel-gateway
gateway·sentinel·springcloud