阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

路由(Route)

405次阅读
没有评论

共计 7240 个字符,预计需要花费 19 分钟才能阅读完成。

1、认识路由的谓词接口和谓词工厂

在 Java 8 中引入了一个函数接口 Predicate,它接收一个输入参数并返回一个布尔值结果。

Spring Cloud Gateway 通过 Predicate 接口组合简单条件来判断当前路由是否满足给定条件。该接口包含许多默认方法,用于将 Predicate 组合成复杂逻辑,以校验请求参数、判断数据是否改变、是否需要更新等。

使用路由的流程:

1、加载路由中的 Predicate

2、用 Predicate 判断路由是否可用

2、认识配置路由规则的方式

2.1、通过 Java API 的方式

直接通过 RouteLocatorBuilder 接口来构建路由规则。

@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder){return builder.routes().route("hello", r->r.path("/hello/**") .uri("http://localhost:500010")) .build();}

2.2、通过配置文件的方式

可以通过 application.properties 或 application.yml 来配置路由规则。

1、application.yml

server: port: 50010 spring: cloud: gateway: routes: - id: hello uri: http://localhost:50010 predicates: - Path=/hello/**

2、application.properties

#id: 自定义路由 ID
spring.cloud.gateway.routes[0].id=hello
#uri: 目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50010
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello/**

3、配置文件方式构建路由

3.1、新建工程,添加依赖

<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>

3.2、在配置文件中配置路由

spring.application.name=gateway-java-api
server.port=50010
#id: 自定义路由 ID
spring.cloud.gateway.routes[0].id=hello
#uri: 目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50009
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello

说明:

此处配置了一个路由 ID 为 ”hello” 的路由规则,其作用是:当请求访问 http://localhost:50010/hello 时,将请求转发到 http://localhost:50009/hello

3.3、启动 ” 服务提供者 ” 和网关工程

访问 http://localhost:50010/hello

路由(Route)

4、通过 Java API 方式配置路由

4.1、修改 ” 服务提供者 ” 的 HelloController

@RestController public class HelloController {@Value("${provider.name}") private String name; @GetMapping("/hello") public String hello(){return name; } @GetMapping("/hello2") public String hello2(){return name; } }

4.2、修改网关工程的 application.properties

spring.application.name=gateway-java-api
server.port=50010
#id: 自定义路由 ID
#spring.cloud.gateway.routes[0].id=hello
#uri: 目标服务地址
#spring.cloud.gateway.routes[0].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
#spring.cloud.gateway.routes[0].predicates[0]=Path=/hello

4.3、添加 Java API 方式配置

@SpringBootApplication public class GatewayJavaApiApplication {public static void main(String[] args) {SpringApplication.run(GatewayJavaApiApplication.class, args); } @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder){// 配置路由 id 为 hell02,转发到 http://localhost:50021/hello return builder.routes().route("hello2", r->r.path("/hello") .uri("http://localhost:50021")) .build();} }

4.4、启动 ” 服务提供者 ” 和网关工程

访问 http://localhost:50010/hello2

路由(Route)

5、Spring Cloud Gateway 的 11 种路由规则

Spring Cloud Gateway 通过 RoutePredicateFactory 创建 Predicate。Spring Cloud Gateway 预置了很多 RoutePredicateFactory,进行简单的配置即可得到想要的路由规则(Predicate)。这些路由规则会根据 HTTP 请求的不同属性来进行匹配。多个路由规则可以通过逻辑进行组合。

Spring Cloud Gateway 预置的规则如下:

谓词工厂 类型 说明
AfterRoutePredicateFactory datetime 请求时间满足在配置时间之后
BeforeRoutePredicateFactory datetime 请求时间满足在配置时间之前
BetweenRoutePredicateFactory datetime 请求时间满足在配置时间之间
CookieRoutePredicateFactory Cookie 请求指定 Cookie 正则匹配指定值
HeaderRoutePredicateFactory Header 请求指定 Headers 正则匹配指定值
CloudFoundryRouteServiceRoutePredicateFactory Header 请求指定 Headers 是否包含指定名称
MethodRoutePredicateFactory Method 请求 Method 匹配配置的 Methods
PathRoutePredicateFactory Path 请求路径正则匹配指定值
QueryRoutePredicateFacotry Queryparam 请求查询参数正则匹配指定值
请求 Method 匹配配置的 Methods Remoteaddr 请求远程地址匹配配置指定值
HostRoutePredicateFactory Host 请求 Host 匹配指定值

5.1、Path 路由谓词工厂

#id: 自定义路由 ID
spring.cloud.gateway.routes[0].id=hello
#uri: 目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello

5.2、After 路由谓词工厂

#id: 自定义路由 ID
spring.cloud.gateway.routes[2].id=after_route
#uri: 目标服务地址
spring.cloud.gateway.routes[2].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[2].predicates[0]=After= 时间

5.3、Before 路由谓词工厂

#id: 自定义路由 ID
spring.cloud.gateway.routes[1].id=before_route
#uri: 目标服务地址
spring.cloud.gateway.routes[1].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[1].predicates[0]=Before= 时间

5.4、Between 路由谓词工厂

#id: 自定义路由 ID
spring.cloud.gateway.routes[3].id=between_route
#uri: 目标服务地址
spring.cloud.gateway.routes[3].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[3].predicates[0]=Between= 时间, 时间

5.5、Cookie 路由谓词工厂

Cookie 路由谓词工厂采用两个参数:Cookie 名称和正则表达式。此谓词工厂匹配具有给定名称的 Cookie,值与正则表达式匹配。

#id: 自定义路由 ID
spring.cloud.gateway.routes[4].id=cookie_route
#uri: 目标服务地址
spring.cloud.gateway.routes[4].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[4].predicates[0]=Cookie=name,long

5.6、Header 路由谓词工厂

Header 路由谓词工厂采用两个参数:Header 名称和正则表达式。此谓词工厂用正则表达式匹配 Header。

#id: 自定义路由 ID
spring.cloud.gateway.routes[5].id=header_route
#uri: 目标服务地址
spring.cloud.gateway.routes[5].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[5].predicates[0]=Header=X-Request-Id,\d+

5.7、Host 路由谓词工厂

#id: 自定义路由 ID
spring.cloud.gateway.routes[6].id=host_route
#uri: 目标服务地址
spring.cloud.gateway.routes[6].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[6].predicates[0]=Host=**.tyschool.com

5.8、Method 路由谓词工厂

#id: 自定义路由 ID
spring.cloud.gateway.routes[7].id=method_route
#uri: 目标服务地址
spring.cloud.gateway.routes[7].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[7].predicates[0]=Method=GET

5.9、Query 路由谓词工厂

Query 路由谓词工厂包含 1 个必需的参数和 1 个可选的正则表达式。

#id: 自定义路由 ID
spring.cloud.gateway.routes[8].id=query_route
#uri: 目标服务地址
spring.cloud.gateway.routes[8].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[8].predicates[0]=Query=name
#?name=x&id=1
#Query=name,lon.
#其中. 代表一个字符
#?name=log

5.10、RemoteAddr 路由谓词工厂

RemoteAddr 路由谓词工厂采用 CIDR 符号 (IPv4 划 IPv6) 字符串列表(最小值为 1),例如:192.168.0.1/16

#id: 自定义路由 ID
spring.cloud.gateway.routes[9].id=ip_route
#uri: 目标服务地址
spring.cloud.gateway.routes[9].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[9].predicates[0]=RemoteAddr=192.168.1.1/255

5.11、Weight 路由谓词工厂

Weight 路由谓词工厂是路由权重的,在配置时需要指定分组和权重。

#id: 自定义路由 ID
spring.cloud.gateway.routes[10].id=weight_route1
#uri: 目标服务地址
spring.cloud.gateway.routes[10].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[10].predicates[0]=Path=/hello
spring.cloud.gateway.routes[10].predicates[1]=Weight=Weight,4
#id: 自定义路由 ID
spring.cloud.gateway.routes[11].id=weight_route2
#uri: 目标服务地址
spring.cloud.gateway.routes[11].uri=http://localhost:50021
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[11].predicates[0]=Path=/hello
spring.cloud.gateway.routes[11].predicates[1]=Weight=Weight,6

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-06-06发表,共计7240字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19348
评论数
4
阅读量
7818570
文章搜索
热门文章
开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南 大家好,我是星哥。之前介绍了腾讯云的 Code...
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板 1Panel:小白轻松构建 Web 服务与面板加固指南 哈喽,我是星哥,经常有人问我不...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换,告别多工具切换

12.2K Star 爆火!开源免费的 FileConverter:右键一键搞定音视频 / 图片 / 文档转换...
开发者福利:免费 .frii.site 子域名,一分钟申请即用

开发者福利:免费 .frii.site 子域名,一分钟申请即用

  开发者福利:免费 .frii.site 子域名,一分钟申请即用 前言 在学习 Web 开发、部署...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手! 前言 对于个人开发者、建站新手或是想搭建测试站点的从业者...
支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare 也瘫了连监控都挂,根因藏在哪? 最近两天的互联网堪称“故障...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
如何免费使用强大的Nano Banana Pro?附赠邪修的用法

如何免费使用强大的Nano Banana Pro?附赠邪修的用法

如何免费使用强大的 Nano Banana Pro?附赠邪修的用法 前言 大家好,我是星哥,今天来介绍谷歌的 ...
颠覆 AI 开发效率!开源工具一站式管控 30+大模型ApiKey,秘钥付费+负载均衡全搞定

颠覆 AI 开发效率!开源工具一站式管控 30+大模型ApiKey,秘钥付费+负载均衡全搞定

  颠覆 AI 开发效率!开源工具一站式管控 30+ 大模型 ApiKey,秘钥付费 + 负载均衡全...
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击 PHP-FPM(FastCGl Process M...
240 元左右!五盘位 NAS主机,7 代U硬解4K稳如狗,拓展性碾压同价位

240 元左右!五盘位 NAS主机,7 代U硬解4K稳如狗,拓展性碾压同价位

  240 元左右!五盘位 NAS 主机,7 代 U 硬解 4K 稳如狗,拓展性碾压同价位 在 NA...