GateWay具体的使用之局部过滤器接口耗时

server/2024/9/24 7:54:50/

1.找规律

局部过滤器命名规则 XXXGatewayFilterFactory, 必须以GatewayFilterFactory结尾。

/*  注意名称约定
*   AddRequestHeaderGatewayFilterFactory    配置的时候写的是 AddRequestHeader
*   AddRequestParameterGatewayFilterFactory 配置的时候写的是 AddRequestParameter
*   LogTimeGatewayFilterFactory   配置的时候写什么? 
* */

2.接口耗时过滤器

package com.by.filter;import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.AddRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.support.GatewayToStringStyler;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;@Component
@Slf4j
public class LogTimeGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {public GatewayFilter apply(final AbstractNameValueGatewayFilterFactory.NameValueConfig config) {return new GatewayFilter() {public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {String value = ServerWebExchangeUtils.expand(exchange, config.getValue());int times = Integer.parseInt(value);long start = System.currentTimeMillis();return chain.filter(exchange).then(Mono.fromRunnable(() -> {long end = System.currentTimeMillis();long time = end - start;if(time>times*1000){log.info("请求耗时过长,耗时:{}",time);}}));}public String toString() {return GatewayToStringStyler.filterToStringCreator(LogTimeGatewayFilterFactory.this).append(config.getName(), config.getValue()).toString();}};}
}

3.如何使用


http://www.ppmy.cn/server/20361.html

相关文章

day04--react中state的简化

一、简化state 回顾我们之前的写法&#xff1a; state是在构造器里面定义的。 1&#xff09;我们为什么要在构造器里面定义&#xff1f; 答&#xff1a;对于创建一个实例对象时&#xff0c;我们对要传进来的数据进行接收&#xff0c;那么我们必须要写一个构造器来接收传进来的…

GCC编译器介绍及编译流程说明

一、计算机基础 1、冯诺依曼模型 1945年冯诺依曼和一些科学家提出了一份报告,报告遵循了图灵机的设计&#xff0c;并提出用电子元件构造计算机&#xff0c;约定了用二进制进行计算和存储&#xff0c;并且将计算机结构分成运算器&#xff0c;控制器、存储器、输入设备、输出设备…

旅游网站制作流程

旅游网站制作流程是一个较复杂的过程&#xff0c;因为它需要结合市场调研、用户需求、内容构建、技术开发等多个方面。在这篇文章中&#xff0c;我将简单介绍一下旅游网站的制作流程&#xff0c;大致分为以下步骤。 第一步&#xff1a;市场调研 在制作旅游网站前&#xff0c;我…

数据结构:实验六:图的操作

一、 实验目的 &#xff08;1&#xff09;掌握图的邻接矩阵和邻接表存储结构。 &#xff08;2&#xff09;熟练图的邻接表的基本运算。 &#xff08;3&#xff09;加深图的深度优先遍历算法和广度优先遍历算法的理解 二、 实验要求 有下图所示的带权有向图及其对应的邻…

探索和构建 LLaMA 3 架构:深入探讨组件、编码和推理技术(十)

探索和构建 LLaMA 3 架构&#xff1a;深入探讨组件、编码和推理技术&#xff08;十&#xff09; Llama 推理 为了对模型进行推理&#xff0c; 需要从Meta的LLaMA 3仓库下载模型的权重。 编写模型推理的代码。在推理模型时&#xff0c;有许多可调参数需要考虑&#xff0c;包括…

MySQL数据库安装——zip压缩包形式

安装压缩包zip形式的 MySQL 8数据库 一 、先进入官网下载 https://dev.mysql.com/downloads/mysql/ 二、解压到某个文件夹 我解压到了D:\mysql\mysql8 下面 然后在这个文件夹下手动创建 my.ini 文件和 data 文件夹 my.ini 内容如下&#xff1a; 注意 basedir 和 datadi…

【机器学习原理】决策树从原理到实践

基于树的模型是机器学习中非常重要的一类模型&#xff0c;最基础的就是决策树&#xff0c;本篇主要讲述决策树的原理和几类最常见的决策树算法&#xff0c;这也是更复杂的树模型算法的基础。 参考文章&#xff1a; 1.CSDN-基于熵的两个模型(ID3,C4.5)比较详细&#xff0c;有数字…

什么ISP是住宅IP,和普通IP有什么区别?

ISP&#xff08;Internet Service Provider&#xff09;即互联网服务提供商&#xff0c;是向广大用户综合提供互联网接入业务、信息业务和增值业务的电信运营商。住宅IP&#xff0c;也称为家庭IP&#xff0c;是指由ISP分配给家庭或个人用户的IP地址。这些IP地址是真实的&#x…