1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
-- upstream_manager_v2.lua
-- 动态 upstream 管理模块(注册中心模式)
-- 从注册中心拉取配置,Nginx 只负责负载均衡
-- 所有节点管理通过后端注册中心完成
local _M = {}
local cjson = require "cjson"
local http = require "resty.http"
-- 共享内存
local upstream_nodes = ngx.shared.upstream_nodes
local healthcheck = ngx.shared.healthcheck
-- 常量
local DEFAULT_NS = "default"
local HEALTH_CHECK_PATH = "/api/status"
local HEALTH_CHECK_TIMEOUT = 2000 -- 2秒
local SYNC_INTERVAL = 10 -- 同步间隔(秒)
-- 注册中心配置(模块级变量,可通过 init 配置)
local REGISTRY_URL = ""
local REGISTRY_TOKEN = ""
local SYNC_VERSION_KEY = "sync:version"
-- 获取 namespace 的 key
local function get_ns_key(ns)
return "ns:" .. (ns or DEFAULT_NS)
end
-- 初始化(传入注册中心配置)
function _M.init(config)
config = config or {}
REGISTRY_URL = config.registry_url or os.getenv("KENGER_REGISTRY_URL") or "http://127.0.0.1:5000"
REGISTRY_TOKEN = config.registry_token or os.getenv("KENGER_REGISTRY_TOKEN") or ""
if config.sync_interval then
SYNC_INTERVAL = config.sync_interval
end
ngx.log(ngx.INFO, "upstream_manager_v2 initialized, registry: ", REGISTRY_URL, ", sync_interval: ", SYNC_INTERVAL, "s")
end
-- 获取指定 namespace 的所有节点
function _M.get_all_nodes(ns)
local ns_key = get_ns_key(ns)
local nodes_json = upstream_nodes:get(ns_key)
if not nodes_json then
return {}
end
local ok, nodes = pcall(cjson.decode, nodes_json)
if not ok then
ngx.log(ngx.ERR, "failed to decode nodes for ", ns_key, ": ", nodes)
return {}
end
-- 添加健康状态
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health = healthcheck:get(key)
node.healthy = (health ~= "unhealthy")
end
return nodes
end
-- 获取所有 namespace 及其节点
function _M.get_all_namespaces()
local keys = upstream_nodes:get_keys(100) -- 最多获取 100 个
local result = {}
for _, key in ipairs(keys) do
if key:sub(1, 3) == "ns:" then
local ns = key:sub(4)
result[ns] = _M.get_all_nodes(ns)
end
end
return result
end
-- 保存节点列表(内部使用)
local function save_nodes(ns, nodes)
local ns_key = get_ns_key(ns)
local ok, json = pcall(cjson.encode, nodes)
if not ok then
return false, "failed to encode nodes"
end
local ok, err = upstream_nodes:set(ns_key, json)
if not ok then
return false, err
end
return true
end
-- 加权随机选择算法
local function weighted_random_select(ns, nodes)
local ns_key = get_ns_key(ns)
local total_weight = 0
local available_nodes = {}
-- 只选择健康且权重大于0的节点
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health = healthcheck:get(key)
if health ~= "unhealthy" and node.weight > 0 then
total_weight = total_weight + node.weight
table.insert(available_nodes, node)
end
end
if #available_nodes == 0 then
return nil
end
if #available_nodes == 1 then
return available_nodes[1]
end
-- 加权随机选择
local rand = math.random(1, total_weight)
local cumulative = 0
for _, node in ipairs(available_nodes) do
cumulative = cumulative + node.weight
if rand <= cumulative then
return node
end
end
return available_nodes[1]
end
-- 获取后端地址(指定 namespace)
function _M.get_backend(ns)
local nodes = _M.get_all_nodes(ns)
if #nodes == 0 then
ngx.log(ngx.WARN, "no nodes available for ns=", ns or DEFAULT_NS)
return nil
end
local node = weighted_random_select(ns, nodes)
if not node then
ngx.log(ngx.WARN, "no healthy nodes available for ns=", ns or DEFAULT_NS)
return nil
end
return node.host .. ":" .. node.port
end
-- 健康检查(检查所有 namespace 的节点)
function _M.health_check()
local all_ns = _M.get_all_namespaces()
for ns, nodes in pairs(all_ns) do
local ns_key = get_ns_key(ns)
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health_path = node.health_path or HEALTH_CHECK_PATH
local sock = ngx.socket.tcp()
sock:settimeout(HEALTH_CHECK_TIMEOUT)
local ok, err = sock:connect(node.host, node.port)
if ok then
local req = "GET " .. health_path .. " HTTP/1.0\r\nHost: " .. node.host .. ":" .. node.port .. "\r\n\r\n"
sock:send(req)
local line, err = sock:receive("*l")
if line and line:match("200") then
healthcheck:set(key, "healthy", 30)
ngx.log(ngx.DEBUG, "health check passed: ", key)
else
healthcheck:set(key, "unhealthy", 30)
ngx.log(ngx.WARN, "health check failed: ", key, " - ", err or "non-200")
end
sock:close()
else
healthcheck:set(key, "unhealthy", 30)
ngx.log(ngx.WARN, "health check failed: ", key, " - ", err)
end
end
end
end
-- 获取健康状态(只读)
function _M.get_health_status(ns)
local nodes
if ns then
nodes = _M.get_all_nodes(ns)
else
-- 返回所有 namespace 的状态
return _M.get_all_namespaces()
end
local ns_key = get_ns_key(ns)
local result = {
namespace = ns or DEFAULT_NS,
total = #nodes,
healthy = 0,
unhealthy = 0,
nodes = {}
}
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health = healthcheck:get(key)
local is_healthy = (health ~= "unhealthy")
if is_healthy then
result.healthy = result.healthy + 1
else
result.unhealthy = result.unhealthy + 1
end
table.insert(result.nodes, {
address = node.host .. ":" .. node.port,
weight = node.weight,
healthy = is_healthy,
health_path = node.health_path
})
end
return result
end
-- ==================== 注册中心同步功能 ====================
-- 从注册中心拉取配置
function _M.sync_from_registry(ns)
if REGISTRY_URL == "" then
ngx.log(ngx.WARN, "REGISTRY_URL not configured, skip sync")
return false, "registry url not configured"
end
local httpc = http.new()
httpc:set_timeout(5000) -- 5秒超时
local url = REGISTRY_URL .. "/api/upstream/sync"
if ns then
url = url .. "?ns=" .. ns
end
local headers = {
["Content-Type"] = "application/json"
}
if REGISTRY_TOKEN ~= "" then
headers["Authorization"] = "Bearer " .. REGISTRY_TOKEN
end
local res, err = httpc:request_uri(url, {
method = "GET",
headers = headers
})
if not res then
ngx.log(ngx.ERR, "failed to sync from registry: ", err)
return false, err
end
if res.status ~= 200 then
ngx.log(ngx.ERR, "registry returned status: ", res.status)
return false, "status " .. res.status
end
local ok, data = pcall(cjson.decode, res.body)
if not ok then
ngx.log(ngx.ERR, "failed to decode registry response: ", data)
return false, "decode error"
end
if data.code ~= 0 then
ngx.log(ngx.ERR, "registry error: ", data.message)
return false, data.message
end
-- 检查版本号,避免重复更新
local current_version = upstream_nodes:get(SYNC_VERSION_KEY)
if current_version and data.version and tonumber(current_version) >= tonumber(data.version) then
ngx.log(ngx.DEBUG, "sync skipped, version unchanged: ", current_version)
return true
end
-- 更新节点数据
local sync_data = data.data
if not sync_data then
return true
end
for namespace, ns_data in pairs(sync_data) do
local nodes = {}
for _, node in ipairs(ns_data.nodes or {}) do
table.insert(nodes, {
host = node.host,
port = node.port,
weight = node.weight or 100,
health_path = node.health_path or ns_data.health_path or HEALTH_CHECK_PATH,
created_at = ngx.time(),
updated_at = ngx.time()
})
end
local ok, err = save_nodes(namespace, nodes)
if ok then
ngx.log(ngx.INFO, "synced namespace: ", namespace, " nodes: ", #nodes)
else
ngx.log(ngx.ERR, "failed to save nodes for ", namespace, ": ", err)
end
end
-- 更新版本号
if data.version then
upstream_nodes:set(SYNC_VERSION_KEY, data.version)
end
return true
end
-- 定时同步任务(在 init_worker_by_lua 中调用)
function _M.start_sync_timer()
if REGISTRY_URL == "" then
ngx.log(ngx.WARN, "REGISTRY_URL not configured, sync timer not started")
return
end
local handler
handler = function(premature)
if premature then
return
end
-- 执行同步
local ok, err = pcall(_M.sync_from_registry)
if not ok then
ngx.log(ngx.ERR, "sync error: ", err)
end
-- 重新设置定时器
local ok, err = ngx.timer.at(SYNC_INTERVAL, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create sync timer: ", err)
end
end
-- 启动首次同步(延迟1秒)
local ok, err = ngx.timer.at(1, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create initial sync timer: ", err)
else
ngx.log(ngx.INFO, "sync timer started, interval: ", SYNC_INTERVAL, "s, registry: ", REGISTRY_URL)
end
end
-- 获取当前配置信息(只读)
function _M.get_config()
return {
registry_url = REGISTRY_URL,
token_set = (REGISTRY_TOKEN ~= ""),
sync_interval = SYNC_INTERVAL
}
end
return _M
|