This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 9e3fd0002 feat: stream subsystem support nacos service discovery
(#8584)
9e3fd0002 is described below
commit 9e3fd00021c26be38b9d83aee76fa5bc7b6d4501
Author: Ashing Zheng <[email protected]>
AuthorDate: Thu Jan 12 08:55:52 2023 +0800
feat: stream subsystem support nacos service discovery (#8584)
Fixes https://github.com/apache/apisix/issues/7779
---
apisix/cli/ngx_tpl.lua | 1 +
apisix/discovery/nacos/init.lua | 3 ++
apisix/init.lua | 5 +++
apisix/router.lua | 3 ++
conf/config-default.yaml | 1 +
docs/en/latest/discovery/nacos.md | 19 ++++++++
docs/zh/latest/discovery/nacos.md | 19 ++++++++
t/APISIX.pm | 1 +
t/discovery/stream/nacos.t | 92 +++++++++++++++++++++++++++++++++++++++
9 files changed, 144 insertions(+)
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 7a1f4e9c7..95ac3b763 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -139,6 +139,7 @@ stream {
lua_shared_dict lrucache-lock-stream {*
stream.lua_shared_dict["lrucache-lock-stream"] *};
lua_shared_dict etcd-cluster-health-check-stream {*
stream.lua_shared_dict["etcd-cluster-health-check-stream"] *};
+ lua_shared_dict worker-events-stream {*
stream.lua_shared_dict["worker-events-stream"] *};
{% if enabled_stream_plugins["limit-conn"] then %}
lua_shared_dict plugin-limit-conn-stream {*
stream.lua_shared_dict["plugin-limit-conn-stream"] *};
diff --git a/apisix/discovery/nacos/init.lua b/apisix/discovery/nacos/init.lua
index 225636eb3..e12c025c5 100644
--- a/apisix/discovery/nacos/init.lua
+++ b/apisix/discovery/nacos/init.lua
@@ -236,6 +236,7 @@ local function get_nacos_services()
-- here we use lazy load to work around circle dependency
local get_upstreams = require('apisix.upstream').upstreams
local get_routes = require('apisix.router').http_routes
+ local get_stream_routes = require('apisix.router').stream_routes
local get_services = require('apisix.http.service').services
local values = get_upstreams()
iter_and_add_service(services, values)
@@ -243,6 +244,8 @@ local function get_nacos_services()
iter_and_add_service(services, values)
values = get_services()
iter_and_add_service(services, values)
+ values = get_stream_routes()
+ iter_and_add_service(services, values)
return services
end
diff --git a/apisix/init.lua b/apisix/init.lua
index d238bd161..b518f0e30 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -907,6 +907,11 @@ function _M.stream_init_worker()
router.stream_init_worker()
apisix_upstream.init_worker()
+ local we = require("resty.worker.events")
+ local ok, err = we.configure({shm = "worker-events-stream", interval =
0.1})
+ if not ok then
+ error("failed to init worker event: " .. err)
+ end
local discovery = require("apisix.discovery.init").discovery
if discovery and discovery.init_worker then
discovery.init_worker()
diff --git a/apisix/router.lua b/apisix/router.lua
index 9bdafebbd..2fd14917c 100644
--- a/apisix/router.lua
+++ b/apisix/router.lua
@@ -123,6 +123,9 @@ function _M.ssls()
end
function _M.http_routes()
+ if not _M.router_http then
+ return nil, nil
+ end
return _M.router_http.routes()
end
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index 4dca7bac0..9e47f0660 100755
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -164,6 +164,7 @@ nginx_config: # config for render the
template to generate n
etcd-cluster-health-check-stream: 10m
lrucache-lock-stream: 10m
plugin-limit-conn-stream: 10m
+ worker-events-stream: 10m
# As user can add arbitrary configurations in the snippet,
# it is user's responsibility to check the configurations
diff --git a/docs/en/latest/discovery/nacos.md
b/docs/en/latest/discovery/nacos.md
index 684098e7f..ace1860e9 100644
--- a/docs/en/latest/discovery/nacos.md
+++ b/docs/en/latest/discovery/nacos.md
@@ -56,6 +56,8 @@ discovery:
### Upstream setting
+#### L7
+
Here is an example of routing a request with an URI of "/nacos/*" to a service
which named
"http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS"
and use nacos discovery client in the registry:
```shell
@@ -96,6 +98,23 @@ The formatted response as below:
}
```
+#### L4
+
+Nacos service discovery also supports use in L4, the configuration method is
similar to L7.
+
+```shell
+$ curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+{
+ "remote_addr": "127.0.0.1",
+ "upstream": {
+ "scheme": "tcp",
+ "discovery_type": "nacos",
+ "service_name": "APISIX-NACOS",
+ "type": "roundrobin"
+ }
+}'
+```
+
### discovery_args
| Name | Type | Requirement | Default | Valid | Description
|
diff --git a/docs/zh/latest/discovery/nacos.md
b/docs/zh/latest/discovery/nacos.md
index c0d7261e3..b30e1dbd3 100644
--- a/docs/zh/latest/discovery/nacos.md
+++ b/docs/zh/latest/discovery/nacos.md
@@ -56,6 +56,8 @@ discovery:
### Upstream 设置
+#### 七层
+
例如,转发 URI 匹配 "/nacos/*" 的请求到一个上游服务,
该服务在 Nacos 中的服务名是 APISIX-NACOS ,查询地址是
http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS
,创建路由时指定服务发现类型为 nacos 。
@@ -97,6 +99,23 @@ $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H
'X-API-KEY: edd1c9f034335f
}
```
+#### 四层
+
+nacos 服务发现也支持在四层中使用,配置方式与七层的类似。
+
+```shell
+$ curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+{
+ "remote_addr": "127.0.0.1",
+ "upstream": {
+ "scheme": "tcp",
+ "discovery_type": "nacos",
+ "service_name": "APISIX-NACOS",
+ "type": "roundrobin"
+ }
+}'
+```
+
### 参数
| 名字 | 类型 | 可选项 | 默认值 | 有效值 | 说明
|
diff --git a/t/APISIX.pm b/t/APISIX.pm
index fe534b20c..7fa11dd10 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -391,6 +391,7 @@ _EOC_
lua_shared_dict lrucache-lock-stream 10m;
lua_shared_dict plugin-limit-conn-stream 10m;
lua_shared_dict etcd-cluster-health-check-stream 10m;
+ lua_shared_dict worker-events-stream 10m;
upstream apisix_backend {
server 127.0.0.1:1900;
diff --git a/t/discovery/stream/nacos.t b/t/discovery/stream/nacos.t
new file mode 100644
index 000000000..1a1053cfd
--- /dev/null
+++ b/t/discovery/stream/nacos.t
@@ -0,0 +1,92 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('info');
+worker_connections(256);
+no_root_location();
+no_shuffle();
+workers(4);
+
+our $yaml_config = <<_EOC_;
+apisix:
+ node_listen: 1984
+deployment:
+ role: data_plane
+ role_data_plane:
+ config_provider: yaml
+discovery:
+ nacos:
+ host:
+ - "http://127.0.0.1:8858"
+ prefix: "/nacos/v1/"
+ fetch_interval: 1
+ weight: 1
+ timeout:
+ connect: 2000
+ send: 2000
+ read: 5000
+
+_EOC_
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->stream_request) {
+ $block->set_value("stream_request", "GET /hello HTTP/1.1\r\nHost:
127.0.0.1:1985\r\nConnection: close\r\n\r\n");
+ }
+
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: get APISIX-NACOS info from NACOS - no auth
+--- yaml_config eval: $::yaml_config
+--- apisix_yaml
+stream_routes:
+ - server_addr: 127.0.0.1
+ server_port: 1985
+ id: 1
+ upstream:
+ service_name: APISIX-NACOS
+ discovery_type: nacos
+ type: roundrobin
+#END
+--- stream_response eval
+qr/server [1-2]/
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: error service_name name - no auth
+--- yaml_config eval: $::yaml_config
+--- apisix_yaml
+stream_routes:
+ - server_addr: 127.0.0.1
+ server_port: 1985
+ id: 1
+ upstream:
+ service_name: APISIX-NACOS-DEMO
+ discovery_type: nacos
+ type: roundrobin
+#END
+--- error_log
+no valid upstream node