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

这篇具有很好参考价值的文章主要介绍了GateWay具体的使用之局部过滤器接口耗时。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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.如何使用

GateWay具体的使用之局部过滤器接口耗时,gateway文章来源地址https://www.toymoban.com/news/detail-861008.html

到了这里,关于GateWay具体的使用之局部过滤器接口耗时的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Gateway自定义过滤器——全局过滤器

    首先,我们要知道全局过滤器其实是特殊路由过滤器(特殊的GatewayFilter),会有条件地作用于所有路由。 为什么要自定义全局过滤器?就好比是看大门的保安大叔,平时主要是做好进出大门外来人员登记即可,但是因为新冠疫情,现在还需要给外来人员测量体温等等。而已有的

    2024年02月16日
    浏览(34)
  • gateway-过滤器执行顺序

    请求进入网关会碰到三类过滤器:当前路由过滤器、DefaultFilter、GlobalFilter。 请求路由后,会将当前路由过滤器和DefaultFilter、GlobalFilter,合并到一个过滤器链(集合)中,排序后依次执行每个过滤器 过滤器执行顺序 1.每一个过滤器都必须指定一个int类型的order值,order值越小

    2024年02月13日
    浏览(37)
  • Gateway网关 全局过滤器

    一、全局过滤器 全局过滤器GlobalFilter 全局过滤器的作用也是处理一切进入网关的请求和微服务响应,与GatewayFilter的作用一样。 区别在于GatewayFilter通过配置定义,处理逻辑是固定的。 需求:定义全局过滤器,拦截请求,判断请求的参数是否满足下面条件: 参数中是否有au

    2024年02月07日
    浏览(39)
  • gateway之过滤器(Filter)详解

    在Spring Cloud中,过滤器(Filter)是一种关键的组件,用于在微服务架构中处理和转换传入请求以及传出响应。过滤器位于服务网关或代理中,并通过拦截请求和响应流量来提供各种功能。 过滤器在请求的不同生命周期阶段执行特定的操作,例如鉴权、认证、请求转发、限流、

    2024年02月05日
    浏览(33)
  • gateway过滤器没生效,特殊原因

    看这边文章的前提,你要会gateway,知道过滤器怎么配置? 直接来看过滤器,局部过滤器 再来看配置 请求路径 http://127.0.0.1:8080/appframework/services/catalog/catalogSpecials.json?pageindex=1pagesize=10pkid=d9873700ef7e42b3b8f4e782f345975b 看起来确实没什么问题 注意: 我这里还有个应用,就是网关转

    2024年02月14日
    浏览(43)
  • Spring Cloud Gateway 过滤器

    Spring Cloud Gateway 过滤器的种类有30多种。 官文文档地址: Spring Cloud Gateway https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories Spring Cloud Gateway大体可以分为下面两种类型的过滤器: 1、内置的过滤器         1.1、内置的局部过滤器         1.2、内置的全

    2024年03月28日
    浏览(46)
  • Spring Cloud GateWay 全局过滤器

    这是一个自定义的 Spring Cloud Gateway 全局过滤器(Global Filter)。在 Spring Cloud Gateway 中,全局过滤器可以在请求被路由到目标服务之前或之后执行一些操作。这个过滤器实现了 GlobalFilter 接口和 Ordered 接口,这两个接口的作用如下: GlobalFilter 接口: 这是一个 Spring Cloud Gateway 提

    2024年02月11日
    浏览(33)
  • Spring Cloud Gateway 过滤器详解

    Spring Cloud Gateway根据作用范围划分为:GatewayFilter和GlobalFilter 由filter工作流程点,可以知道filter有着非常重要的作用,在“pre”类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等,在“post”类型的过滤器中可以做响应内容、响应头的修改,日志的输

    2023年04月08日
    浏览(30)
  • gateway过滤器中实现记录访问日志

    SpringCloud多服务项目环境,前端请求经过网关中转发到各个服务节点。日志中需要记录请求头中的部分参数、请求的body、响应状态及响应内容,并在请求头中新增一个标识。 PS: 1.Order 最高优先级。 2.Request Header 不可添加值,需重新创建后绑定。 3.Request Body IO流,读取一次后

    2024年02月15日
    浏览(32)
  • 云原生之 Gateway 的 Filter 过滤器

    通常情况下,出于安全方面的考虑, 服务端提供的服务往往都会有一定的校验逻辑 ,例如用户登陆状态校验、签名校验等。 在微服务架构中,系统由多个微服务组成,所有这些服务都需要这些校验逻辑,此时我们就可以将 这些校验逻辑写到 Spring Cloud Gateway 的 Filter 过滤器中

    2023年04月14日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包