## 5\. Routes *地址*:/routes *说明*:Route 字面意思就是路由,通过定义一些规则来匹配客户端的请求,然后根据匹配结果加载并执行相应的 插件,并把请求转发给到指定 Upstream。 ### 5.1 请求方法 | 序号 | NAME | 请求URI | 请求body | 说明 | | --- | --- | --- | --- | --- | | 1 | POST | /routes | {...} | 新增routes | | 2 | POST | /services/{service name or id}/routes | {...} | 在当前服务下创建路由 | | 3 | GET | /routes | | 获取路由列表 | | 4 | GET | /services/{service name or id}/routes | 服务名称或ID | 获取服务下的路由列表 | | 5 | GET | /routes/{name or id} | 路由名称或ID | 获取单 个路由信息 | | 6 | GET | /plugins/{plugin id}/route | 插件ID | 同上 | | 7 | PATCH | /routes/{name or id} | 路由名称或ID<br>{...} | 更新路由 | | 8 | PATCH | /plugins/{plugin id}/route | 插件ID<br>{...} | 同上 | | 9 | PUT | /routes/{name or id} | {...} | 新增/更新路由 | | 10 | PUT | /plugins/{plugin id}/route | {...} | 新增/更新路由 | | 11 | DELETE | /routes/{name or id} | 路由名称或ID | 删除路由 | ### 5.2 body 请求参数 | 序号 | 参数名 | 可选 | 默认值 | 说明 | 示例 | | --- | --- | --- | --- | --- | --- | | 1 | name | 可选 | | 标识路由名称 | examples.v1.route | | 2 | protocols | 必选 | `["http", "https"]` | 路由访问协议 | | | 3 | methods | 半选 | | methods、hosts、paths三者至少填写一个 | GET or POST | | 4 | hosts | 半选 | | 同上 | \["[example.com](http://example.com)","foo.test"\] | | 6 | paths | 半选 | | 同上 | \["/path1","/path2"\] | | 7 | https\_redirect\_status\_code | 可选 | 426 | 重定向的状态码 | 426 | | 8 | regex\_priority | | 0 | 正则匹配优先级 | | | 9 | strip\_path | | true | 是否过滤掉【截取】请求中的path | | | 10 | preserve\_host | | false | 是否保留请求的host到请求头中对上游服务进行请求,设置为false,则使用service中的host放在请求头中对请求上游执行服务请求 | | | 11 | snis | | | 当使用协议为tcp或tls时,snis、sources、destinations三者至少填写一项 | | | 12 | sources | | | 同上 | | | 13 | destinations | | | 同上 | | | 14 | tags | | | 标签 | | | 15 | service | | | 所属服务 | service.id=<[service.id](http://service.id)\>或"service":{"id":""} | | 16 | path\_handling | 可选 | v1 | 可选值:v0,v1<br>(注:v1.5.0版本+) | v1 | ### 5.3 数据样例 body 请求 ``` { "id": "51e77dc2-8f3e-4afa-9d0e-0e3bbbcfd515", "created_at": 1422386534, "updated_at": 1422386534, "name": "my-route", "protocols": ["http", "https"], "methods": ["GET", "POST"], "hosts": ["example.com", "foo.test"], "paths": ["/foo", "/bar"], "https_redirect_status_code": 426, "regex_priority": 0, "strip_path": true, "preserve_host": false, "tags": ["user-level", "low-priority"], "service": {"id":"fc73f2af-890d-4f9b-8363-af8945001f7f"} } ``` 路由列表 ``` { "data": [{ "id": "4506673d-c825-444c-a25b-602e3c2ec16e", "created_at": 1422386534, "updated_at": 1422386534, "name": "my-route", "protocols": ["http", "https"], "methods": ["GET", "POST"], "hosts": ["example.com", "foo.test"], "paths": ["/foo", "/bar"], "https_redirect_status_code": 426, "regex_priority": 0, "strip_path": true, "preserve_host": false, "tags": ["user-level", "low-priority"], "service": {"id":"d35165e2-d03e-461a-bdeb-dad0a112abfe"} }, { "id": "af8330d3-dbdc-48bd-b1be-55b98608834b", "created_at": 1422386534, "updated_at": 1422386534, "name": "my-route", "protocols": ["tcp", "tls"], "https_redirect_status_code": 426, "regex_priority": 0, "strip_path": true, "preserve_host": false, "snis": ["foo.test", "example.com"], "sources": [{"ip":"10.1.0.0/16", "port":1234}, {"ip":"10.2.2.2"}, {"port":9123}], "destinations": [{"ip":"10.1.0.0/16", "port":1234}, {"ip":"10.2.2.2"}, {"port":9123}], "tags": ["admin", "high-priority", "critical"], "service": {"id":"a9daa3ba-8186-4a0d-96e8-00d80ce7240b"} }], "next": "http://localhost:8001/routes?offset=6378122c-a0a1-438d-a5c6-efabae9fb969" } ```