## 4\. services *地址*:/services/{id} *说明*:Service 是某类 API 的抽象(也可以理解为一组 Route 的抽象)。它通常与上游服务抽象是一一对应的,Route 与 Service 之间,通常是 N:1 的关系。 ### 4.1 请求方法 | 序号 | NAME | 请求URI | 请求body | 说明 | | --- | --- | --- | --- | --- | | 1 | POST | /services | {...} | 新增services | | 2 | POST | /certificates/{certificate name or id}/services | {...} | 基于certificates新增services | | 3 | GET | /services | | 获取services列表 | | 4 | GET | /certificates/{certificate name or id}/services | | 获取services列表 | | 5 | GET | /services/{name or id} | | 根据name或ID获取service信息 | | 6 | GET | /routes/{route name or id}/service | | 根据路由name或id,获取servcie信息 | | 7 | GET | /plugins/{plugin id}/service | | 根据插件id,获取service信息 | | 8 | PATCH | /services/{name or id} | {...} | 更新service信息 | | 9 | PATCH | /routes/{route name or id}/service | {...} | 更新service信息 | | 10 | PATCH | /plugins/{plugin id}/service | {...} | 更新servie信息 | | 11 | PUT | /services/{name or id} | {...} | 新建/更新service信息 | | 12 | PUT | /routes/{route name or id}/service | {...} | 新建/更新service | | 13 | PUT | /plugins/{plugin id}/service | {...} | 新建/更新service | | 14 | DELETE | /services/{name or id} | 服务name或ID | 删除service | | 15 | DELETE | /routes/{route name or id}/service | 路由名称或ID | 删除service | ### 4.2 body 请求参数 | 序号 | 参数名 | 可选 | 默认值 | 说明 | 示例 | | --- | --- | --- | --- | --- | --- | | 1 | name | 可选 | | 标识服务名称 | examples.v1.service | | 2 | retries | 可选 | 5 | 重试次数 | 5 | | 3 | protocol | 必选 | http | 请求协议,http,https | http | | 4 | host | 必选 | | 上游服务upstream,或者host | examples.v1.upstream 或192.168.66.1 | | 5 | port | 必选 | 80 | 上游服务端口号 | 默认值为80 | | 6 | path | 可选 | | | | | 7 | connect\_timeout | 可选 | 60000 | 连接超时,默认为60s | 60000 | | 8 | write\_timeout | 可选 | 60000 | 向上游服务器传输请求的两个连续写入操作之间的超时时长 | 60000 | | 9 | read\_timeout | 可选 | 60000 | 向上游服务器传输请求的两个连续读取操作之间的超时(毫秒 | 60000 | | 10 | tags | 可选 | | 标签,用于分组或过滤 | | | 11 | client\_certificate | 可选 | | 参考:client\_certificate,1.3.x+版本 | | | 12 | url | 可选 | | 用于同时设置protocol,host,port 和path 的速记属性 | [http://localhost:8090/example](http://localhost:8090/example) | ### 4.3 数据样例 body 请求参数 ``` { "id": "9748f662-7711-4a90-8186-dc02f10eb0f5", "created_at": 1422386534, "updated_at": 1422386534, "name": "my-service", "retries": 5, "protocol": "http", "host": "example.com", "port": 80, "path": "/some_api", "connect_timeout": 60000, "write_timeout": 60000, "read_timeout": 60000, "tags": ["user-level", "low-priority"], "client_certificate": {"id":"4e3ad2e4-0bc4-4638-8e34-c84a417ba39b"} } ``` 服务列表 ``` { "data": [{ "id": "a5fb8d9b-a99d-40e9-9d35-72d42a62d83a", "created_at": 1422386534, "updated_at": 1422386534, "name": "my-service", "retries": 5, "protocol": "http", "host": "example.com", "port": 80, "path": "/some_api", "connect_timeout": 60000, "write_timeout": 60000, "read_timeout": 60000, "tags": ["user-level", "low-priority"], "client_certificate": {"id":"51e77dc2-8f3e-4afa-9d0e-0e3bbbcfd515"} }, { "id": "fc73f2af-890d-4f9b-8363-af8945001f7f", "created_at": 1422386534, "updated_at": 1422386534, "name": "my-service", "retries": 5, "protocol": "http", "host": "example.com", "port": 80, "path": "/another_api", "connect_timeout": 60000, "write_timeout": 60000, "read_timeout": 60000, "tags": ["admin", "high-priority", "critical"], "client_certificate": {"id":"4506673d-c825-444c-a25b-602e3c2ec16e"} }], "next": "http://localhost:8001/services?offset=6378122c-a0a1-438d-a5c6-efabae9fb969" } ```