Skip to content

Commit

Permalink
perf: use events optimize nacos service discovery (#4347)
Browse files Browse the repository at this point in the history
related #4346
  • Loading branch information
Demogorgon314 authored Jun 2, 2021
1 parent 2f34e1a commit 804437f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
35 changes: 33 additions & 2 deletions apisix/discovery/nacos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ local applications
local auth_path = 'auth/login'
local instance_list_path = 'ns/instance/list?healthyOnly=true&serviceName='

local events
local events_list

local host_pattern = [[^http(s)?:\/\/[a-zA-Z0-9-_.:\@]+$]]
local prefix_pattern = [[^[\/a-zA-Z0-9-_.]+$]]
local schema = {
Expand Down Expand Up @@ -82,6 +85,12 @@ local schema = {

local _M = {}

local function discovery_nacos_callback(data, event, source, pid)
applications = data
log.notice("update local variable application, event is: ", event,
"source: ", source, "server pid:", pid,
", application: ", core.json.encode(applications, true))
end

local function request(request_uri, path, body, method, basic_auth)
local url = request_uri .. path
Expand Down Expand Up @@ -286,8 +295,8 @@ local function fetch_full_registry(premature)
end
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);
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 .. group_name_param)
if err then
Expand All @@ -311,7 +320,18 @@ local function fetch_full_registry(premature)
})
end
end
local new_apps_md5sum = ngx.md5(core.json.encode(up_apps))
local old_apps_md5sum = ngx.md5(core.json.encode(applications))
if new_apps_md5sum == old_apps_md5sum then
return
end
applications = up_apps
local ok, err = events.post(events_list._source, events_list.updating,
applications)
if not ok then
log.error("post_event failure with ", events_list._source,
", update application error: ", err)
end
end


Expand Down Expand Up @@ -344,6 +364,17 @@ function _M.init_worker()
error('invalid nacos configuration: ' .. err)
return
end

events = require("resty.worker.events")
events_list = events.event_list("discovery_nacos_update_application",
"updating")

if 0 ~= ngx.worker.id() then
events.register(discovery_nacos_callback, events_list._source,
events_list.updating)
return
end

default_weight = local_conf.discovery.nacos.weight
log.info('default_weight:', default_weight)
local fetch_interval = local_conf.discovery.nacos.fetch_interval
Expand Down
3 changes: 1 addition & 2 deletions docs/en/latest/discovery/nacos.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ This is experimental discovery module for Nacos.

The performance of this module needs to be improved:

1. avoid synchroning configuration in each workers. You can refer the implementation in `consul_kv.lua`.
2. send the request parallelly.
1. send the request parallelly.

### Configuration for Nacos

Expand Down
1 change: 1 addition & 0 deletions t/discovery/nacos.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
workers(4);

our $yaml_config = <<_EOC_;
apisix:
Expand Down

0 comments on commit 804437f

Please sign in to comment.