小工具知识网

当前位置: 首页 > 行业知识 > 两天学会微服务网关Gateway-Gateway过滤器

行业知识

两天学会微服务网关Gateway-Gateway过滤器

2026-11-12 11:03:09 来源:互联网转载

锋哥原创���微服务网关Gateway视频教程:

Gateway微服务网关视频教程(无废话版)_哔哩哔哩_bilibiliGateway微服务网关视频教程(无废话版)共计17条视频,包括:1_Gateway简介、2_Gateway工作原理、3_Gateway HelloWorld快速入门等,UP主更多精彩视频,请关注UP账号。https://www.bilibili.com/video/BV1oX4y1u7ZJ/Gateway分为Pre类型的过滤器和Post类型的过滤器。

  • Pre类型的过滤器在请求转发到后端微服务之前执行,在Pre类型过滤器链中可以做鉴权、限流等操作。

  • Post类型的过滤器在请求执行完成之后、将结果返回给客户端之前执行。

    在Spring Cloud Gateway中内置了很多Filter,Filter有两种实现,分别是GatewayFilter和GlobalFilter。GlobalFilter全局过滤器会应用到所有的路由上,而GatewayFilter只会应用到单个路由或者一个分组的路由上。

    5.1 Gateway过滤器工厂介绍

    过滤器 有 20 多个 实现 类, 包括 头部 过滤器、 路径 类 过滤器、 Hystrix 过滤器 和 变更 请求 URL 的 过滤器, 还有 参数 和 状态 码 等 其他 类型 的 过滤器。

    内置的过滤器工厂有22个实现类,包括 头部过滤器、路径过滤器、Hystrix 过滤器 、请求URL 变更过滤器,还有参数和状态码等其他类型的过滤器。根据过滤器工厂的用途来划分,可以分为以下几种:Header、Parameter、Path、Body、Status、Session、Redirect、Retry、RateLimiter和Hystrix

    5.2 AddRequestParameter GatewayFilter Factory

    spring:  application:    name: gateway-server  cloud:    gateway:      routes:       - id: add_request_parameter_route         uri: http://localhost:8080/         predicates:          - Path=/product/**         filters:          - AddRequestParameter=info, hehe

    添加请求参数(属于前置过滤网关)

    5.3 RewritePath GatewayFilter Factory

    spring:  application:    name: gateway-server  cloud:    gateway:      routes:       - id: add_request_parameter_route         uri: http://localhost:8080/         predicates:          - Path=/product/**, /api-gateway/**         filters:          - AddRequestParameter=info, hehe          - RewritePath=/api-gateway(?/?.*), $\{segment}

    将/api-gateway/product/请求重写成/product/请求(属于前置过滤网关)

    5.4 SetStatus GatewayFilter Factory

    spring:  application:    name: gateway-server  cloud:    gateway:      routes:       - id: add_request_parameter_route         uri: http://localhost:8080/         predicates:          - Path=/product/**, /api-gateway/**         filters:          - AddRequestParameter=info, hehe          - RewritePath=/api-gateway(?/?.*), $\{segment}          - SetStatus=456

    无论哪种情况,响应的HTTP状态都设置为456(属于后置过滤网关)

    5.5 AddResponseHeader GatewayFilter Factory

    spring:  application:    name: gateway-server  cloud:    gateway:      routes:       - id: add_request_parameter_route         uri: http://localhost:8080/         predicates:          - Path=/product/**, /api-gateway/**         filters:          - AddRequestParameter=info, hehe          - RewritePath=/api-gateway(?/?.*), $\{segment}          - SetStatus=456          - AddResponseHeader=X-Response-Author, java1234

    返回信息,添加header头信息(属于后置过滤网关)

    5.6 gateway全局过滤器 GlobalFilter

    全局过滤器不需要在配置文件中配置,作用在所有的路由上。我们可以用它来实现很多统一化处理的业务需求,比如负载均衡,统一过滤,路径转发,监控,日志等等。

    全局过滤器加上网关过滤器组成过滤器链,该过滤器链的执行顺序是根据@Order注解指定的数字大小,从小到大进行排序,数字越小,优先级越高。

    5.7 自定义过滤器

    Spring Cloud Gateway提供了过滤器的扩展功能,开发者可以根据实际业务需求来自定义GatewayFilter网关过滤器或者GlobalFilter全局过滤器。

    5.7.1 自定义GatewayFilter

    实现GatewayFilter,Ordered接口,以及配置类

    package com.java1234.filter;import org.springframework.cloud.gateway.filter.GatewayFilter;import org.springframework.cloud.gateway.filter.GatewayFilterChain;import org.springframework.core.Ordered;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;/** * 自定义网关过滤器 * @author java1234_小锋 * @site www.java1234.com * @company Java知识分享网 * @create 2021-04-08 16:25 */public class MyCustomerGatewayFilter implements GatewayFilter,Ordered {    @Override    public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {        System.out.println("自定义网关过滤器");        return chain.filter(exchange);    }    @Override    public int getOrder() {        return -1;    }}

    5.7.2 自定义GlobalFilter

    实现GlobalFilter ,Ordered 接口,加上@Component注解即可;

    package com.java1234.filter;import org.springframework.cloud.gateway.filter.GatewayFilterChain;import org.springframework.cloud.gateway.filter.GlobalFilter;import org.springframework.core.Ordered;import org.springframework.stereotype.Component;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;/** * @author java1234_小锋 * @site www.java1234.com * @company Java知识分享网 * @create 2021-04-09 12:29 */@Componentpublic class MyCustomerGlobalFilter implements GlobalFilter ,Ordered {    @Override    public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {        System.out.println("自定义全局过滤器");        return chain.filter(exchange);    }    @Override    public int getOrder() {        return -2;    }}

上一篇:Overcooked下载攻略,一键获取,轻松玩转游戏指南!

下一篇:公司服务器地址怎么进入?