网关(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/ #重定向到百度

访问效果

相关推荐
读书笔记控13 小时前
计算机相关术语科普之什么叫网关(Gateway)
gateway
Lill_bin1 天前
面试题--SpringBoot
spring boot·后端·spring·spring cloud·zookeeper·gateway·mybatis
怪兽也会哭哭2 天前
微服务应用与开发知识点练习【Gateway,OpenFeign,Dubbo,RocketMQ和RabbitMQ,JPA,Redis,Mycat】
微服务·gateway·rabbitmq·dubbo·rocketmq
怪兽也会哭哭2 天前
Spring Cloud Alibaba之网关组件Gateway
spring boot·gateway·学习笔记
池塘边的菜园子3 天前
学习gateway网关路由时遇到的问题
网络·学习·gateway
池塘边的菜园子3 天前
微服务-网关Gateway
linux·微服务·gateway
曾燕辉4 天前
Sentinel实现区分来源
java·sentinel·springcloud
曾燕辉4 天前
Sentinel如何使用BlockExceptionHandler实现限流/降级错误页面显示
java·sentinel·springcloud
一个搬砖的农民工4 天前
微服务——服务治理
java·微服务·nacos·springcloud·服务治理
请叫我鹏鹏君4 天前
微服务 | Springboot整合GateWay+Nacos实现动态路由
spring boot·微服务·gateway