1.引入lua_package_path "/usr/local/openresty/lualib/resty/?.lua;/usr/local/openresty/lualib/resty/upstream/?.lua;;";
如
定义三个个变量
reset_timedout_connection on;
lua_shared_dict healthcheck 1m;
lua_socket_log_errors off;
}
之后就是编写健康检查的代码了
init_worker_by_lua_block {
local hc = require "resty.upstream.healthcheck"
local ok, err = hc.spawn_checker {
shm = "healthcheck",
upstream = "paperlessoffice",
type = "http",
http_req = "GET /paperless/generateData HTTP/1.0\r\nHost: paperlessoffice\r\n\r\n",
interval = 2000,
timeout = 5000,
fall = 3,
rise = 2,
valid_statuses = {200, 302},
concurrency = 1,
}
if not ok then
ngx.log(ngx.ERR, "=======> failed to spawn paperlessoffice health checker: ", err)
return
end
local ok, err = hc.spawn_checker {
shm = "healthcheck",
upstream = "PaperlessServer",
type = "http",
http_req = "POST /PaperlessServer/Tx5007 HTTP/1.0\r\nHost: PaperlessServer\r\nContent-Type: application/json\r\nContent-Length: 17\r\n\r\n{\"checkHealth\":1}",
interval = 2000,
timeout = 5000,
fall = 3,
rise = 2,
valid_statuses = {200, 302},
concurrency = 1,
}
if not ok then
ngx.log(ngx.ERR, "=======> failed to spawn PaperlessServer health checker: ", err)
return
end
local ok, err = hc.spawn_checker {
shm = "healthcheck",
upstream = "PaperlessProxy",
type = "http",
http_req = "GET /PaperlessProxy/checkNetWork?testMsg=testServer HTTP/1.0\r\nHost: PaperlessProxy\r\n\r\n",
interval = 2000,
timeout = 5000,
fall = 3,
rise = 2,
valid_statuses = {200, 302},
concurrency = 1,
}
if not ok then
ngx.log(ngx.ERR, "=======> failed to spawn PaperlessProxy health checker: ", err)
return
end
}