欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > Spring Cloud Alibaba学习 4- Spring Cloud Gateway入门使用

Spring Cloud Alibaba学习 4- Spring Cloud Gateway入门使用

2025/3/16 20:29:25 来源:https://blog.csdn.net/weixin_55797790/article/details/146022922  浏览:    关键词:Spring Cloud Alibaba学习 4- Spring Cloud Gateway入门使用

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.orgbeta.somehost.orgwww.anotherhost.org则匹配
Method匹配请求的方式- Method=GET,POST请求方式是 GETPOST则匹配
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: #请求的方式- "*" #允许所有请求方式,记住是要双引号而不是单引号

结果:

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词