在前文中,我们介绍了Spring Cloud Gateway内置了一系列的全局过滤器,本文介绍如何自定义全局过滤器。

自定义全局过滤需要实现GlobalFilter 接口,该接口和 GatewayFilter 有一样的方法定义,只不过 GlobalFilter 的实例会作用于所有的路由。

自定义全局过滤器

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; /**
* 自定义全局过滤器
*/
@Slf4j
@Component
public class CustomizeGlobalFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String path = exchange.getRequest().getURI().getPath();
log.info("[CustomizeGlobalFilter] 访问的接口:{}", path); long start = System.currentTimeMillis();
return chain.filter(exchange)
// then的内容会在过滤器返回的时候执行,即最后执行
.then(Mono.fromRunnable(() ->
log.info("[ {} ] 接口的访问耗时:{} /ms", path, System.currentTimeMillis() - start)
));
}
}

上述代码中,我们使用@Component注解使该全局过滤器生效。还有一种方式,就是使用一个专门的配置类去实例化这些全局过滤器并交给Spring容器管理,代码如下:

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; @Slf4j
@Configuration
public class FilterConfig { @Bean
// 该注解用于指定过滤器的执行顺序,数字越小越优先执行
@Order(Ordered.HIGHEST_PRECEDENCE)
public GlobalFilter myGlobalFilter(){
log.info("create myGlobalFilter...");
return new MyGlobalFilter();
}
}

Spring Cloud Alibaba学习笔记(21) - Spring Cloud Gateway 自定义全局过滤器的更多相关文章

  1. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  2. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  3. Spring Cloud Alibaba学习笔记(1) - 整合Spring Cloud Alibaba

    Spring Cloud Alibaba从孵化器版本毕业:https://github.com/alibaba/spring-cloud-alibaba,记录一下自己学习Spring Cloud Al ...

  4. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

  5. Spring Cloud Alibaba学习笔记(3) - Ribbon

    1.手写一个客户端负载均衡器 在了解什么是Ribbon之前,首先通过代码的方式手写一个负载均衡器 RestTemplate restTemplate = new RestTemplate(); // ...

  6. Spring Cloud Alibaba学习笔记(22) - Nacos配置管理

    目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...

  7. Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用

    Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...

  8. Spring Cloud Alibaba学习笔记(2) - Nacos服务发现

    1.什么是Nacos Nacos的官网对这一问题进行了详细的介绍,通俗的来说: Nacos是一个服务发现组件,同时也是一个配置服务器,它解决了两个问题: 1.服务A如何发现服务B 2.管理微服务的配置 ...

  9. Spring Cloud Alibaba学习笔记

    引自B站楠哥:https://space.bilibili.com/434617924 一.创建父工程 创建父工程hello-spring-cloud-alibaba Spring Cloud Ali ...

随机推荐

  1. 面试问我 Java 逃逸分析,瞬间被秒杀了。。

    记得几年前有一次栈长去面试,问到了这么一个问题: Java中的对象都是在堆中分配吗?说明为什么! 当时我被问得一脸蒙逼,瞬间被秒杀得体无完肤,当时我压根就不知道他在考什么知识点,难道对象不是在堆中分配 ...

  2. Java 自定义异常

    新建类CustomException继承 Exception /** * Create by on 2019-07-30 * 自定义类需要继承Exception * @author lsw */ pu ...

  3. edusoho上传视频弹出abort之解决方案

    错误描述:edusoho上传如avi.mp4等容量大的图片(如100m以上或500m等)弹出abort提示框 原因:是因为web服务器apache默认上传文件有限制导致的 解决办法如下: (1)首先修 ...

  4. react项目如何运行

    react项目如何运行 一.总结 一句话总结: npm i 安装好package.json的 指定插件后,npm start 启动项目 二.react项目的安装与运行 转自或参考:react项目的安装 ...

  5. EFProf用法

    SQL Server Profiler用来跟踪应用程序发送到SQL Server中的SQL语句,用于检测性能,查找问题.Entity Framework 也有它的跟踪工具EFProf,用于跟踪Enti ...

  6. pycharm项目添加.gitignore忽略.idea文件夹

    本地项目结构: .gitignore文件中添加: at_alsv_pro/.idea/SearchImage.iml at_alsv_pro/.idea/misc.xml at_alsv_pro/.i ...

  7. H3C Telnet 配置

    Telnet 配置管理方法是网络工程师和网络管理员使用最广泛的一种设备访问控制方法,它通过局域网或广域网实现本地或远程的访问控制,但是它的实验必须要求首先对设备进行初始化配置,否则用户无法正常登录和访 ...

  8. [LeetCode] 12. Integer to Roman 整数转为罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  9. 深入理解-CLI与PHP-FPM

    原文地址:https://blog.csdn.net/lzx_victory/article/details/85917161 PHP-FPM模式相对于CLI比较复杂,因为PHP-FPM为常驻进程不断 ...

  10. C++中int型数据的十六进制写法

    C++中int类型数据的十六进制写法 在C++中,int类型一般表示一个有符号的32位整数.在一些情况下(例如进行位操作时)我们需要使用十六进制的方式来表示int类型.int32的取值范围:最大值:2 ...