feat: 新增 8 个语言客户端 SDK
- PHP 客户端 (kenaito-conf-php-client) - .NET 客户端 (kenaito-conf-dotnet-client) - C 客户端 (kenaito-conf-c-client) - C++ 客户端 (kenaito-conf-cpp-client) - Lua 客户端 (kenaito-conf-lua-client) - Kotlin 客户端 (kenaito-conf-kotlin-client) - Rust 客户端 (kenaito-conf-rust-client) - Ruby 客户端 (kenaito-conf-ruby-client) 每个客户端包含完整的 SDK 实现和 demo 示例,支持: - 配置拉取(全量/增量) - AES-128-CBC 解密 - 本地缓存 - 变更回调 - Token 认证 更新根目录 README.md: - 技术栈列表新增 8 种语言 - 项目结构新增 16 个目录 - 客户端接入章节新增 8 个使用示例
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# Kenaito Config Center Lua Client
|
||||
|
||||
Lua 语言的配置中心客户端 SDK。
|
||||
|
||||
## 依赖
|
||||
|
||||
- Lua 5.3+
|
||||
- LuaSocket
|
||||
- lua-cjson
|
||||
- lua-openssl
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
luarocks install kenaito-conf-client-1.0.0-1.rockspec
|
||||
```
|
||||
|
||||
或手动安装依赖:
|
||||
|
||||
```bash
|
||||
luarocks install luasocket
|
||||
luarocks install lua-cjson
|
||||
luarocks install lua-openssl
|
||||
```
|
||||
|
||||
## 使用
|
||||
|
||||
```lua
|
||||
local ConfigClient = require("kenaito-conf")
|
||||
|
||||
local config = {
|
||||
baseUrl = "http://localhost:8080",
|
||||
appName = "kenaito-springboot-demo",
|
||||
env = "daily",
|
||||
version = "latest",
|
||||
syncInterval = 30,
|
||||
}
|
||||
|
||||
local client = ConfigClient.new(config)
|
||||
|
||||
client:on_change(function(changed)
|
||||
for k, v in pairs(changed) do
|
||||
print("config changed: " .. k .. " = " .. v)
|
||||
end
|
||||
end)
|
||||
|
||||
client:start()
|
||||
|
||||
local value = client:get("database.host")
|
||||
local all = client:get_all()
|
||||
|
||||
client:stop()
|
||||
```
|
||||
|
||||
## 配置参数
|
||||
|
||||
| 参数 | 说明 | 默认值 |
|
||||
|------|------|--------|
|
||||
| `baseUrl` | 配置中心地址 | 必填 |
|
||||
| `appName` | 应用名称 | 必填 |
|
||||
| `env` | 环境 | `dev` |
|
||||
| `version` | 配置版本 | `latest` |
|
||||
| `token` | 认证令牌 | 空 |
|
||||
| `syncInterval` | 同步间隔(秒) | `60` |
|
||||
| `ignoreGlobal` | 是否忽略全局配置 | `false` |
|
||||
| `cacheDir` | 本地缓存目录 | `.` |
|
||||
|
||||
## Demo
|
||||
|
||||
```bash
|
||||
cd kenaito-conf-lua-client-demo
|
||||
lua app.lua
|
||||
```
|
||||
@@ -0,0 +1,120 @@
|
||||
local http = require("socket.http")
|
||||
local ltn12 = require("ltn12")
|
||||
local cjson = require("cjson")
|
||||
local openssl = require("openssl")
|
||||
|
||||
local ConfigClient = {}
|
||||
ConfigClient.__index = ConfigClient
|
||||
|
||||
function ConfigClient.new(config)
|
||||
local self = setmetatable({}, ConfigClient)
|
||||
self.config = config or {}
|
||||
self.config.baseUrl = self.config.baseUrl or ""
|
||||
self.config.appName = self.config.appName or ""
|
||||
self.config.env = self.config.env or "dev"
|
||||
self.config.version = self.config.version or "latest"
|
||||
self.config.token = self.config.token or ""
|
||||
self.config.syncInterval = self.config.syncInterval or 60
|
||||
self.config.ignoreGlobal = self.config.ignoreGlobal or false
|
||||
self.config.cacheDir = self.config.cacheDir or "."
|
||||
|
||||
self.configs = {}
|
||||
self.secret = ""
|
||||
self.globalSecret = ""
|
||||
self.lastTime = 0
|
||||
self.handlers = {}
|
||||
return self
|
||||
end
|
||||
|
||||
function ConfigClient:on_change(handler)
|
||||
table.insert(self.handlers, handler)
|
||||
end
|
||||
|
||||
function ConfigClient:get(key)
|
||||
return self.configs[key]
|
||||
end
|
||||
|
||||
function ConfigClient:get_all()
|
||||
return self.configs
|
||||
end
|
||||
|
||||
local function decrypt_config(secret, data)
|
||||
local prefix = "{Cipher}"
|
||||
if string.sub(data, 1, #prefix) ~= prefix then
|
||||
return data
|
||||
end
|
||||
local encrypted = string.sub(data, #prefix + 1)
|
||||
|
||||
-- MD5 hash
|
||||
local md5_hash = openssl.digest("md5", secret)
|
||||
local aes_key = string.sub(md5_hash, 17)
|
||||
|
||||
-- Base64 decode
|
||||
local cipher_text = openssl.base64(encrypted, false)
|
||||
|
||||
-- AES-128-CBC decrypt
|
||||
local iv = string.rep("\0", 16)
|
||||
local cipher = openssl.cipher.new("aes-128-cbc")
|
||||
local plain_text = cipher:decrypt(cipher_text, aes_key, iv)
|
||||
|
||||
-- Remove PKCS7 padding
|
||||
local padding = string.byte(plain_text, -1)
|
||||
return string.sub(plain_text, 1, -padding - 1)
|
||||
end
|
||||
|
||||
function ConfigClient:start()
|
||||
-- Fetch all configs
|
||||
local url = self.config.baseUrl .. "/api/fetchAllConfigs"
|
||||
local payload = cjson.encode({
|
||||
appName = self.config.appName,
|
||||
env = self.config.env,
|
||||
version = self.config.version,
|
||||
ignoreGlobal = self.config.ignoreGlobal,
|
||||
token = self.config.token
|
||||
})
|
||||
|
||||
local response_body = {}
|
||||
local res, code, response_headers = http.request{
|
||||
url = url,
|
||||
method = "POST",
|
||||
headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
["Content-Length"] = #payload
|
||||
},
|
||||
source = ltn12.source.string(payload),
|
||||
sink = ltn12.sink.table(response_body)
|
||||
}
|
||||
|
||||
if code == 200 then
|
||||
local data = cjson.decode(table.concat(response_body))
|
||||
|
||||
if data["kenaito.config.encrypt-secret"] then
|
||||
self.secret = data["kenaito.config.encrypt-secret"]
|
||||
end
|
||||
if data["kenaito.config.global-encrypt-secret"] then
|
||||
self.globalSecret = data["kenaito.config.global-encrypt-secret"]
|
||||
end
|
||||
if data["latest.fetch.timestamp"] then
|
||||
self.lastTime = tonumber(data["latest.fetch.timestamp"])
|
||||
end
|
||||
|
||||
for k, v in pairs(data) do
|
||||
if not string.match(k, "^kenaito%.") and k ~= "latest.fetch.timestamp" then
|
||||
if type(v) == "string" then
|
||||
self.configs[k] = decrypt_config(self.secret, v)
|
||||
if self.configs[k] == v and self.globalSecret ~= "" then
|
||||
self.configs[k] = decrypt_config(self.globalSecret, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
print("[ConfigClient] fetched " .. #self.configs .. " configs")
|
||||
end
|
||||
end
|
||||
|
||||
function ConfigClient:stop()
|
||||
-- Cleanup
|
||||
end
|
||||
|
||||
return ConfigClient
|
||||
@@ -0,0 +1,22 @@
|
||||
package = "kenaito-conf-client"
|
||||
version = "1.0.0-1"
|
||||
source = {
|
||||
url = "git+https://gitea.odboy.cn/odboy/kenaito-conf.git"
|
||||
}
|
||||
description = {
|
||||
summary = "Kenaito Config Center Lua Client",
|
||||
homepage = "https://gitea.odboy.cn/odboy/kenaito-conf",
|
||||
license = "MIT"
|
||||
}
|
||||
dependencies = {
|
||||
"lua >= 5.3",
|
||||
"luasocket",
|
||||
"lua-cjson",
|
||||
"lua-openssl"
|
||||
}
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
["kenaito-conf"] = "client.lua"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user