## 11\. Upstreams *地址*:/upstreams *说明*:Upstream 是虚拟主机抽象,对给定的多个服务节点按照配置规则进行负载均衡。Upstream 的地址信息可以直接配置到`Route`(或`Service`) 上,当 Upstream 有重复时,就需要用“引用”方式避免重复了。 ### 11.1 请求方法 | NAME | 请求URI | 请求body | 说明 | | --- | --- | --- | --- | | POST | /upstreams | {...} | 新增upstreams | | GET | /upstreams | | 查询upstreams列表 | | GET | /upstreams/{name or id} | | 查询单个upstream | | GET | /targets/{target host:port or id}/upstream | | 同上 | | PATCH | /upstreams/{name or id} | {...} | 更新upstream | | ~ | /targets/{target host:port or id}/upstream | {...} | 同上 | | PUT | /upstreams/{name or id} | | 新增/更新upstream | | ~ | /targets/{target host:port or id}/upstream | | 同上 | | DELETE | /upstreams/{name or id} | | 删除upstream | | ~ | /targets/{target host:port or id}/upstream | | 同上 | | GET | /upstreams/{name or id}/health | | 获取upstream健康状态 | ### 11.2 body 请求参数 | 参数名 | 可选 | 默认值 | 说明 | 示例 | | --- | --- | --- | --- | --- | | name | 可选 | | 标识upsream名称 | examples.v1.upstream | | hash\_on | 可选 | none | | | | hash\_fallback | 可选 | | One of:`none`,`consumer`,`ip`,`header`, or`cookie` | | | hash\_on\_header | 可选 | | The header name to take the value from as hash input. Only required when`hash_on`is set to`header`. | | | hash\_fallback\_header | 可选 | | The header name to take the value from as hash input. Only required when`hash_fallback`is set to`header`. | | | hash\_on\_cookie | 可选 | | The cookie name to take the value from as hash input. Only required when`hash_on`or`hash_fallback`is set to`cookie`. If the specified cookie is not in the request, Kong will generate a value and set the cookie in the response. | | | hash\_on\_cookie\_path | 可选 | / | The cookie path to set in the response headers. Only required when`hash_on`or`hash_fallback`is set to`cookie`. Defaults to`"/"`. | | | slots | 可选 | 10000 | `10`\-`65536` | | | healthchecks | 参见active和passive | | | | | tags | 可选 | | 标签 | | healthchecks(ACTIVE) | https\_verify\_certificate | 可选 | true | | | | --- | --- | --- | --- | --- | | unhealthy.http\_statuses | 可选 | \[429, 404, 500, 501, 502, 503, 504, 505\] | 非健康的http状态码 | | | unhealthy.tcp\_failures | 可选 | 0 | 非健康tcp失败次数 | | | unhealthy.timeouts | 可选 | 0 | 超时次数,超过该值就设置为unhealthy | | | unhealthy.http\_failures | 可选 | 0 | http访问失败次数,超过该值设置为unhealthy,为0不探测 | | | unhealthy.interval | 可选 | 0 | 为0 不探测,两次探测非健康检查的间隔时长 | | | http\_path | 可选 | / | 健康检查的访问路径 | | | timeout | 可选 | 1 | socket超时时长 | | | healthy.http\_statuses | 可选 | \[200, 302\] | http状态为设置值其中 个,则对成功指标进行+1 | | | healthy.interval | 可选 | 0 | 两次健康检查探测的间隔时长(单位为秒) | | | healthy.successes | 可选 | 0 | 健康的成功计数指标 | | | https\_sni | 可选 | | | | | concurrency | 可选 | 10 | 并发数 | | | type | 可选 | http | `tcp`,`http`or`https` | | healthchecks(passive) | unhealthy.http\_failures | 可选 | 0 | 代理流量中的HTTP失败数(由定义健康检查。被动。不健康.http\_status)将目标视为不健康,如被动运行状况检查所观察到的 | 1 | | --- | --- | --- | --- | --- | | unhealthy.http\_statuses | 可选 | \[429, 500, 503\] | 访问http的状态码为其中之一的 | \[429,500,503\] | | unhealthy.tcp\_failures | 可选 | 0 | 被动运行状况检查观察到的被代理流量中认为目标不正常的TCP故障数 | 1 | | unhealthy.timeouts | 可选 | 0 | 被动运行状况检查所观察到的代理流量中认为目标不正常的超时数 | 1 | | type | 可选 | http | `tcp`,`http`or`https` | http | | healthy.successes | 可选 | 0 | 成功的次数 | 0 | | healthy.http\_statuses | 可选 | \[200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308\] | http成功的状态 | | ### 11.3 数据样例 body请求参数 ``` { "id": "91020192-062d-416f-a275-9addeeaffaf2", "created_at": 1422386534, "name": "my-upstream", "hash_on": "none", "hash_fallback": "none", "hash_on_cookie_path": "/", "slots": 10000, "healthchecks": { "active": { "https_verify_certificate": true, "unhealthy": { "http_statuses": [429, 404, 500, 501, 502, 503, 504, 505], "tcp_failures": 0, "timeouts": 0, "http_failures": 0, "interval": 0 }, "http_path": "/", "timeout": 1, "healthy": { "http_statuses": [200, 302], "interval": 0, "successes": 0 }, "https_sni": "example.com", "concurrency": 10, "type": "http" }, "passive": { "unhealthy": { "http_failures": 0, "http_statuses": [429, 500, 503], "tcp_failures": 0, "timeouts": 0 }, "type": "http", "healthy": { "successes": 0, "http_statuses": [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308] } } }, "tags": ["user-level", "low-priority"] } ``` upstream列表 ``` { "data": [{ "id": "a2e013e8-7623-4494-a347-6d29108ff68b", "created_at": 1422386534, "name": "my-upstream", "hash_on": "none", "hash_fallback": "none", "hash_on_cookie_path": "/", "slots": 10000, "healthchecks": { "active": { "https_verify_certificate": true, "unhealthy": { "http_statuses": [429, 404, 500, 501, 502, 503, 504, 505], "tcp_failures": 0, "timeouts": 0, "http_failures": 0, "interval": 0 }, "http_path": "/", "timeout": 1, "healthy": { "http_statuses": [200, 302], "interval": 0, "successes": 0 }, "https_sni": "example.com", "concurrency": 10, "type": "http" }, "passive": { "unhealthy": { "http_failures": 0, "http_statuses": [429, 500, 503], "tcp_failures": 0, "timeouts": 0 }, "type": "http", "healthy": { "successes": 0, "http_statuses": [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308] } } }, "tags": ["user-level", "low-priority"] }, { "id": "147f5ef0-1ed6-4711-b77f-489262f8bff7", "created_at": 1422386534, "name": "my-upstream", "hash_on": "none", "hash_fallback": "none", "hash_on_cookie_path": "/", "slots": 10000, "healthchecks": { "active": { "https_verify_certificate": true, "unhealthy": { "http_statuses": [429, 404, 500, 501, 502, 503, 504, 505], "tcp_failures": 0, "timeouts": 0, "http_failures": 0, "interval": 0 }, "http_path": "/", "timeout": 1, "healthy": { "http_statuses": [200, 302], "interval": 0, "successes": 0 }, "https_sni": "example.com", "concurrency": 10, "type": "http" }, "passive": { "unhealthy": { "http_failures": 0, "http_statuses": [429, 500, 503], "tcp_failures": 0, "timeouts": 0 }, "type": "http", "healthy": { "successes": 0, "http_statuses": [200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308] } } }, "tags": ["admin", "high-priority", "critical"] }], "next": "http://localhost:8001/upstreams?offset=6378122c-a0a1-438d-a5c6-efabae9fb969" } ```