spring cloud gateway提供了很多内置的过滤器,那么因为需求的关系,需要自定义实现,并且要可配置,在一番折腾之后,总算是解决了,那么久记录下来

对于自定义的factory,我们可以选择去实现接口或继承已有的抽象类,相关的接口是GatewayFilterFactory,而springboot默认帮我们实现的抽象类是AbstractGatewayFilterFactory这个

首先来看我们的自定义的过滤器工厂类代码

/**
* Description: it's purpose...
*
* @author a 2019-5-22
* @version 1.0
*/
public class ExampleGatewayFilterFactory extends AbstractGatewayFilterFactory<ExampleGatewayFilterFactory.Config> { /**
* 定义可以再yaml中声明的属性变量
*/
private static final String TYPE = "type";
private static final String OP = "op"; /**
* constructor
*/
public ExampleGatewayFilterFactory(){
// 这里需要将自定义的config传过去,否则会报告ClassCastException
super(Config.class);
} @Override
public List<String> shortcutFieldOrder() {
return Arrays.asList(TYPE, OP);
} @Override
public GatewayFilter apply(Config config) {
return ((exchange, chain) -> {
boolean root = "root".equals(config.getOp());
if (root){
LogUtil.info("GatewayFilter root");
}
else {
LogUtil.info("GatewayFilter customer");
}
// 在then方法里的,相当于aop中的后置通知
return chain.filter(exchange).then(Mono.fromRunnable(()->{
// do something
}));
});
} /**
* 自定义的config类,用来设置传入的参数
*/
public static class Config { /**
* 过滤类型
*/
private String type; /**
* 操作
*/
private String op; public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getOp() {
return op;
} public void setOp(String op) {
this.op = op;
}
}
}

因为我使用的是springboot,那么需要在启动类里,将这个工厂注入到spring容器当中

@Bean
public ExampleGatewayFilterFactory exampleGatewayFilterFactory(){
return new ExampleGatewayFilterFactory();
}

然后是yaml的配置,这里需要注意的是,之前我一直失败,在调用gateway会报告找不到对应的过滤率,这是因为命名导致的

springboot约定过滤器的前缀为配置的name,而后面最好统一都是GatewayFilterFactory

spring:
application:
name: demo-gateway
cloud:
gateway:
routes:
- id: custom
uri: lb://demo-consumer
predicates:
- Path=/account/v1/**
filters:
- name: Example
args:
op: root
type: he

到这里,过滤器的编写与配置就完成了,然后启动项目,访问/account/v1/test,就会在日志里看到在过滤器中打印的信息了

注意我们通过这种工厂创建出来的过滤器是没有指定order的,会被默认设置为是0,配置在yml文件中,则按照它书写的顺序来执行

如果想要在代码中设置好它的顺序,工厂的apply方法需要做一些修改

@Override
public GatewayFilter apply(Config config) {
return new InnerFilter(config);
} /**
* 创建一个内部类,来实现2个接口,指定顺序
*/
private class InnerFilter implements GatewayFilter, Ordered { private Config config; InnerFilter(Config config) {
this.config = config;
} @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println(" pre 自定义过滤器工厂 AAAA " + this.getClass().getSimpleName());
boolean root = "root".equals(config.getOp());
if (root) {
System.out.println(" is root ");
} else {
System.out.println(" is no root ");
}
// 在then方法里的,相当于aop中的后置通知
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println(" post 自定义过滤器工厂 AAAA " + this.getClass().getSimpleName());
}));
} @Override
public int getOrder() {
return -100;
}
} // config类的代码同上面一样,不再展示了

spring cloud gateway 自定义GatewayFilterFactory的更多相关文章

  1. spring cloud gateway自定义过滤器

    在API网关spring cloud gateway和负载均衡框架ribbon实战文章中,主要实现网关与负载均衡等基本功能,详见代码.本节内容将继续围绕此代码展开,主要讲解spring cloud g ...

  2. Spring Cloud Alibaba学习笔记(19) - Spring Cloud Gateway 自定义过滤器工厂

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的内置过滤器工厂,若Spring Cloud Gateway内置的过滤器工厂无法满足我们的业务需求,那么此时就需要自定义自己的过 ...

  3. Spring Cloud Alibaba学习笔记(21) - Spring Cloud Gateway 自定义全局过滤器

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的全局过滤器,本文介绍如何自定义全局过滤器. 自定义全局过滤需要实现GlobalFilter 接口,该接口和 GatewayFi ...

  4. Spring Cloud Alibaba学习笔记(17) - Spring Cloud Gateway 自定义路由谓词工厂

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的路由谓词工厂,但是如果这些内置的路由谓词工厂不能满足业务需求的话,我们可以自定义路由谓词工厂来实现特定的需求. 例如有某个服务 ...

  5. Spring cloud gateway自定义filter以及负载均衡

    自定义全局filter package com.example.demo; import java.nio.charset.StandardCharsets; import org.apache.co ...

  6. Spring Cloud Gateway自定义过滤器实战(观测断路器状态变化)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. Spring Cloud Gateway自定义异常处理Exception Handler

    版本: Spring Cloud 2020.0.3 常见的方法有 实现自己的 DefaultErrorWebExceptionHandler 或 仅实现ErrorAttributes. 方法1: Er ...

  8. spring cloud gateway获取response body

    网关发起请求后,微服务返回的response的值要经过网关才发给客户端.本文主要讲解在spring cloud gateway 的过滤器中获取微服务的返回值,因为很多情况我们需要对这个返回进行处理.网 ...

  9. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

随机推荐

  1. kafka命令及启动

    默认内网访问,要在外网访问的话,需要在修改config/server.properties中的配置 将listeners和advertised.listeners的值用主机名进行替换,在外用使用jav ...

  2. 自搭建jetbrains系列ide授权服务器

    1.下载 LicenseServer 地址:https://mega.nz/#!7B5UVY6b!Hae2ceTBPIrTowQN0sV9fQ5lGOKzGxas2ug02RZAdGU,里面有不同的服 ...

  3. SpringBoot基于EasyExcel解析Excel实现文件导出导入、读取写入

    1. 简介   Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题 ...

  4. Flink读写Redis(二)-flink-redis-connector代码学习

    源码结构 RedisSink package org.apache.flink.streaming.connectors.redis; import org.apache.flink.configur ...

  5. Loading class `com.mysql.jdbc.Driver'. This is deprecated警告处理

    com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 mysql客户端6以后,数据库驱动com.mysql.jdbc.Driver'已经被弃用了.应当 ...

  6. AdaBoost 算法-分析波士顿房价数据集

    公号:码农充电站pro 主页:https://codeshellme.github.io 在机器学习算法中,有一种算法叫做集成算法,AdaBoost 算法是集成算法的一种.我们先来看下什么是集成算法. ...

  7. Spark内核-任务调度机制

    作者:十一喵先森 链接:https://juejin.im/post/5e1c414fe51d451cad4111d1 来源:掘金 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. ...

  8. 2020软件测试工程师面试题汇总(内含答案)-看完BATJ面试官对你竖起大拇指!

    测试技术面试题 1.什么是兼容性测试?兼容性测试侧重哪些方面? 参考答案: 兼容测试主要是检查软件在不同的硬件平台.软件平台上是否可以正常的运行,即是通常说的软件的可移植性. 兼容的类型,如果细分的话 ...

  9. 彻底理解Spring如何解决循环依赖

    Spring bean生命周期 可以简化为以下5步. 1.构建BeanDefinition 2.实例化 Instantiation 3.属性赋值 Populate 4.初始化 Initializati ...

  10. tkinter + 爬虫 实现影视在线资源系统

    应吾爱朋友现公布代码如下: import tkinter as tk import requests,re,sys,asyncio from tkinter import scrolledtext,E ...