Spring Cloud Alibaba学习 4- Spring Cloud Gateway入门使用
中文文档Spring Cloud Gateway 中文文档
一. 基本使用
1. Predicate配置
1.1 配置参数介绍
直接通过官方给的application.yml介绍,这里就不介绍简写方式了,直接介绍完整方式
spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:- name: Cookieargs:name: mycookievalue: xxxregexp: mycookievalue
-
routes:
可以配置多个需要管理的的路由规则 route
-
route:
配置了route就必须配置predicate!!
详细写法:
- id: 路由的编号uri: 需要转发的目的URIpredicates: 断言,只有完全匹配断言规则的才可以对请求进行处理,同一个断言下的配置的路由规则是与的关系- name: 路由规则名args:name: 参数的名称value: 参数的值
简单写法:
- id: 路由的编号uri: 需要转发的目的URIpredicates: #断言- Cookie=chocolate, ch.p #路由规则名=参数名, 参数值
- 路由规则名
下面是官方给出的一些例子
name | 说明 | 举例 | 例子解释 |
---|---|---|---|
After | 在这个时间后的请求才转发(时间类型得是ZonedDateTime) | - After=2017-01-20T17:42:47.789-07:00[America/Denver] | 北美山区时间(丹佛)2017年1月20日17:42之后发出的任何请求相匹配。 |
Before | 在这个时间前的请求才转发(时间类型得是ZonedDateTime) | - Before=2017-01-20T17:42:47.789-07:00[America/Denver] | 北美山区时间2017年1月20日17:42(丹佛)之前发出的任何请求相匹配 |
Between | 在这两个时间之间的请求(时间类型得是ZonedDateTime) | - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver] | 匹配2017年1月20日山区时间(丹佛)17:42之后和2017年1月21日山区时间(丹佛)17:42之前的任何请求 |
Cookie | 匹配具有给定名称且其值符合正则表达式的cookie | - Cookie=chocolate, ch.p | 匹配有一个名为 chocolate 的cookie,其值符合 ch.p 正则表达式的请求 |
Header | 匹配具有给定名称且其值符合正则表达式的请求头 | - Header=X-Request-Id, \d+ | X-Request-Id 的header,其值与 \d+ 正则表达式相匹配 |
Host | 匹配请求的Host | - Host=**.somehost.org,**.anotherhost.org | 请求的 Host 值为 www.somehost.org 或 beta.somehost.org 或 www.anotherhost.org 则匹配 |
Method | 匹配请求的方式 | - Method=GET,POST | 请求方式是 GET 或 POST 则匹配 |
Path | 匹配对应值的路径(支持通配符**和占位符{segment}) | - Path=/red/{segment},/blue/{segment} | 如果请求路径是 /red/1 或 /red/1/ 或 /red/blue 或 /blue/green ,则该路由匹配 |
Query | 匹配具有给定名称且其值符合正则表达式的方法参数 | - Query=green | 请求中包含一个 red 的查询参数,其值与 gree. 表达式相匹配,那么路由就会匹配 |
RemoteAddr | 匹配远程地址(IPv4或IPv6) | - RemoteAddr=192.168.1.1/24 | 如果请求的远程地址 192.168.1.10 ,则该路由匹配 |
Weight | |||
XForwarded Remote Addr |
1.2 使用
1.2.1 引入依赖
注意要和 Spring Boot 版本匹配,否则启动会报错
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><version>4.1.2</version>
</dependency>
1.2.2 配置路由规则
以下的规则代表,如果有请求发送到路由所在的服务上,如果是GET请求,那么就会转发到 http://localhost:8081
application.yml
spring:application:name: gatewaycloud:gateway:routes:- id: checkParamuri: http://localhost:8081predicates:- name: Methodargs:name: pathvalue: GET
1.2.3 测试
运行一个在8081端口上的服务 orderService,orderController如下:
@RestController
@RequestMapping("/order")
@Slf4j
@RefreshScope
public class OrderController {@GetMapping("/testGetGateWay")public String testGetGateWay() {return "你好";}@PostMapping("/testPostGateWay")public String testPostGateWay() {return "你好";}
}
运行的服务如下:
发送GET请求,能成功转发到orderservice上
GET http://localhost:8080/order/testGetGateWay
发送POST请求,转发失败,因为我们只允许GET请求转发,显示404
2. Filter配置
参数很多,见官方文档,用法和上面Predicate基本相同,这里不做过多介绍
下面这个例子表示,如果请求带有param参数,则转发请求并带上X-Request-Token: 123
作为请求头
spring:cloud:gateway:routes:- id: after_routeuri: http://localhost:8081predicates:- Query=paramfilters:- AddRequestHeader=X-Request-Token, 123
OrderController的方法如下:
@GetMapping("/testGetGateWay")
public String testGetGateWay(HttpServletRequest req) {Enumeration<String> headers = req.getHeaderNames();while (headers.hasMoreElements()) {String name = headers.nextElement();String value = req.getHeader(name);log.info(name + ": " + value);}return "你好";
}
GET http://localhost:8080/order/testGetGateWay?param=1
结果:
二. 进阶使用
1. 全局CORS配置
解决跨域问题
写一个html文件给发送上述请求http://localhost:8080/order/testGetGateWay?param=1
test.html
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head><title>测试网页</title>
</head>
<body><button style="height: 50px; width: 50px;">导出</button>
</body><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>axios.get('http://localhost:8080/order/testGetGateWay?param=1').then(x => {console.log(x)}).catch(x => {console.log(x);})
</script>
</html>
报错:
配置127.0.0.1:8083允许跨域
application.yml
spring:application:name: gatewaycloud:gateway:globalcors:cors-configurations:'[/**]': #任意请求allowedOrigins: "http://127.0.0.1:8083" #允许http://127.0.0.1:8083跨域allowedMethods: #请求的方式- "*" #允许所有请求方式,记住是要双引号而不是单引号
结果: