Spring Cloud Gateway 内置过滤器 filter

https://docs.spring.io/spring-cloud-gateway/docs/2.2.6.RELEASE/reference/html/#gatewayfilter-factories

Spring Cloud Gateway 内置过滤器 filter
spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        filters:
        - AddRequestHeader=X-Request-red, blue

为原始请求添加名为 X-Request-red,值为 blue 的请求头。

- AddRequestHeader=X-Request-color, blue
server:
  port: 8060
spring:
  application:
    name: api-gateway
  cloud:
    # gateway的配置
    gateway:
      # 路由规则
      routes:
        - id: order_route # 路由的唯一标识, 路由到 order
          #          uri: http://localhost:8020 # 需要转发的地址
          uri: lb://order-nacos-service # 需要转发的地址  lb:使用nacos中的本地负载均衡策略
          # 断言规则 用于路由规则的匹配
          predicates:
            - Path=/order-serv/**
              # http://localhost:8060/order-serv/order/add 路由转到
            # http://localhost:8020/order-serv/order/add
           - After=2017-01-20T17:42:47.789-07:00[Asia/Shanghai]
           - Header=X-Request-Id,\d+
           - Method=GET
           - Query=name,zhangsan|lisi
           - CheckAuth=lisi
          filters:
            - StripPrefix=1  # 转发之前去掉第一层路径
              # http://localhost:8020/order-serv/order/add 过虑成
            # http://localhost:8020/order/add
            - AddRequestHeader=X-Request-color, blue
    # 配置 Nacos
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        #        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        namespace: public
package com.wsm.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add(){
        System.out.println("aaaaaaaaaaaaa");
//        String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
        String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
        return "hello world "+msg;
    }

    @RequestMapping("/header")
    public String header(@RequestHeader("X-Request-color") String color){
        return color;
    }
}

Spring Cloud Gateway 内置过滤器 filter
- AddRequestParameter=color, red
package com.wsm.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add(){
        System.out.println("aaaaaaaaaaaaa");
//        String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
        String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
        return "hello world "+msg;
    }

    @RequestMapping("/header")
    public String header(@RequestHeader("X-Request-color") String color){
        return color;
    }

    @RequestMapping("/param")
    public String param(@RequestParam("color") String color){
        return color;
    }
}

Spring Cloud Gateway 内置过滤器 filter

Spring Cloud Gateway 内置过滤器 filter
server:
  port: 8020
  #应用名称  (nacos 会将该名称当作服务名称)
  servlet:
    context-path: /mall-order
spring:
  application:
    name: order-nacos-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
     server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public
- PrefixPath=/mall-order    #添加前缀, 对应微服务需要配置context-path
server:
  port: 8060
spring:
  application:
    name: api-gateway
  cloud:
    # gateway的配置
    gateway:
      # 路由规则
      routes:
        - id: order_route # 路由的唯一标识, 路由到 order
          #          uri: http://localhost:8020 # 需要转发的地址
          uri: lb://order-nacos-service # 需要转发的地址  lb:使用nacos中的本地负载均衡策略
          # 断言规则 用于路由规则的匹配
          predicates:
            - Path=/order-serv/**
              # http://localhost:8060/order-serv/order/add 路由转到
            # http://localhost:8020/order-serv/order/add
           - After=2017-01-20T17:42:47.789-07:00[Asia/Shanghai]
           - Header=X-Request-Id,\d+
           - Method=GET
           - Query=name,zhangsan|lisi
           - CheckAuth=lisi
          filters:
            - StripPrefix=1  # 转发之前去掉第一层路径
              # http://localhost:8020/order-serv/order/add 过虑成
            # http://localhost:8020/order/add
           - AddRequestHeader=X-Request-color, blue
           - AddRequestParameter=color, red
            - PrefixPath=/mall-order    #添加前缀, 对应微服务需要配置context-path
    # 配置 Nacos
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        #        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        namespace: public

Spring Cloud Gateway 内置过滤器 filter

Spring Cloud Gateway 内置过滤器 filter

Spring Cloud Gateway 内置过滤器 filter
- RedirectTo=302, https://www.baidu.com/ #重定向到百度
- SetStatus=404

Spring Cloud Gateway 内置过滤器 filter

Spring Cloud Gateway 内置过滤器 filter

Original: https://www.cnblogs.com/mingforyou/p/15820419.html
Author: 残星
Title: Spring Cloud Gateway 内置过滤器 filter

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/540629/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 第二周总结-Spring学习

    java;gutter:true;</p> <h2>Spring_day01</h2> <p><strong>今日目标&…

    Java 2023年6月7日
    089
  • 一文了解Cookie

    Cookie 什么是 Cookie? 先要了解HTTP是 无状态的Web服务器,什么是无状态呢?一次对话完成后下一次对话完全不知道上一次对话发生了什么。如果在Web服务器中只是用来…

    Java 2023年6月15日
    074
  • 【转载】SpringCloud-Eurek 心跳阈值说明

    在使用eureka过程中,查看监控界面,出现: Renews threshold:server期望在每分钟中收到的心跳次数Renews (last min):上一分钟内收到的心跳次…

    Java 2023年5月29日
    069
  • el-form表单 input回车刷新页面问题

    1原因 是因为当form表单中只有一个input时,按下回车建会自动触发页面的提交功能, 所以会产生刷新页面的行为 解决方案: <el-form :model="q…

    Java 2023年6月5日
    087
  • springboot集成swagger,出现 No mapping for GET /swagger-ui.html的错误

    在尝试降低springboot版本和swagger版本都无法解决这个问题后 在SpringBoot项目引入Swagger2后,在浏览器地址里输入地址:http://ip:port/…

    Java 2023年5月30日
    061
  • java学习之爬虫

    对比与Python的爬虫机制和java的爬虫机制来详解一下java的爬虫,对于一般性的需求无论java还是python都可以胜任。如需要模拟登陆、对抗防采集选择python更方便些…

    Java 2023年6月13日
    0101
  • 华为暑期实习 通用软件开发 面经

    华为暑期实习 通用软件开发工程师 数据存储与机器视觉 面经 机试 7.6 第一题 字符串匹配 给五行英文句子,找出来其中的网址,网址以http或https开头,以com结尾,不要重…

    Java 2023年6月5日
    0109
  • keytool命令制作CA根证书,签发二级证书

    关于TLS的一些基本信息我这里就不多说了,网上一搜一大堆。这里主要说一下,在tls单向认证里,怎么用keytool命令去制作CA证书,签发二级证书。双向认证也就是照着反方向做一遍就…

    Java 2023年6月9日
    0108
  • springBean的生命周期

    posted @2022-03-30 15:49 不死码农 阅读(24 ) 评论() 编辑 Original: https://www.cnblogs.com/leeego-123…

    Java 2023年5月30日
    082
  • SpringMvc(一)-初识

    1、环境搭建 1.1 jar包 4.3.18.RELEASE org.springframework spring-web ${spring.version} org.spring…

    Java 2023年6月15日
    066
  • 【碎】bat 文件闪退

    解决办法:使用命令行窗口运行 bat 文件 posted @2022-09-25 15:41 HypoPine 阅读(43 ) 评论() 编辑 Original: https://…

    Java 2023年6月15日
    073
  • 123取反操作为什么是-124 取反操作

    首先请记住一点,在计算机中所有的二进制都是以补码的形式存储的,所以你最后取反之后只是这个数的补码,你还需要转换成源码,才是我们最终的十进制数字 下面是计算过程: 正数取反(123,…

    Java 2023年6月6日
    0114
  • Buuctf-Web-[ACTF2020 新生赛]Include

    前言 刷题网址:https://buuoj.cn/challenges#[ACTF2020 新生赛]Include 首先打开网页,提示 tips点击他,根据题目提示很难不想到是文件…

    Java 2023年6月13日
    0111
  • 7.Hystrix缓存特性及请求特性

    Hystrix请求缓存 Hystrix支持将请求结果进行本地缓存 通过实现getCacheKey方法来判断是否取出缓存 请求缓存要求请求必须在同一个上下文 可以用过RequestC…

    Java 2023年6月8日
    070
  • 我在Google AppEngine上部署了一个Java应用(OpenID测试)

    我在Google AppEngine上部署了一个Java应用(OpenID测试)http://cogito-study.appspot.com Google Apps不支持线程,所…

    Java 2023年5月29日
    093
  • 123456789 给一个字符串随机添加 “-”,“+” 和为100

    面试题 123456789 给一个字符串随机添加 “-“,”+”生成一个公式计算和为100; import java.util.Ra…

    Java 2023年6月7日
    070
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球