Skip to content

Commit

Permalink
feat: nacos discovery support group (#4325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Demogorgon314 authored May 31, 2021
1 parent 01360fb commit 0d7fa65
Show file tree
Hide file tree
Showing 4 changed files with 484 additions and 3 deletions.
20 changes: 18 additions & 2 deletions apisix/discovery/nacos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ local function get_namespace_param(namespace_id)
return param
end

local function get_group_name_param(group_name)
local param = ''
if group_name then
local args = {groupName = group_name}
param = '&' .. ngx.encode_args(args)
end
return param
end

local function get_base_uri()
local host = local_conf.discovery.nacos.host
-- TODO Add health check to get healthy nodes.
Expand Down Expand Up @@ -220,10 +229,16 @@ local function iter_and_add_service(services, values)
namespace_id = up.discovery_args.namespace_id
end

local group_name
if up.discovery_args then
group_name = up.discovery_args.group_name
end

if up.discovery_type == 'nacos' then
core.table.insert(services, {
service_name = up.service_name,
namespace_id = namespace_id
namespace_id = namespace_id,
group_name = group_name
})
end
::CONTINUE::
Expand Down Expand Up @@ -272,8 +287,9 @@ local function fetch_full_registry(premature)
local data, err
for _, service_info in ipairs(infos) do
local namespace_param = get_namespace_param(service_info.namespace_id);
local group_name_param = get_group_name_param(service_info.group_name);
data, err = get_url(base_uri, instance_list_path .. service_info.service_name
.. token_param .. namespace_param)
.. token_param .. namespace_param .. group_name_param)
if err then
log.error('get_url:', instance_list_path, ' err:', err)
if not applications then
Expand Down
4 changes: 4 additions & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ local upstream_schema = {
description = "namespace id",
type = "string",
},
group_name = {
description = "group name",
type = "string",
},
}
},
pass_host = {
Expand Down
111 changes: 110 additions & 1 deletion docs/en/latest/discovery/nacos.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ The formatted response as below:
}
```

### discovery_args

| Name | Type | Requirement | Default | Valid | Description |
| ------------ | ------ | ----------- | ------- | ----- | ------------------------------------------------------------ |
| namespace_id | string | optional | public | | This parameter is used to specify the namespace of the corresponding service |
| group_name | string | optional | DEFAULT_GROUP | | This parameter is used to specify the group of the corresponding service |

#### Specify the namespace

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
Expand All @@ -124,7 +133,7 @@ The formatted response as below:
"node": {
"key": "\/apisix\/routes\/2",
"value": {
"id": "1",
"id": "2",
"create_time": 1615796097,
"status": 1,
"update_time": 1615799165,
Expand All @@ -146,3 +155,103 @@ The formatted response as below:
"action": "set"
}
```

#### Specify the group

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

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

The formatted response as below:

```json
{
"node": {
"key": "\/apisix\/routes\/3",
"value": {
"id": "3",
"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": {
"group_name": "test_group"
}
},
"priority": 0,
"uri": "\/nacosWithGroupName\/*"
}
},
"action": "set"
}
```

#### Specify the namespace and group

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

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

The formatted response as below:

```json
{
"node": {
"key": "\/apisix\/routes\/4",
"value": {
"id": "4",
"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",
"group_name": "test_group"
}
},
"priority": 0,
"uri": "\/nacosWithNamespaceIdAndGroupName\/*"
}
},
"action": "set"
}
```
Loading

0 comments on commit 0d7fa65

Please sign in to comment.