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

相关推荐
yuluo_YX13 天前
AI Gateway 介绍
人工智能·gateway
网硕互联的小客服16 天前
502 Bad Gateway:服务器作为网关或代理时收到无效响应处理方式
运维·服务器·gateway
sevevty-seven16 天前
什么是Gateway
gateway
秋の花16 天前
【GateWay】和权限验证
java·gateway
欧先生^_^18 天前
org.springframework.cloud.gateway 组件解释
gateway
jarenyVO19 天前
Spring Cloud Gateway 全面学习指南
java·gateway
保持学习ing21 天前
微服务--Gateway网关
java·网关·微服务·gateway
SZ17011023122 天前
IGP(Interior Gateway Protocol,内部网关协议)
运维·服务器·gateway
肥仔哥哥193022 天前
SpringCloud2025+SpringBoot3.5.0+gateway+webflux子服务路由报503
微服务·gateway·最新微服务
亚林瓜子1 个月前
AWS API Gateway配置日志
云计算·gateway·aws·log·cloudwatch