## 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"
}
```
- 1. 概述
- 2. 快速安裝
- 2.1 環境準備
- 2.2 開始安裝
- 2.3 啟動/關閉kongx
- 2.4 使用kongx
- 3. 使用指南
- 3.0 mockbin配置示例
- 3.0.1 不含upstream的配置
- 3.0.2 含upstream的配置
- 3.1 Gateway
- 3.1.1 Upstreams
- 3.1.1.1 新增/修改upstreams
- 3.1.1.2 管理targets
- 3.1.1.3 設置健康檢查
- 3.1.1.4 upstream視圖
- 3.1.2 Services
- 3.1.2.1 新建/修改service
- 3.1.2.2 添加服務路由
- 3.1.2.3 添加服務插件
- 3.1.2.4 同步services
- 3.1.2.5 services視圖
- 3.1.3 Routes
- 3.1.3.1 路由列表
- 3.1.3.2 修改路由
- 3.1.3.3 批量修改HOSTS
- 3.1.4 Plugins
- 3.1.4.1 新增插件
- 3.1.4.2 插件列表
- 3.1.5 Consumers
- 3.1.5.1 新建/修改consumers
- 3.1.6 Kong Shell
- 3.1.6.1 shell安裝
- 3.1.6.2 使用Shell
- 3.2 系統管理
- 3.2.1 用戶管理
- 3.2.2 角色管理
- 3.2.3 用戶組管理
- 3.2.4 菜單管理
- 3.3 參數管理
- 3.3.1 環境管理
- 3.3.2 系統參數
- 3.3.3 如何增加多個環境?
- 3.4 日志管理
- 3.4.1 操作日志
- 3.4.2 同步日志
- 3.5 工具箱
- 3.5.1 Kong Shell
- 3.5.2 切換工作臺
- 3.6 網關流水線
- 3.6.1 Pipeline
- 4. 最佳實踐
- 4.1 灰度插件canary使用
- 4.2 kong與consul集成
- 4.2.1 使用kong提供dns服務
- 4.2.2 使用dnsmasq提供dns服務
- 4.2.3 使用consul自主發現服務
- 4.3 kong健康檢查
- 4.3.1 簡介
- 4.3.2 健康檢查(主動檢查)
- 4.3.3 斷路器(被動檢查)
- 4.3.4 總結
- 4.4 認證插件之key-auth
- 4.5 認證插件之basic-auth
- 4.6 認證插件之oauth2-auth
- 4.7 認證插件之jwt
- 4.8 kong自定義access_log格式
- 4.8.1 前言
- 4.8.2 配置文本格式
- 4.8.3 配置JSON格式
- 4.9 kong的訪問監控
- 4.9.1 解決方案
- 4.9.2 方案實施
- 4.9.3 接入grafana報表
- 5. 常見問題
- 5.1 默認賬號及密碼
- 5.2 新增用戶默認密碼為123456
- 5.3 如何設置超級管理員
- 5.4 密碼忘記了咋辦
- 6. Kong
- 6.1 Kong簡介
- 6.2 kong安裝指南
- 6.2.1 kong安裝-RPM
- 6.2.2 kong安裝-源碼
- 6.2.3 kong基于yum源安裝
- 6.3 Admin API
- 6.3.1 API支持兩種內容類型
- 6.3.2 聲明式配置(Declarative Configuration)
- 6.3.3 獲取實體schema
- 6.3.4 services
- 6.3.5 Routes
- 6.3.6 Consumers
- 6.3.7 Plugins
- 6.3.8 Certficate
- 6.3.9 CA certficate(1.3.x+以上版本)
- 6.3.10 SNI
- 6.3.11 Upstreams
- 6.3.12 Targets
- 6.4 Kong使用
- 6.4.1 DB-LESS模式
- 6.4.2 DB模式