This is an automated email from the ASF dual-hosted git repository.
tokers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 804437f perf: use events optimize nacos service discovery (#4347)
804437f is described below
commit 804437fe8a8815b69690a9bebedd8957b7508ccd
Author: Demogorgon314 <[email protected]>
AuthorDate: Wed Jun 2 17:49:44 2021 +0800
perf: use events optimize nacos service discovery (#4347)
related #4346
---
apisix/discovery/nacos.lua | 35 +++++++++++++++++++++++++++++++++--
docs/en/latest/discovery/nacos.md | 3 +--
t/discovery/nacos.t | 1 +
3 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/apisix/discovery/nacos.lua b/apisix/discovery/nacos.lua
index d4b53b9..ab661f2 100644
--- a/apisix/discovery/nacos.lua
+++ b/apisix/discovery/nacos.lua
@@ -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 = {
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/docs/en/latest/discovery/nacos.md
b/docs/en/latest/discovery/nacos.md
index 7e5a271..de996eb 100644
--- a/docs/en/latest/discovery/nacos.md
+++ b/docs/en/latest/discovery/nacos.md
@@ -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
diff --git a/t/discovery/nacos.t b/t/discovery/nacos.t
index cc59def..b139a99 100644
--- a/t/discovery/nacos.t
+++ b/t/discovery/nacos.t
@@ -20,6 +20,7 @@ repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();
+workers(4);
our $yaml_config = <<_EOC_;
apisix: