全域性自定義表格
概述
Global Custom Table(全域性自定義表格)是 Edge Admin 提供的強大功能,允許管理員建立和管理自定義資料表,用於儲存和組織特定的業務資料。該功能提供了靈活的資料結構定義和管理能力,支援多種資料型別和查詢方式。
注意
此功能下發的資料會佔用 Edge Node 中 LMDB 資料庫的空間,該空間預設大小為 8GB,所有配置共用,因此不建議儲存大量資料。建議合理規劃資料量,避免影響系統效能。建立全域性自定義表格
步驟 1:訪問管理介面
- 登入 Edge Admin 管理後臺
- 導航至 全域性配置 > 全域性自定義表格
- 點選 新建自定義表格 按鈕
步驟 2:配置表格基本資訊
基本配置
- 表名稱:輸入表的名稱(僅支援英文字母、數字和下劃線,用於系統內部標識)
- 描述:新增表的用途描述,便於後續管理和維護
- 是否傳送事件:當變更表格資料時,是否傳送對應的事件通知到相關係統,通常需與 Edge Admin 的 Lua 外掛配合使用。
資料欄位配置
資料欄位是儲存業務資料的核心結構,每個欄位包含以下屬性:
屬性 | 說明 | 備註 |
---|---|---|
欄位名稱 | 資料欄位的名稱 | 僅支援英文字母、數字和下劃線 |
欄位型別 | 資料欄位的型別 | 詳見下方支援的欄位型別 |
欄位描述 | 欄位的用途描述 | 可選,便於理解欄位含義 |
預設值 | 欄位的預設值 | 新增資料時的預設填充值 |
查詢鍵 | 用於查詢該行資料的主鍵 | |
不為 NULL | 是否允許欄位值為空 | 必填欄位請勾選此選項 |
同步 | 是否需要同步到邊緣節點 | 影響資料在 Edge Node 的可用性 |
唯一 | 欄位值是否必須唯一 | 用於確保資料完整性 |
支援的欄位型別
型別 | 說明 | 使用場景 |
---|---|---|
文字 | 字串型別 | 使用者名稱、標題、描述等 |
數字 | 數值型別 | 年齡、計數、價格等 |
布林值 | 真/假值 | 開關狀態、是否啟用等 |
IP 地址 | IPv4/IPv6 地址 | 網路配置、訪問控制等 |
域名 | 域名格式 | 網站地址、服務端點等 |
陣列 | 字串陣列 | 標籤列表、分類等 |
IP 地址陣列 | IP 地址列表 | 白名單、黑名單等 |
域名陣列 | 域名列表 | 多域名配置等 |
使用示例
以下透過一個完整的使用者資訊管理示例,演示全域性自定義表格的使用方法。
1. 建立使用者資訊表格
建立一個名為 example_users
的使用者資訊表,包含以下欄位:
username
:使用者名稱(文字型別,查詢鍵,唯一)age
:年齡(數字型別)enable
:是否啟用(布林值型別)ip
:IP 地址(IP 地址型別)domain
:域名(域名型別)array
:標籤陣列(陣列型別)ip_array
:IP 地址列表(IP 地址陣列型別)domain_array
:域名列表(域名陣列型別)
2. 新增測試資料
新增一條測試使用者資料:
- 使用者名稱:
oredge
- 年齡:
10
- 啟用狀態:
true
- IP 地址:
1.2.3.4
- 域名:
openresty.com
- 標籤:
["openresty", "edge"]
- IP 列表:
["1.2.3.4", "2.3.4.5"]
- 域名列表:
["openresty.com", "doc.openresty.com"]
3. 建立 Lua 查詢模組
建立一個名為 check_user
的 Lua 模組,用於查詢使用者資訊:
local ct = require("custom_table")
local cjson_encode = require("cjson.safe").encode
local get_uri_args = ngx.req.get_uri_args
local ngx_say = ngx.say
local _M = {}
function _M.go()
-- 獲取 URI 引數中的使用者名稱
local uri_args = get_uri_args()
local username = uri_args.username
if not username then
ngx_say("Error: username parameter is required")
return
end
-- 查詢使用者資訊
local res, err = ct.get("example_users", { username = username })
if not res then
ngx_say("Error: ", err or "User Not Found")
return
end
-- 返回使用者資訊
ngx_say("Hello ", res.username, ", your info: ", cjson_encode(res))
end
return _M
在 Lua 模組中,透過使用 custom_table
模組來獲取自定義資料:
local ct = require("custom_table")
local res, err = ct.get("table_name", { field = "value" })
4. 在應用中引用 Lua 模組
在應用的 Edge Language 規則中呼叫 Lua 模組:
true =>
foreign-call(module: "check_user", func: "go");
5. 測試驗證
配置釋出完成後,進行功能測試:
測試 1:查詢存在的使用者
$ curl --resolve oredge-example.com:80:127.0.0.1 'http://oredge-example.com/?username=oredge'
Hello oredge, your info: {"enable":true,"domain_array":["openresty.com","doc.openresty.com"],"ip_array":["1.2.3.4","2.3.4.5"],"username":"oredge","domain":"openresty.com","ip":"1.2.3.4","age":10,"array":["openresty","edge"]}
測試 2:查詢不存在的使用者
$ curl --resolve oredge-example.com:80:127.0.0.1 'http://oredge-example.com/?username=notfound'
Error: User Not Found