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
Original: https://www.cnblogs.com/mingforyou/p/15820419.html
Author: 残星
Title: Spring Cloud Gateway 内置过滤器 filter
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/540629/
转载文章受原作者版权保护。转载请注明原作者出处!