Kong 网关 | Route

一、简介

路由用来匹配客户端向上游服务器请求的规则,也就是客户端调用的 API,每个路由(Route)和一个服务(Service) 相关联,一个服务可有有多个路由,我们可以对每一条路由进行细粒度的配置,可以使用正则表达式进行通用的配置。

二、重要属性

创建一个路由需要配置的属性,其中路径 paths 为必须设置,其余为可选。

Attributes Description
name 路由名称
protocols 可以请求该路由的协议,默认为 [“http”, “https”]
methods 方法,如 GET,POST,DELETE,PATCH,PUT
hosts 匹配此路由的域名列表,可设置多个值
paths 匹配此路由的路径,也就是 API,可以设置多个值
regex_priority 路由请求的优先级,数字越大的在数字越小的之前匹配,默认为0
strip_path 当通过一条路径进行请求时,从上游中去掉匹配的前缀,默认为 true,如果上游的路径和配置的请求路径或者前缀一样,配置路由时需要配置为 false。

三、操作示例

1、创建路由
POST /services/{service name or id}/routes
curl -i -X POST \
  --url http://localhost:8001/services/myservice/routes \
  --data 'hosts[]=gateway.com' \
  --data 'name=getBlackRoles' \
  --data 'strip_path=false' \
  --data 'paths[]=/blackRoles'\
  --data 'methods[]=GET&methods[]=POST' \
2、查询路由

查询所有路由

GET /routes
curl -i -X GET \
  --url http://localhost:8001/routes \

查询某个服务的路由

GET /services/{service name or id}/routes
curl -i -X GET \
  --url http://localhost:8001/services/myservice/routes \
3、搜索路由

根据路由名称或 id 搜索

GET /routes/{route name or id}
curl -i -X GET \
  --url http://localhost:8001/routes/getBlackRoles \

搜索特定服务的路由

GET /services/{service name or id}/routes/{route name or id}
curl -i -X GET \
  --url http://localhost:8001/services/myservice/routes/getBlackRoles \
4、更新路由

根据路由 ID 或名称更新路由

PATCH /routes/{route name or id}
curl -i -X PATCH \
  --url http://localhost:8001/routes/getBlackRoles \
  --data 'hosts[]=gateway.com' \
  --data 'name=getBlackRole' \
  --data 'strip_path=false' \
  --data 'paths[]=/blackRoles'\
  --data 'methods[]=GET&methods[]=POST' \

更新指定服务的路由

PATCH /services/{service name or id}/routes/{route name or id}
curl -i -X PATCH \
  --url http://localhost:8001/services/myservice/routes/getBlackRole \
  --data 'hosts[]=gateway.com' \
  --data 'name=getBlackRoles' \
  --data 'strip_path=false' \
  --data 'paths[]=/blackRoles'\
  --data 'methods[]=GET&methods[]=POST' \
5、更新或创建路由

根据路由名称或 ID 更新

PUT /routes/{route name or id}
curl -i -X PUT \
  --url http://localhost:8001/routes/getBlackRoles \
  --data 'hosts[]=gateway.com' \
  --data 'name=getBlackRoles' \
  --data 'strip_path=false' \
  --data 'paths[]=/blackRoles'\
  --data 'methods[]=GET&methods[]=POST' \

更新指定服务的路由

PUT /services/{service name or id}/routes/{route name or id}
curl -i -X PUT \
  --url http://localhost:8001/services/myservice/routes/getBlackRoles \
  --data 'hosts[]=gateway.com' \
  --data 'name=getBlackRoles' \
  --data 'strip_path=false' \
  --data 'paths[]=/blackRoles'\
  --data 'methods[]=GET&methods[]=POST' \
6、删除路由

根据路由ID或名称删除路由

DELETE /routes/{route name or id}
curl -i -X DELETE \
  --url http://localhost:8001/routes/getBlackRoles \

删除指定服务的路由

DELETE /services/{service name or id}/routes/{route name or id}
curl -i -X DELETE \
  --url http://localhost:8001/services/myservice/routes/getBlackRoles \

相关资料

Kong官方文档(路由) :https://docs.konghq.com/1.4.x/admin-api/#route-object

ABOUT

推荐阅读
史上最全,最完美的 JAVA 技术体系思维导图总结,没有之一!
全站导航 | 我为什么要写这些博客?

我的 Github:Github
CSDN: CSDN
个人网站: sirius blog
E-mail: 1136513099qq.com

发布了81 篇原创文章 · 获赞 373 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/Sirius_hly/article/details/104032853
今日推荐