From eb03cf9f5e76e65945ec8ca3ee79db35644e3d5b Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Sun, 29 Nov 2020 21:18:32 +0800 Subject: [PATCH 1/2] allow user to specify APISIX id in config.yaml fix #2810 --- FAQ.md | 15 ++++++++++-- FAQ_CN.md | 11 +++++++++ apisix/core/id.lua | 13 ++++++++--- t/plugin/node-status.t | 52 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) diff --git a/FAQ.md b/FAQ.md index d6f770273b11..86eee61d277f 100644 --- a/FAQ.md +++ b/FAQ.md @@ -257,7 +257,7 @@ Steps: Now you can trace the info level log in logs/error.log. -## How to reload your own plugin +## How to reload your own plugin? The Apache APISIX plugin supports hot reloading. See the `Hot reload` section in [plugins](./doc/plugins.md) for how to do that. @@ -274,7 +274,7 @@ By default, APISIX only listens on port 9080 when handling HTTP requests. If you - 9080 - 9081 - 9082 - ``` + ``` Handling HTTPS requests is similar, modify the parameter of HTTPS port listen `ssl.listen_port` in `conf/config.yaml`, for example: @@ -288,3 +288,14 @@ By default, APISIX only listens on port 9080 when handling HTTP requests. If you ``` 2. Reload or restart APISIX + +## How to customize the APISIX instance id? + +By default, APISIX uses `uuid` as instance id, stored in `/conf/apisix.uid`, generated when the APISIX first start, and does not change. + +If you want to specify a meaningful id to bind APISIX instance with your internal system, you can configure it in `conf/config.yaml`, for example: + + ``` + apisix: + id: "your-meaningful-id" + ``` diff --git a/FAQ_CN.md b/FAQ_CN.md index dcd792cd7d7b..ea04a16b3b13 100644 --- a/FAQ_CN.md +++ b/FAQ_CN.md @@ -250,3 +250,14 @@ APISIX 主要使用 [etcd.watchdir](https://github.com/api7/lua-resty-etcd/blob/ * 如果监听目录有数据更新:etcd 将立刻返回订阅(毫秒级)到的新数据,APISIX 将它更新到内存缓存。 借助 etcd 增量通知毫秒级特性,APISIX 也就完成了毫秒级的配置同步。 + +## 如何自定义 APISIX 实例 id + +默认情况下,APISIX 使用 `uuid` 作为实例 id,存储在`/conf/apisix.uid`中,在 APISIX 第一次启动时生成,不会改变。 + +如果你想指定一个有意义的 id 来绑定 APISIX 实例与你的内部系统,你可以在`conf/config.yaml`中进行配置,示例: + + ``` + apisix: + id: "your-meaningful-id" + ``` diff --git a/apisix/core/id.lua b/apisix/core/id.lua index d6e728d2f0fc..34b8f8d03edf 100644 --- a/apisix/core/id.lua +++ b/apisix/core/id.lua @@ -14,6 +14,7 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- +local fetch_local_conf = require("apisix.core.config_local").local_conf local log = require("apisix.core.log") local uuid = require('resty.jit-uuid') local smatch = string.match @@ -65,9 +66,15 @@ function _M.init() return end - uuid.seed() - apisix_uid = uuid.generate_v4() - log.notice("not found apisix uid, generate a new one: ", apisix_uid) + --allow user to specify a meaningful id as apisix instance id + local local_conf = fetch_local_conf() + if local_conf and local_conf.apisix and local_conf.apisix.id then + apisix_uid = local_conf.apisix.id + else + uuid.seed() + apisix_uid = uuid.generate_v4() + log.notice("not found apisix uid, generate a new one: ", apisix_uid) + end local ok, err = write_file(uid_file_path, apisix_uid) if not ok then diff --git a/t/plugin/node-status.t b/t/plugin/node-status.t index d73dcc1c4fd2..ac7bbc01200f 100644 --- a/t/plugin/node-status.t +++ b/t/plugin/node-status.t @@ -53,3 +53,55 @@ qr/"accepted":/ --- request PATCH /apisix/status --- error_code: 404 + + + +=== TEST 3: test for use default uuid as apisix_uid +--- 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/status', ngx.HTTP_GET) + + if code >= 300 then + ngx.status = code + end + local json_decode = require("cjson").decode + local body_json = json_decode(body_org) + ngx.say(body_json.id) + } +} +--- request +GET /t +--- response_body_like eval +qr/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ +--- no_error_log +[error] + + + +=== TEST 4: test for allow user to specify a meaningful id as apisix_uid +--- yaml_config +apisix: + id: "user-set-apisix-instance-id-A" +#END +--- 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/status', ngx.HTTP_GET) + + if code >= 300 then + ngx.status = code + end + ngx.say(body_org) + } +} +--- request +GET /t +--- response_body eval +qr/"id":"user-set-apisix-instance-id-A"/ +--- no_error_log +[error] From 67a2c2d7f2d8349adba79b399f129d349dd35a8b Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Mon, 30 Nov 2020 00:24:35 +0800 Subject: [PATCH 2/2] solve code review --- FAQ.md | 4 ++-- FAQ_CN.md | 4 ++-- apisix/core/id.lua | 12 +++++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/FAQ.md b/FAQ.md index 86eee61d277f..98199b46000e 100644 --- a/FAQ.md +++ b/FAQ.md @@ -291,9 +291,9 @@ By default, APISIX only listens on port 9080 when handling HTTP requests. If you ## How to customize the APISIX instance id? -By default, APISIX uses `uuid` as instance id, stored in `/conf/apisix.uid`, generated when the APISIX first start, and does not change. +By default, APISIX will read the instance id from `conf/apisix.uid`. If it is not found, and no id is configured, APISIX will generate a `uuid` as the instance id. -If you want to specify a meaningful id to bind APISIX instance with your internal system, you can configure it in `conf/config.yaml`, for example: +If you want to specify a meaningful id to bind APISIX instance to your internal system, you can configure it in `conf/config.yaml`, for example: ``` apisix: diff --git a/FAQ_CN.md b/FAQ_CN.md index ea04a16b3b13..1842f9cd6157 100644 --- a/FAQ_CN.md +++ b/FAQ_CN.md @@ -253,9 +253,9 @@ APISIX 主要使用 [etcd.watchdir](https://github.com/api7/lua-resty-etcd/blob/ ## 如何自定义 APISIX 实例 id -默认情况下,APISIX 使用 `uuid` 作为实例 id,存储在`/conf/apisix.uid`中,在 APISIX 第一次启动时生成,不会改变。 +默认情况下,APISIX 会从 `conf/apisix.uid` 中读取实例 id。如果找不到,且没有配置 id,APISIX 会生成一个 `uuid` 作为实例 id。 -如果你想指定一个有意义的 id 来绑定 APISIX 实例与你的内部系统,你可以在`conf/config.yaml`中进行配置,示例: +如果你想指定一个有意义的 id 来绑定 APISIX 实例到你的内部系统,你可以在 `conf/config.yaml` 中进行配置,示例: ``` apisix: diff --git a/apisix/core/id.lua b/apisix/core/id.lua index 34b8f8d03edf..9793a42c11a5 100644 --- a/apisix/core/id.lua +++ b/apisix/core/id.lua @@ -15,10 +15,11 @@ -- limitations under the License. -- local fetch_local_conf = require("apisix.core.config_local").local_conf -local log = require("apisix.core.log") -local uuid = require('resty.jit-uuid') -local smatch = string.match -local open = io.open +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local uuid = require('resty.jit-uuid') +local smatch = string.match +local open = io.open local prefix = ngx.config.prefix() @@ -68,7 +69,8 @@ function _M.init() --allow user to specify a meaningful id as apisix instance id local local_conf = fetch_local_conf() - if local_conf and local_conf.apisix and local_conf.apisix.id then + local id = try_read_attr(local_conf, "apisix", "id") + if id then apisix_uid = local_conf.apisix.id else uuid.seed()