Spring Cloud GateWay构建Route的应用

1.使用Java API方式构建路由
1)添加依赖配置

Spring Cloud GateWay构建Route的应用

2)配置路由规则

通过RouteLocatorBuilder接口来构建路由规则,此处使用的是指定的Path来构建路由
Spring Cloud GateWay构建Route的应用

3)验证路由

启动项目,浏览器访问http://localhost:8080/shenzhen/a,会进入https://www.jd.com/

Spring Cloud GateWay构建Route的应用

之后可以在项目控制台的debug info中看到匹配的路由是forward
Spring Cloud GateWay构建Route的应用

2.使用配置文件方式构建路由
1)添加依赖配置

Spring Cloud GateWay构建Route的应用

2)使用指定Path匹配指定路由

在application.properties中配置端口、route的id、uri、predicates

Spring Cloud GateWay构建Route的应用

启动项目后,浏览器访问http://localhost:8220/foundation,会进入https://www.apache.org/foundation/

Spring Cloud GateWay构建Route的应用

项目的debug info中显示匹配的路由id是Path_Route

Spring Cloud GateWay构建Route的应用

3)使用After路由规则匹配指定路由

在application.properties中配置端口、route的id、uri、predicates
Spring Cloud GateWay构建Route的应用

启动项目后,浏览器访问http://localhost:8220/index.html,会进入https://www.apache.org/index.html/

Spring Cloud GateWay构建Route的应用

项目的debug info中显示匹配的路由id是After_Route

Spring Cloud GateWay构建Route的应用

4)使用指定Cookie匹配指定路由

在application.properties中配置端口、route的id、uri、predicates

Spring Cloud GateWay构建Route的应用

使用curl访问 http://localhost:8220 会接收mysql服务器返回资源文件
Spring Cloud GateWay构建Route的应用

curl http://localhost:8220 --cookie “name=wxl”
Spring Cloud GateWay构建Route的应用

项目的debug info中显示匹配的路由id是Cookie_Route

Spring Cloud GateWay构建Route的应用

5)使用综合路由规则匹配指定路由

可以在配置文件中对11种路由规则综合配置
Spring Cloud GateWay构建Route的应用

启动项目后,浏览器访问http://localhost:8220/index.html,会进入https://www.oracle.com/index.html/
Spring Cloud GateWay构建Route的应用

项目的debug info中显示匹配的路由id是Weight_Route2

Spring Cloud GateWay构建Route的应用

* 某些路由匹配规则之间会存在相互冲突,请按实际效果配置使用顺序

相关文章: