Skip to content

Commit

Permalink
fix(traffic-split): configure multiple "rules", the request will be c…
Browse files Browse the repository at this point in the history
…onfused between upstream (#4092)
  • Loading branch information
Firstsawyou authored Apr 22, 2021
1 parent 3ac7c1b commit 9456568
Show file tree
Hide file tree
Showing 4 changed files with 511 additions and 2 deletions.
3 changes: 1 addition & 2 deletions apisix/plugins/traffic-split.lua
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ function _M.access(conf, ctx)
return
end

local rr_up, err = core.lrucache.plugin_ctx(lrucache, ctx, nil, new_rr_obj,
weighted_upstreams)
local rr_up, err = lrucache(weighted_upstreams, nil, new_rr_obj, weighted_upstreams)
if not rr_up then
core.log.error("lrucache roundrobin failed: ", err)
return 500
Expand Down
93 changes: 93 additions & 0 deletions docs/en/latest/plugins/traffic-split.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ title: traffic-split
- [Grayscale Release](#grayscale-release)
- [Blue-green Release](#blue-green-release)
- [Custom Release](#custom-release)
- [Matching rules correspond to upstream](#matching-rules-correspond-to-upstream)
- [Disable Plugin](#disable-plugin)

## Name
Expand Down Expand Up @@ -482,6 +483,98 @@ Content-Type: text/html; charset=utf-8
hello 1980
```

### Matching rules correspond to upstream

By configuring multiple `rules`, we can achieve one-to-one correspondence between different matching rules and upstream.

**Example:**

When the request header `x-api-id` is equal to 1, it hits the upstream with port 1981; when `x-api-id` is equal to 2, it hits the upstream with port 1982; otherwise, it hits the upstream with port 1980 (the upstream response data is the corresponding port number).

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"traffic-split": {
"rules": [
{
"match": [
{
"vars": [
["http_x-api-id","==","1"]
]
}
],
"weighted_upstreams": [
{
"upstream": {
"name": "upstream-A",
"type": "roundrobin",
"nodes": {
"127.0.0.1:1981":1
}
},
"weight": 3
}
]
},
{
"match": [
{
"vars": [
["http_x-api-id","==","2"]
]
}
],
"weighted_upstreams": [
{
"upstream": {
"name": "upstream-B",
"type": "roundrobin",
"nodes": {
"127.0.0.1:1982":1
}
},
"weight": 3
}
]
}
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'
```

**Test plugin:**

The request header `x-api-id` is equal to 1, hitting the upstream with the 1981 port.

```shell
$ curl http://127.0.0.1:9080/hello -H 'x-api-id: 1'
1981
```

The request header `x-api-id` is equal to 2, hitting the upstream with the 1982 port.

```shell
$ curl http://127.0.0.1:9080/hello -H 'x-api-id: 2'
1982
```

The request header `x-api-id` is equal to 3, the rule does not match, and it hits the upstream with port 1980.

```shell
$ curl http://127.0.0.1:9080/hello -H 'x-api-id: 3'
1980
```

## Disable Plugin

When you want to remove the traffic-split plugin, it's very simple, just delete the corresponding json configuration in the plugin configuration, no need to restart the service, it will take effect immediately:
Expand Down
93 changes: 93 additions & 0 deletions docs/zh/latest/plugins/traffic-split.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ title: traffic-split
- [灰度发布](#灰度发布)
- [蓝绿发布](#蓝绿发布)
- [自定义发布](#自定义发布)
- [匹配规则与上游对应](#匹配规则与上游对应)
- [禁用插件](#禁用插件)

## 名字
Expand Down Expand Up @@ -493,6 +494,98 @@ Content-Type: text/html; charset=utf-8
hello 1980
```

### 匹配规则与上游对应

通过配置多个 `rules`,我们可以实现不同的匹配规则与上游一一对应。

**示例:**

当请求头 `x-api-id` 等于 1 时,命中 1981 端口的上游;当 `x-api-id` 等于 2 时,命中 1982 端口的上游;否则,命中 1980 端口的上游(上游响应数据为对应的端口号)。

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"traffic-split": {
"rules": [
{
"match": [
{
"vars": [
["http_x-api-id","==","1"]
]
}
],
"weighted_upstreams": [
{
"upstream": {
"name": "upstream-A",
"type": "roundrobin",
"nodes": {
"127.0.0.1:1981":1
}
},
"weight": 3
}
]
},
{
"match": [
{
"vars": [
["http_x-api-id","==","2"]
]
}
],
"weighted_upstreams": [
{
"upstream": {
"name": "upstream-B",
"type": "roundrobin",
"nodes": {
"127.0.0.1:1982":1
}
},
"weight": 3
}
]
}
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'
```

**测试插件:**

请求头 `x-api-id` 等于 1,命中带 1981 端口的上游。

```shell
$ curl http://127.0.0.1:9080/hello -H 'x-api-id: 1'
1981
```

请求头 `x-api-id` 等于 2,命中带 1982 端口的上游。

```shell
$ curl http://127.0.0.1:9080/hello -H 'x-api-id: 2'
1982
```

请求头 `x-api-id` 等于 3,规则不匹配,命中带 1980 端口的上游。

```shell
$ curl http://127.0.0.1:9080/hello -H 'x-api-id: 3'
1980
```

## 禁用插件

当你想去掉 traffic-split 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:
Expand Down
Loading

0 comments on commit 9456568

Please sign in to comment.