Skip to content

Commit

Permalink
feat: stream subsystem support nacos service discovery (#8584)
Browse files Browse the repository at this point in the history
Fixes #7779
  • Loading branch information
ronething authored Jan 12, 2023
1 parent c9ed5d7 commit 9e3fd00
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"] *};
Expand Down
3 changes: 3 additions & 0 deletions apisix/discovery/nacos/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,16 @@ 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)
values = get_routes()
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

Expand Down
5 changes: 5 additions & 0 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions apisix/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions docs/en/latest/discovery/nacos.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 |
Expand Down
19 changes: 19 additions & 0 deletions docs/zh/latest/discovery/nacos.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 。
Expand Down Expand Up @@ -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"
}
}'
```

### 参数

| 名字 | 类型 | 可选项 | 默认值 | 有效值 | 说明 |
Expand Down
1 change: 1 addition & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
92 changes: 92 additions & 0 deletions t/discovery/stream/nacos.t
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9e3fd00

Please sign in to comment.