Spring Cloud Gateway 内置过滤器 filter
https://docs.spring.io/spring-cloud-gateway/docs/2.2.6.RELEASE/reference/html/#gatewayfilter-factories
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; } }
- 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; } }
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
- RedirectTo=302, https://www.baidu.com/ #重定向到百度
- SetStatus=404
分类:
SpringCloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异