网关(Gateway)- 内置过滤器工厂

官方文档:Spring Cloud Gateway

内置过滤器工厂

AddRequestHeaderGatewayFilterFactory

为请求添加Header Header的名称及值

配置说明

java 复制代码
server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8847
        username: nacos
        password: nacos
    gateway:
      routes:
        - id: order_route # 路由唯一标识
          #uri: http://localhost:8020 # 需要转发的地址
          uri: lb://order-service # 需要转发的地址
          # 断言规则  用于路由规则的匹配
          predicates:
#            - Path=/order-serv/**
#            - After=2024-01-01T23:00:11.518+08:00[Asia/Shanghai]
#            - Before=2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
            - Between=2024-01-01T23:00:11.518+08:00[Asia/Shanghai],2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
#            - Header=X-Request-Id,\d+
#            - Host=127.0.0.1
#            - Query=name,lq
#            - Method=GET,POST
            # http://localhost:8088/order-serv/order/add  => http://localhost:8020/order-serv/order/add
            - Demo=YES
          #配置过滤器工厂
          filters:
            - StripPrefix=1 # 转发去掉第一层路径
            - AddRequestHeader=X-Request-name,tom #添加请求头
            # http://localhost:8020/order-serv/order/add => http://localhost:8020/order/add

代码编写

java 复制代码
package com.learning.springcloud.gateway.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/gateway")
public class GatewayController {


  @GetMapping("/test")
  public String testGateway(HttpServletRequest request) throws Exception { 
    return "success => gateWay-test 获取请求头X-Request-name:" + request.getHeader("X-Request-name");
  }

  @GetMapping("/test2")
  public String testGateway(@RequestHeader("X-Request-name") String name) throws Exception {
    return "success => gateWay-test2 获取请求头X-Request-name:" + name;
  }

}

访问效果

AddRequestParameterGatewayFilterFactory

为请求添加参数的名称及值

配置说明

java 复制代码
 filters:
    - StripPrefix=1 # 转发去掉第一层路径
    - AddRequestHeader=X-Request-name,tom #添加请求头
    - AddRequestParameter=color, blue # 添加请求参数

代码编写

java 复制代码
    @GetMapping("/test3")
    public String testGateway3(@RequestParam("color") String color) throws Exception {
        log.info("gateWay-get 获取请求参数color:" + color);
        return "success => gateWay-get 获取请求参数color:" + color;
    }

访问效果

PrefixPathGatewayFilterFactory

增加项目路径的过滤器

配置说明

java 复制代码
# order-service 服务配置
server:
  port: 8020
  servlet:
    context-path: /demo

# 网关配置
  filters:
    - StripPrefix=1 # 转发去掉第一层路径
    - AddRequestHeader=X-Request-name,tom #添加请求头
    - AddRequestParameter=color, blue # 添加请求参数
    - PrefixPath=/demo

访问效果

  • 不携带 配置的项目路径 demo 是无法访问的
  • 携带配置的项目路径demo是可以访问的
  • 通过网关访问不携带项目路径demo 可以正常访问

RedirectToGatewayFilterFactory

重定向过滤器,两个参数:HTTP状态码 和 重定向的目标地址

配置说明

java 复制代码
      #配置过滤器工厂
      filters:
        - StripPrefix=1 # 转发去掉第一层路径
        - AddRequestHeader=X-Request-name,tom #添加请求头
        - AddRequestParameter=color, blue # 添加请求参数
        - PrefixPath=/demo
        - RedirectTo=302, https://www.baidu.com/ #重定向到百度

访问效果

相关推荐
yangmf20401 天前
如何使用 INFINI Gateway 增量迁移 ES 数据
大数据·数据库·elasticsearch·搜索引擎·gateway
非凡的世界2 天前
ThinkPHP6 集成TCP长连接 GatewayWorker
网络·网络协议·tcp/ip·gateway·thinkphp·worker·workman
戮戮2 天前
一次深入排查:Spring Cloud Gateway TCP 连接复用导致 K8s 负载均衡失效
tcp/ip·spring cloud·kubernetes·gateway·负载均衡·netty
洛克大航海4 天前
7-SpringCloud-服务网关 Gateway-高级特性 Route
java·spring cloud·gateway·route
阿Y加油吧6 天前
微服务——day02
springcloud
AWS官方合作商7 天前
在AWS S3上动态自定义图片尺寸:Lambda + API Gateway无服务器解决方案
serverless·gateway·aws
青鱼入云8 天前
对比nginx、kong、apisix、zuul、gateway网关
nginx·gateway·kong
1.01^100010 天前
[3-03-01].第07节:搭建服务 - 服务重构cloud-consumer-ocommon
springcloud
唐僧洗头爱飘柔952711 天前
【SpringCloud(6)】Gateway路由网关;zuul路由;gateway实现原理和架构概念;gateway工作流程;静态转发配置
spring·spring cloud·架构·gateway·请求转发·服务降级·服务雪崩
xrkhy11 天前
微服务之Gateway网关(1)
微服务·架构·gateway