在前文中,我们介绍了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. 在安卓手机下按钮会悬浮在键盘上,怎么解决vue.js

    data里面 screenHeight: window.innerHeight mounted里面 mounted () { var that = this var u = navigator.use ...

  2. CTF CMS(转)

    CTF--CMS漏洞总结 海洋CMS 6.28 海洋CMS6.28命令执行漏洞 6.45-6.54 漏洞预警 | 海洋CMS(SEACMS)0day漏洞预警 8.8(未验证) 海洋cms前台到后台的g ...

  3. EF 调试跟踪生成的SQL语句

    IQueryable query = from x in appEntities select x; var sql = ((System.Data.Objects.ObjectQuery)query ...

  4. RedisHelper Redis帮助类

    using StackExchange.Redis; using System; using System.Collections.Generic; using System.IO; using Sy ...

  5. spring cloud Eureka server 问题 Spring Cloud java.lang.TypeNotPresentException

    版本: spring-cloud.version : Greenwich.SR2 pom配置: <project xmlns="http://maven.apache.org/POM/ ...

  6. IntelliJ IDEA添加JavaDOC注释 方法 快捷键

    第一种方法 Settings ->Keymap ->Other ->Fix doc comment ->右键 ->选择 Add Keyboard Shortcut, 然后 ...

  7. win10无法运行phantomjs

    @参考文章 echarts利用phantomjs将 图片保存到电脑,在win7下好好的,在win10下报错: 'phantomjs' 不是内部或外部命令,也不是可运行的程序或批处理文件. 在win7下 ...

  8. 一码学程 10284 排队找bug 题解 单调队列 或者 线段树RMQ

    注:只是看到题目,未评测,所以不确定代码正确性,但是算法思路没有问题 描述 同学们的bug还真是多啊,orz... 春节期间大家存下的bug都来找肖老师解决了. 每个人都有bug,但是肖老师却只有一个 ...

  9. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  10. [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...