Skip to content

Commit

Permalink
feat: nacos discovery support namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Demogorgon314 committed May 27, 2021
1 parent f7c6f4f commit 2a1fa6f
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 8 deletions.
25 changes: 18 additions & 7 deletions apisix/discovery/nacos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ local function get_token_param(base_uri, username, password)
return '&accessToken=' .. data.accessToken
end

local function get_namespace_param(namespace_id)
local param = ''
if namespace_id then
local args = {namespaceId = namespace_id}
param = '&' .. ngx.encode_args(args)
end
return param
end

local function get_base_uri()
local host = local_conf.discovery.nacos.host
Expand Down Expand Up @@ -208,7 +216,10 @@ local function iter_and_add_service(services, values)
end

if up.discovery_type == 'nacos' then
core.table.insert(services, up.service_name)
core.table.insert(services, {
service_name = up.service_name,
nacos_namespace_id = up.nacos_namespace_id
})
end
::CONTINUE::
end
Expand All @@ -222,7 +233,6 @@ local function get_nacos_services()
local get_upstreams = require('apisix.upstream').upstreams
local get_routes = require('apisix.router').http_routes
local get_services = require('apisix.http.service').services

local values = get_upstreams()
iter_and_add_service(services, values)
values = get_routes()
Expand Down Expand Up @@ -254,10 +264,11 @@ local function fetch_full_registry(premature)
applications = up_apps
return
end

local data, err
for _, service_name in ipairs(infos) do
data, err = get_url(base_uri, instance_list_path .. service_name .. token_param)
for _, service_info in ipairs(infos) do
local namespace_param = get_namespace_param(service_info.nacos_namespace_id);
data, err = get_url(base_uri, instance_list_path .. service_info.service_name
.. token_param .. namespace_param)
if err then
log.error('get_url:', instance_list_path, ' err:', err)
if not applications then
Expand All @@ -267,10 +278,10 @@ local function fetch_full_registry(premature)
end

for _, host in ipairs(data.hosts) do
local nodes = up_apps[service_name]
local nodes = up_apps[service_info.service_name]
if not nodes then
nodes = {}
up_apps[service_name] = nodes
up_apps[service_info.service_name] = nodes
end
core.table.insert(nodes, {
host = host.ip,
Expand Down
4 changes: 4 additions & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ local upstream_schema = {
description = "discovery type",
type = "string",
},
nacos_namespace_id = {
description = "nacos namespace id",
type = "string",
},
pass_host = {
description = "mod of host passing",
type = "string",
Expand Down
22 changes: 21 additions & 1 deletion ci/install-ext-services-via-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ wget https://raw.githubusercontent.com/api7/nacos-test-service/main/spring-nacos
curl https://raw.githubusercontent.com/api7/nacos-test-service/main/Dockerfile | docker build -t nacos-test-service:1.0-SNAPSHOT -f - .
docker run -d --rm --network nacos_net --env SERVICE_NAME=APISIX-NACOS --env NACOS_ADDR=nacos2:8848 --env SUFFIX_NUM=1 -p 18001:18001 --name nacos-service1 nacos-test-service:1.0-SNAPSHOT
docker run -d --rm --network nacos_net --env SERVICE_NAME=APISIX-NACOS --env NACOS_ADDR=nacos2:8848 --env SUFFIX_NUM=2 -p 18002:18001 --name nacos-service2 nacos-test-service:1.0-SNAPSHOT
url="127.0.0.1:18002/hello"
# register nacos service with namespaceId=test_ns
docker run -d --rm --network nacos_net --env SERVICE_NAME=APISIX-NACOS --env NACOS_ADDR=nacos2:8848 --env NAMESPACE=test_ns --env SUFFIX_NUM=1 -p 18003:18001 --name nacos-service3 nacos-test-service:1.0-SNAPSHOT
# register nacos service with group=test_group
docker run -d --rm --network nacos_net --env SERVICE_NAME=APISIX-NACOS --env NACOS_ADDR=nacos2:8848 --env GROUP=test_group --env SUFFIX_NUM=1 -p 18004:18001 --name nacos-service4 nacos-test-service:1.0-SNAPSHOT
# register nacos service with namespaceId=test_ns and group=test_group
docker run -d --rm --network nacos_net --env SERVICE_NAME=APISIX-NACOS --env NACOS_ADDR=nacos2:8848 --env NAMESPACE=test_ns --env GROUP=test_group --env SUFFIX_NUM=1 -p 18005:18001 --name nacos-service5 nacos-test-service:1.0-SNAPSHOT

url="127.0.0.1:18005/hello"
until [[ "$(curl -s -o /dev/null -w ''%{http_code}'' $url)" == "200" ]]; do
echo 'wait nacos service...'
sleep 1;
Expand All @@ -64,4 +71,17 @@ until [[ $(curl -s "127.0.0.1:8858/nacos/v1/ns/service/list?pageNo=1&pageSize=2
echo 'wait nacos reg...'
sleep 1;
done
until [[ $(curl -s "127.0.0.1:8858/nacos/v1/ns/service/list?groupName=test_group&pageNo=1&pageSize=2" | grep "APISIX-NACOS") ]]; do
echo 'wait nacos reg...'
sleep 1;
done
until [[ $(curl -s "127.0.0.1:8858/nacos/v1/ns/service/list?groupName=DEFAULT_GROUP&namespaceId=test_ns&pageNo=1&pageSize=2" | grep "APISIX-NACOS") ]]; do
echo 'wait nacos reg...'
sleep 1;
done
until [[ $(curl -s "127.0.0.1:8858/nacos/v1/ns/service/list?groupName=test_group&namespaceId=test_ns&pageNo=1&pageSize=2" | grep "APISIX-NACOS") ]]; do
echo 'wait nacos reg...'
sleep 1;
done

cd ..
43 changes: 43 additions & 0 deletions docs/en/latest/discovery/nacos.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,46 @@ The format response as below:
"action": "set"
}
```

Example of routing a request with a URL of "/nacosWithNamespaceId/*" to a service which name, namespaceId "http://192.168.33.1:8848/nacos/v1/ns/instance/list?serviceName=APISIX-NACOS&namespaceId=test_ns" and use nacos discovery client in the registry :

```shell
$ curl http://127.0.0.1:9080/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"uri": "/nacosWithNamespaceId/*",
"upstream": {
"service_name": "APISIX-NACOS",
"type": "roundrobin",
"discovery_type": "nacos",
"nacos_namespace_id": "test_ns"
}
}'
```

The format response as below:

```json
{
"node": {
"key": "\/apisix\/routes\/2",
"value": {
"id": "1",
"create_time": 1615796097,
"status": 1,
"update_time": 1615799165,
"upstream": {
"hash_on": "vars",
"pass_host": "pass",
"scheme": "http",
"service_name": "APISIX-NACOS",
"type": "roundrobin",
"discovery_type": "nacos",
"nacos_namespace_id": "test_ns"
},
"priority": 0,
"uri": "\/nacosWithNamespaceId\/*"
}
},
"action": "set"
}
```
131 changes: 131 additions & 0 deletions t/discovery/nacos.t
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,134 @@ discovery:
]
--- no_error_log
[error]



=== TEST 8: get APISIX-NACOS info from NACOS - no auth with namespace
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
nacos_namespace_id: test_ns
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
--- no_error_log
[error]



=== TEST 9: get APISIX-NACOS info from NACOS - configured in services with namespace
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream:
service_name: APISIX-NACOS
discovery_type: nacos
type: roundrobin
nacos_namespace_id: test_ns
#END
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
--- no_error_log
[error]



=== TEST 10: get APISIX-NACOS info from NACOS - configured in upstreams + etcd with namespace
--- extra_yaml_config
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
fetch_interval: 1
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
[[{
"service_name": "APISIX-NACOS",
"discovery_type": "nacos",
"type": "roundrobin",
"nacos_namespace_id": "test_ns"
}]]
)

if code >= 300 then
ngx.status = code
ngx.say(body)
return
end

local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream_id": 1
}]]
)

if code >= 300 then
ngx.status = code
end

ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 11: hit with namespace
--- extra_yaml_config
discovery:
nacos:
host:
- "http://127.0.0.1:8858"
fetch_interval: 1
--- pipelined_requests eval
[
"GET /hello",
"GET /hello",
]
--- response_body_like eval
[
qr/server [1-2]/,
qr/server [1-2]/,
]
--- no_error_log
[error]

0 comments on commit 2a1fa6f

Please sign in to comment.