feat: nacos discovery support namespace (#4313)

This commit is contained in:
Demogorgon314 2021-05-27 20:08:12 +08:00 committed by GitHub
parent f0a5c94f83
commit 0746f81ea3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 256 additions and 10 deletions

View File

@ -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
@ -207,8 +215,16 @@ local function iter_and_add_service(services, values)
up = conf
end
local namespace_id
if up.discovery_args then
namespace_id = up.discovery_args.namespace_id
end
if up.discovery_type == 'nacos' then
core.table.insert(services, up.service_name)
core.table.insert(services, {
service_name = up.service_name,
namespace_id = namespace_id
})
end
::CONTINUE::
end
@ -222,7 +238,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()
@ -254,10 +269,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.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
@ -267,10 +283,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,

View File

@ -389,6 +389,15 @@ local upstream_schema = {
description = "discovery type",
type = "string",
},
discovery_args = {
type = "object",
properties = {
namespace_id = {
description = "namespace id",
type = "string",
},
}
},
pass_host = {
description = "mod of host passing",
type = "string",

View File

@ -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;
@ -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 ..

View File

@ -59,7 +59,7 @@ discovery:
### Upstream setting
Here is an example of routing a request with a URL 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 :
Here is an example of routing a request with a URL 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
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
@ -73,7 +73,7 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f
}'
```
The format response as below:
The formatted response as below:
```json
{
@ -99,3 +99,50 @@ 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",
"discovery_args": {
"namespace_id": "test_ns"
}
}
}'
```
The formatted 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",
"discovery_args": {
"namespace_id": "test_ns"
}
},
"priority": 0,
"uri": "\/nacosWithNamespaceId\/*"
}
},
"action": "set"
}
```

154
t/discovery/nacos.t vendored
View File

@ -251,3 +251,157 @@ 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
discovery_args:
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: error namespace_id - no auth
--- yaml_config eval: $::yaml_config
--- apisix_yaml
routes:
-
uri: /hello
upstream:
service_name: APISIX-NACOS-DEMO
discovery_type: nacos
type: roundrobin
discovery_args:
namespace_id: err_ns
#END
--- request
GET /hello
--- error_code: 503
=== TEST 10: 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
discovery_args:
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 11: 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",
"discovery_args": {
"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 12: 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]