Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mqtt-proxy): support using route's upstream #5666

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apisix/stream/plugins/mqtt-proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ local schema = {
},
}
},
required = {"protocol_name", "protocol_level", "upstream"},
required = {"protocol_name", "protocol_level"},
}


Expand Down Expand Up @@ -164,6 +164,10 @@ function _M.preread(conf, ctx)

core.log.info("mqtt client id: ", res.client_id)

if not conf.upstream then
return
end

local host = conf.upstream.host
if not host then
host = conf.upstream.ip
Expand Down
15 changes: 10 additions & 5 deletions docs/en/latest/plugins/mqtt-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ And this plugin both support MQTT protocol [3.1.*](http://docs.oasis-open.org/mq
| -------------- | ------- | ----------- | ------- | ----- | -------------------------------------------------------------------------------------- |
| protocol_name | string | required | | | Name of protocol, should be `MQTT` in normal. |
| protocol_level | integer | required | | | Level of protocol, it should be `4` for MQTT `3.1.*`. it should be `5` for MQTT `5.0`. |
| upstream | object | deprecated | | | Use separate upstream in the route instead. |
| upstream.host | string | required | | | the IP or host of upstream, will forward current request to. |
| upstream.ip | string | deprecated | | | Use "host" instead. IP address of upstream, will forward current request to.|
| upstream.port | number | required | | | Port of upstream, will forward current request to. |
Expand Down Expand Up @@ -73,12 +74,16 @@ curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f03
"plugins": {
"mqtt-proxy": {
"protocol_name": "MQTT",
"protocol_level": 4,
"upstream": {
"host": "127.0.0.1",
"port": 1980
}
"protocol_level": 4
}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "127.0.0.1",
"port": 1980,
"weight": 1
}]
}
}'
```
Expand Down
15 changes: 10 additions & 5 deletions docs/zh/latest/plugins/mqtt-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ title: mqtt-proxy
| -------------- | ------- | ------ | ------ | ------ | ------------------------------------------------------ |
| protocol_name | string | 必须 | | | 协议名称,正常情况下应为“ MQTT” |
| protocol_level | integer | 必须 | | | 协议级别,MQTT `3.1.*` 应为 `4` ,MQTT `5.0` 应是`5`。 |
| upstream | object | 废弃 | | | 推荐改用 route 上配置的上游信息 |
| upstream.host | string | 必须 | | | 将当前请求转发到的上游的 IP 地址或域名 |
| upstream.ip | string | 废弃 | | | 推荐使用“host”代替。将当前请求转发到的上游的 IP 地址 |
| upstream.port | number | 必须 | | | 将当前请求转发到的上游的端口 |
Expand Down Expand Up @@ -71,12 +72,16 @@ curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f03
"plugins": {
"mqtt-proxy": {
"protocol_name": "MQTT",
"protocol_level": 4,
"upstream": {
"host": "127.0.0.1",
"port": 1980
}
"protocol_level": 4
}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "127.0.0.1",
"port": 1980,
"weight": 1
}]
}
}'
```
Expand Down
53 changes: 53 additions & 0 deletions t/stream-plugin/mqtt-proxy.t
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,56 @@ passed
--- error_log
failed to parse domain: loc, error:
--- timeout: 10



=== TEST 11: set route with upstream
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/stream_routes/1',
ngx.HTTP_PUT,
[[{
"remote_addr": "127.0.0.1",
"server_port": 1985,
"plugins": {
"mqtt-proxy": {
"protocol_name": "MQTT",
"protocol_level": 4
}
},
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "127.0.0.1",
"port": 1995,
"weight": 1
}]
}
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 12: hit route
--- stream_enable
--- stream_request eval
"\x10\x0f\x00\x04\x4d\x51\x54\x54\x04\x02\x00\x3c\x00\x03\x66\x6f\x6f"
--- stream_response
hello world
--- no_error_log
[error]