diff --git a/bin/apisix b/bin/apisix index a9010a3bc528..3f69dd2a62c4 100755 --- a/bin/apisix +++ b/bin/apisix @@ -536,6 +536,10 @@ local function init_etcd(show_output) error("failed to read local yaml config of apisix: " .. err) end + if yaml_conf.apisix.config_center ~= "etcd" then + return true + end + local etcd_conf = yaml_conf.etcd local uri = etcd_conf.host .. "/v2/keys" .. (etcd_conf.prefix or "") diff --git a/conf/apisix.yaml b/conf/apisix.yaml new file mode 100644 index 000000000000..1a406aea2948 --- /dev/null +++ b/conf/apisix.yaml @@ -0,0 +1,25 @@ +# +# 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. +# +# example +routes: + - + uri: /hello + upstream: + nodes: + "127.0.0.1:1980": 1 + type: roundrobin +#END diff --git a/lua/apisix/admin/init.lua b/lua/apisix/admin/init.lua index 2c035c064af0..128340f3cc96 100644 --- a/lua/apisix/admin/init.lua +++ b/lua/apisix/admin/init.lua @@ -36,7 +36,6 @@ local resources = { proto = require("apisix.admin.proto"), global_rules = require("apisix.admin.global_rules"), stream_routes = require("apisix.admin.stream_routes"), - node_status = require("apisix.admin.node_status"), } diff --git a/lua/apisix/admin/node_status.lua b/lua/apisix/admin/node_status.lua deleted file mode 100644 index a5d42afe26e0..000000000000 --- a/lua/apisix/admin/node_status.lua +++ /dev/null @@ -1,41 +0,0 @@ --- --- 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. --- -local core = require("apisix.core") - - -local _M = { - version = 0.1, -} - - -function _M.get(id) - local key = "/node_status" - if id then - key = key .. "/" .. id - end - - local res, err = core.etcd.get(key) - if not res then - core.log.error("failed to get route[", key, "]: ", err) - return 500, {error_msg = err} - end - - return res.status, res.body -end - - -return _M diff --git a/lua/apisix/plugins/heartbeat.lua b/lua/apisix/plugins/heartbeat.lua index fca021ddaf7f..b9275c3366bb 100644 --- a/lua/apisix/plugins/heartbeat.lua +++ b/lua/apisix/plugins/heartbeat.lua @@ -63,15 +63,26 @@ end local function report() -- ngx.sleep(3) - local etcd_version, err = core.etcd.server_version() - if not etcd_version then - core.log.error("failed to fetch etcd version: ", err) + local etcd_version, etcd_version_err + local local_conf = core.config.local_conf() + + if local_conf.apisix.config_center == "etcd" then + etcd_version, etcd_version_err = core.etcd.server_version() + if not etcd_version then + core.log.error("failed to fetch etcd version: ", etcd_version_err) + else + etcd_version = etcd_version.body and etcd_version.body.etcdserver + end end + core.log.warn(core.json.encode(etcd_version)) + local info = { version = core.version, - plugins = core.config.local_conf().plugins, - etcd_version = etcd_version.body, + plugins = local_conf.plugins, + config_center = local_conf.apisix.config_center, + etcd_version = etcd_version, + etcd_version_err = etcd_version_err, uuid = core.id.get(), } @@ -81,7 +92,7 @@ local function report() core.log.error("failed to encode hearbeat information: ", err) return end - core.log.debug("heartbeat body: ", args) + core.log.warn("heartbeat body: ", args) local res res, err = request_apisix_svr(args) diff --git a/lua/apisix/plugins/node-status.lua b/lua/apisix/plugins/node-status.lua index d7254b020ea0..d34d271c1d9b 100644 --- a/lua/apisix/plugins/node-status.lua +++ b/lua/apisix/plugins/node-status.lua @@ -24,7 +24,7 @@ local ipairs = ipairs local _M = { version = 0.1, - priority = 1000, -- TODO: add a type field, may be a good idea + priority = 1000, name = plugin_name, } @@ -37,32 +37,15 @@ local ngx_statu_items = { local function collect() - core.log.info("try to collect node status from etcd: ", - "/node_status/" .. apisix_id) - local res, err = core.etcd.get("/node_status/" .. apisix_id) - if not res then - return 500, {error = err} - end - - return res.status, res.body -end - - -local function run_loop() local res, err = core.http.request_self("/apisix/nginx_status", { keepalive = false, }) if not res then - if err then - return core.log.error("failed to fetch nginx status: ", err) - end - return + return 500, "failed to fetch nginx status: " .. err end if res.status ~= 200 then - core.log.error("failed to fetch nginx status, response code: ", - res.status) - return + return res.status end -- Active connections: 2 @@ -72,8 +55,7 @@ local function run_loop() local iterator, err = re_gmatch(res.body, [[(\d+)]], "jmo") if not iterator then - core.log.error("failed to re.gmatch Nginx status: ", err) - return + return 500, "failed to re.gmatch Nginx status: " .. err end core.table.clear(ngx_status) @@ -86,17 +68,7 @@ local function run_loop() ngx_status[name] = val[0] end - local res, err = core.etcd.set("/node_status/" .. apisix_id, ngx_status) - if not res then - core.log.error("failed to create etcd client: ", err) - return - end - - if res.status >= 300 then - core.log.error("failed to update node status, code: ", res.status, - " body: ", core.json.encode(res.body, true)) - return - end + return 200, core.json.encode({id = apisix_id, status = ngx_status}) end @@ -111,13 +83,4 @@ function _M.api() end - local timer -function _M.init() - if timer or ngx.worker.id() ~= 0 then - return - end - timer = core.timer.new(plugin_name, run_loop, {check_interval = 5 * 60}) -end - - return _M diff --git a/t/config-center-yaml/route-upstream.t b/t/config-center-yaml/route-upstream.t index 7da4850a6fb4..b3a9cdb11796 100644 --- a/t/config-center-yaml/route-upstream.t +++ b/t/config-center-yaml/route-upstream.t @@ -84,6 +84,7 @@ GET /hello failed to find upstream by id: 1111 + === TEST 3: upstream_id priority upstream --- yaml_config eval: $::yaml_config --- apisix_yaml diff --git a/t/plugin/node-status.t b/t/plugin/node-status.t index 9abde21f7a40..d73dcc1c4fd2 100644 --- a/t/plugin/node-status.t +++ b/t/plugin/node-status.t @@ -49,32 +49,7 @@ qr/"accepted":/ -=== TEST 2: get node status ---- config - location /t { - content_by_lua_block { - ngx.sleep(0.5) - local t = require("lib.test_admin").test - local code, body, body_org = t('/apisix/admin/node_status', - ngx.HTTP_GET - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body_org) - } - } ---- request -GET /t ---- response_body eval -qr/"accepted"/ ---- no_error_log -[error] - - - -=== TEST 3: test for unsupported method +=== TEST 2: test for unsupported method --- request PATCH /apisix/status --- error_code: 404