今天我们来学习下GatewayFilter Factory,中文解释就是过滤器工厂。

官方文档对GatewayFilter Factory的介绍:

Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories.

文档地址:http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html#_gatewayfilter_factories

英文不好,就随意翻译下,大概的意思如下:

Spring Cloud Gateway的路由过滤器允许以某种方式修改传入的HTTP请求或输出的HTTP响应。只作用于特定的路由。Spring Cloud Gateway中内置了很多的过滤器工厂。

通过目前的文档,我看到了内置的数量为20个,今天我们来学习第一个过滤器工厂AddRequestHeader GatewayFilter Factory。

通过名称我们可以快速的明白这个过滤器工厂的作用,就是添加请求头。

使用示列:

spring:
cloud:
gateway:
routes:
- id: fsh-house
uri: lb://fsh-house
predicates:
- Path=/house/**
filters:
- AddRequestHeader=NAME, yinjihuan

上面的配置,我们针对fsh-house这个服务的路由配置了AddRequestHeader,增加了一个名称为NAME,值是yinjihuan的请求头。

这样配置之后,在fsh-house服务中的所有接口,都可以通过request来获取到NAME请求头的信息,代码如下:

@GetMapping("/hello")
public String hello(HttpServletRequest request) throws Exception {
System.err.println(request.getHeader("NAME"));
return "Hello"+serverPort;
}

如果我们需要传递多个请求头咋配置?

 filters:
- AddRequestHeader=NAME, yinjihuan
- AddRequestHeader=NAME2, yinjihuan2

配置2个过滤器就行了,是不支持一个过滤器配置多个请求头的方式,原因我们通过源码可以得出,请看源码:

public class AddRequestHeaderGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {

	@Override
public GatewayFilter apply(NameValueConfig config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest().mutate()
.header(config.getName(), config.getValue())
.build(); return chain.filter(exchange.mutate().request(request).build());
};
} }

NameValueConfig就2个字段,一个name,一个value,对应的也就是请求头的名称和值。

类似的工厂类还有AddRequestParameter和AddResponseHeader,RemoveRequestHeader,RemoveResponseHeader,这几个就不做单独讲解了,使用方式是一样的。一个是添加请求参数,一个是添加响应的请求头。

AddRequestParameter配置示列:

 filters:
- AddRequestParameter=name, yinjihuan

AddResponseHeader配置示列:

 filters:
- AddResponseHeader=name, yinjihuan

讨论时间

文章中讲的这几个工厂类的作用我们已经了解了,那具体的使用场景有哪些?适合在什么场景下使用呢?欢迎大家留言讨论。

欢迎加入我的知识星球,一起交流技术,免费学习猿天地的课程(http://cxytiandi.com/course)

Spring Cloud Gateway 之 AddRequestHeader GatewayFilter Factory的更多相关文章

  1. Spring Cloud Gateway GatewayFilter的使用

    Spring Cloud Gateway GatewayFilter的使用 一.GatewayFilter的作用 二.Spring Cloud Gateway内置的 GatewayFilter 1.A ...

  2. Spring Cloud Gateway Route Predicate Factory 的使用

    Spring Cloud Gateway的使用 一.需求 二.基本组成 1.简介 2.核型概念 1.Route 路由 2.Predicate 谓语.断言 3.Filter 过滤器 3.工作原理 三.网 ...

  3. Spring Cloud Gateway(九):网关过滤器 GatewayFilter

    本文基于 spring cloud gateway 2.0.1 1.简介 GatewayFilter 网关过滤器用于拦截并链式处理web请求,可以实现横切的与应用无关的需求,比如:安全.访问超时的设置 ...

  4. spring cloud gateway之filter篇

    转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理,在路由 ...

  5. 微服务网关 Spring Cloud Gateway

    1.  为什么是Spring Cloud Gateway 一句话,Spring Cloud已经放弃Netflix Zuul了.现在Spring Cloud中引用的还是Zuul 1.x版本,而这个版本是 ...

  6. Spring Cloud gateway 网关服务二 断言、过滤器

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

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

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

  8. Spring Cloud Alibaba学习笔记(18) - Spring Cloud Gateway 内置的过滤器工厂

    参考:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-clou ...

  9. Spring Cloud Gateway(十):网关过滤器工厂 GatewayFilterFactory

    本文基于 spring cloud gateway 2.0.1 1.GatewayFilterFactory 简介 路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应. 路径过滤器的范 ...

随机推荐

  1. re2c安装

    wget  https://kojipkgs.fedoraproject.org//packages/re2c/1.1.1/3.fc31/src/re2c-1.1.1-3.fc31.src.rpm 解 ...

  2. CodeForce 117C Cycle DFS

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  3. 线程池之ScheduledThreadPoolExecutor线程池源码分析笔记

    1.ScheduledThreadPoolExecutor 整体结构剖析. 1.1类图介绍 根据上面类图图可以看到Executor其实是一个工具类,里面提供了好多静态方法,根据用户选择返回不同的线程池 ...

  4. eclipse查看一个方法被谁调用的快捷键

    我们知道,在idea中是可以通过[ctrl+鼠标左键单击]去跳到方法调用方去的,但是在eclipse中却是不行的. 三种快捷键方式 这里列出在eclipse中查看一个方法被谁调用的三种方式(快捷键). ...

  5. 基于MicroPython结合ESP8266模块实现TCP通信(AT指令版)

    转载请注明文章来源,更多教程可自助参考docs.tpyboard.com,QQ技术交流群:157816561,公众号:MicroPython玩家汇 实验目的 - 学习ESP8266模块(ESP-01) ...

  6. 单独KafkaConsumer实例and多worker线程。

    1.单独KafkaConsumer实例and多worker线程.将获取的消息和消息的处理解耦,将消息的处理放入单独的工作者线程中,即工作线程中,同时维护一个或者若各干consumer实例执行消息获取任 ...

  7. NET 特性(Attribute)

    NET 特性(Attribute) 转自 博客园(Fish) 特性(Attribute):是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签. 您可以通过使用特 ...

  8. SQLi-LABS Page-2 (Adv Injections) Less23-Less26

    Less-23 GET - Error based - strip comments http://10.10.202.112/sqli/Less-23?id=1' Warning: mysql_fe ...

  9. 软工Alpha七天冲刺

    七天冲刺博客: 1.第一篇Scrum冲刺博客 2.第二篇Scrum冲刺博客 3.第三篇Scrum冲刺博客 4.第四篇Scrum冲刺博客 5.第五篇Scrum冲刺博客 6.第六篇Scrum冲刺博客 7. ...

  10. DotNet Core管道通信

    前言 在之前,我们需要明确的一个概念是, Web 程序中,用户的每次请求流程都是线性的,放在 ASP.NET Core 程序中,都会对应一个 请求管道(request pipeline),在这个请求管 ...