Spring Cloud GateWay构建Route的应用
1.使用Java API方式构建路由
1)添加依赖配置
2)配置路由规则
通过RouteLocatorBuilder接口来构建路由规则,此处使用的是指定的Path来构建路由
3)验证路由
启动项目,浏览器访问http://localhost:8080/shenzhen/a,会进入https://www.jd.com/
之后可以在项目控制台的debug info中看到匹配的路由是forward
2.使用配置文件方式构建路由
1)添加依赖配置
2)使用指定Path匹配指定路由
在application.properties中配置端口、route的id、uri、predicates
启动项目后,浏览器访问http://localhost:8220/foundation,会进入https://www.apache.org/foundation/
项目的debug info中显示匹配的路由id是Path_Route
3)使用After路由规则匹配指定路由
在application.properties中配置端口、route的id、uri、predicates
启动项目后,浏览器访问http://localhost:8220/index.html,会进入https://www.apache.org/index.html/
项目的debug info中显示匹配的路由id是After_Route
4)使用指定Cookie匹配指定路由
在application.properties中配置端口、route的id、uri、predicates
使用curl访问 http://localhost:8220 会接收mysql服务器返回资源文件
curl http://localhost:8220 --cookie “name=wxl”
项目的debug info中显示匹配的路由id是Cookie_Route
5)使用综合路由规则匹配指定路由
可以在配置文件中对11种路由规则综合配置
启动项目后,浏览器访问http://localhost:8220/index.html,会进入https://www.oracle.com/index.html/
项目的debug info中显示匹配的路由id是Weight_Route2