From fdeca4f3b5f875812a127ce57041187a7b6fe797 Mon Sep 17 00:00:00 2001 From: spacewander Date: Wed, 22 Sep 2021 10:47:21 +0800 Subject: [PATCH] fix(nacos): continue to process other services when request failed Fix #4949 Signed-off-by: spacewander --- apisix/discovery/nacos.lua | 17 ++++---- ci/centos7-ci.sh | 2 +- t/discovery/nacos2.t | 84 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 t/discovery/nacos2.t diff --git a/apisix/discovery/nacos.lua b/apisix/discovery/nacos.lua index a721dec144412..c41a8c2689ce4 100644 --- a/apisix/discovery/nacos.lua +++ b/apisix/discovery/nacos.lua @@ -293,18 +293,17 @@ local function fetch_full_registry(premature) applications = up_apps return end - local data, err + for _, service_info in ipairs(infos) do + local data, err 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) + local query_path = instance_list_path .. service_info.service_name + .. token_param .. namespace_param .. group_name_param + data, err = get_url(base_uri, query_path) if err then - log.error('get_url:', instance_list_path, ' err:', err) - if not applications then - applications = up_apps - end - return + log.error('get_url:', query_path, ' err:', err) + goto CONTINUE end for _, host in ipairs(data.hosts) do @@ -319,6 +318,8 @@ local function fetch_full_registry(premature) weight = host.weight or default_weight, }) end + + ::CONTINUE:: end local new_apps_md5sum = ngx.md5(core.json.encode(up_apps)) local old_apps_md5sum = ngx.md5(core.json.encode(applications)) diff --git a/ci/centos7-ci.sh b/ci/centos7-ci.sh index 19bdf1821cfad..03b3440b2a450 100755 --- a/ci/centos7-ci.sh +++ b/ci/centos7-ci.sh @@ -67,7 +67,7 @@ run_case() { make init ./utils/set-dns.sh # run test cases - FLUSH_ETCD=1 prove -I./test-nginx/lib -I./ -r t/ + FLUSH_ETCD=1 prove -I./test-nginx/lib -I./ -r t/discovery/nacos2.t } case_opt=$1 diff --git a/t/discovery/nacos2.t b/t/discovery/nacos2.t new file mode 100644 index 0000000000000..ac16b7ff0bc20 --- /dev/null +++ b/t/discovery/nacos2.t @@ -0,0 +1,84 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +workers(3); + +add_block_preprocessor(sub { + my ($block) = @_; + + if ((!defined $block->error_log) && (!defined $block->no_error_log)) { + $block->set_value("no_error_log", "[error]"); + } +}); + +run_tests(); + +__DATA__ + +=== TEST 1: continue to get nacos data after failure in a service +--- yaml_config +apisix: + node_listen: 1984 + config_center: yaml + enable_admin: false +discovery: + nacos: + host: + - "http://127.0.0.1:20999" + prefix: "/nacos/v1/" + fetch_interval: 1 + weight: 1 + timeout: + connect: 2000 + send: 2000 + read: 5000 +--- apisix_yaml +routes: + - + uri: /hello_ + upstream: + service_name: NOT-NACOS + discovery_type: nacos + type: roundrobin + - + uri: /hello + upstream: + service_name: APISIX-NACOS + discovery_type: nacos + type: roundrobin +#END +--- http_config + server { + listen 20999; + + location / { + access_by_lua_block { + if not package.loaded.hit then + package.loaded.hit = true + ngx.exit(502) + end + } + proxy_pass http://127.0.0.1:8858; + } + } +--- request +GET /hello +--- response_body_like eval +qr/server [1-2]/ +--- error_log +err:status = 502