在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):hamishforbes/lua-resty-upstream开源软件地址(OpenSource Url):https://github.com/hamishforbes/lua-resty-upstream开源编程语言(OpenSource Language):Perl 69.2%开源软件介绍(OpenSource Introduction):lua-resty-upstreamUpstream connection load balancing and failover module Table of ContentsStatusExperimental, API may change without warning. Requires ngx_lua > 0.9.5 OverviewCreate a lua shared dictionary. Define your upstream pools and hosts in init_by_lua, this will be saved into the shared dictionary. Use the Alternatively pass in a resty module (e.g lua-resty-redis or lua-resty-http) that implements Call Use
It allows for failover based on HTTP status codes as well as socket connection status. lua_shared_dict my_upstream_dict 1m;
init_by_lua '
upstream_socket = require("resty.upstream.socket")
upstream_api = require("resty.upstream.api")
upstream, configured = upstream_socket:new("my_upstream_dict")
if not upstream then
error(configured)
end
api = upstream_api:new(upstream)
if not configured then -- Only reconfigure on start, shared mem persists across a HUP
api:create_pool({id = "primary", timeout = 100})
api:set_priority("primary", 0)
api:set_method("primary", "round_robin")
api:add_host("primary", { id="a", host = "127.0.0.1", port = "80", weight = 10 })
api:add_host("primary", { id="b", host = "127.0.0.1", port = "81", weight = 10 })
api:create_pool({id = "dr"})
api:set_priority("dr", 10)
api:add_host("dr", { host = "127.0.0.1", port = "82", weight = 5 })
api:add_host("dr", { host = "127.0.0.1", port = "83", weight = 10 })
api:create_pool({id = "test", priority = 5})
api:add_host("primary", { id="c", host = "127.0.0.1", port = "82", weight = 10 })
api:add_host("primary", { id="d", host = "127.0.0.1", port = "83", weight = 10 })
end
';
init_worker_by_lua 'upstream:init_background_thread()';
server {
location / {
content_by_lua '
local sock, err = upstream:connect()
upstream:process_failed_hosts()
';
}
} upstream.socketnew
Returns a new upstream object using the provided dictionary name. When called in init_by_lua returns an additional variable if the dictionary already contains configuration. Takes an optional id parameter, this must be unique if multiple instances of upstream.socket are using the same dictionary. init_background_thread
Initialises the background thread, should be called in connect
Attempts to connect to a host in the defined pools in priority order using the selected load balancing method.
Returns a connected socket and a table containing the connected When passed a socket or resty module it will return the same object after successful connection or nil. Additionally, hash methods may take an optional resty_redis = require('resty.redis')
local redis = resty_redis.new()
local key = ngx.req.get_headers()["X-Forwarded-For"]
local redis, err = upstream:connect(redis, key)
if not redis then
ngx.log(ngx.ERR, err)
ngx.status = 500
return ngx.exit(ngx.status)
end
ngx.log(ngx.info, 'Connected to ' .. err.host.host .. ':' .. err.host.port)
local ok, err = redis:get('key') process_failed_hosts
Processes any failed or recovered hosts from the current request. Spawns an immediate callback via ngx.timer.at, does not block current request. get_pools
Returns a table containing the current pool and host configuration. e.g. {
primary = {
up = true,
method = 'round_robin',
timeout = 100,
priority = 0,
hosts = {
web01 = {
host = "127.0.0.1",
weight = 10,
port = "80",
lastfail = 0,
failcount = 0,
up = true,
healthcheck = true
}
web02 = {
host = "127.0.0.1",
weight = 10,
port = "80",
lastfail = 0,
failcount = 0,
up = true,
healthcheck = { interval = 30, path = '/check' }
}
}
},
secondary = {
up = true,
method = 'round_robin',
timeout = 2000,
priority = 10,
hosts = {
dr01 = {
host = "10.10.10.1",
weight = 10,
port = "80",
lastfail = 0,
failcount = 0,
up = true
}
}
},
} save_pools
Saves a table of pools to the shared dictionary, sort_pools
Generates a priority order in the shared dictionary based on the table of pools provided bind
Bind a function to be called when events occur. Returns local function host_down_handler(event)
ngx.log(ngx.ERR, "Host: ", event.host.host, ":", event.host.port, " in pool '", event.pool.id,'" is down!')
end
local ok, err = upstream:bind('host_down', host_down_handler) Event: host_upFired when a host changes status from down to up. Event data is a table containing the affected host and pool. Event: host_downFired when a host changes status from up to down. Event data is a table containing the affected host and pool. upstream.apiThese functions allow you to dynamically reconfigure upstream pools and hosts new
Returns a new api object using the provided upstream object. set_method
Sets the load balancing method for the specified pool. Currently randomised round robin and hashing methods are supported. create_pool
Creates a new pool from a table of options, Other valid options are
Hosts cannot be defined at this point. Note: IDs are converted to a string by this function Default pool values { method = 'round_robin', timeout = 2000, priority = 0 } set_priority
Priority must be a number, returns nil on error. add_host
Takes a pool ID and a table of options, Note: IDs are converted to a string by this function Defaults: { host = '', port = 80, weight = 0} remove_host
Takes a poolid and a hostid to remove from the pool down_host
Manually marks a host as down, this host will not be revived automatically. up_host
Manually restores a dead host to the pool upstream.httpFunctions for making http requests to upstream hosts. status_codesThis pool option is an array of status codes that indicate a failed request. Defaults to none. The {
['5xx'] = true, -- Matches 500, 503, 524
['400'] = true -- Matches only 400
} new
Returns a new http upstream object using the provided upstream object.
https_upstream = Upstream_HTTP:new(upstream_ssl, {
ssl = true,
ssl_verify = true,
sni_host = "foo.example.com"
}) init_background_thread
Initialises the background thread, should be called in Do not call the request
Takes the same parameters as lua-resty-http's request method. On a successful request returns the lua-resty-http object and a table containing the connected host and pool. If the request failed returns nil, the error and a suggested http status code local ok, err, status = upstream_http:request({
path = "/helloworld",
headers = {
["Host"] = "example.com",
}
})
if not ok then
ngx.status = status
ngx.say(err)
ngx.exit(status)
else
local host = err.host
local pool = err.pool
end set_keepalive
Passes the keepalive timeout / pool from the pool configuration through to the lua-resty-http get_reused_times
Passes through to the lua-resty-http close
Passes through to the lua-resty-http HTTP HealthchecksActive background healthchecks can be enabled by adding the A value of The With a few additional parameters
Failure for the background check is according to the same parameters as for a frontend request, unless overriden explicitly. -- Custom check parameters
api:add_host("primary", {
host = 123.123.123.123,
port = 80,
healthcheck = {
interval = 30, -- check every 30s
timeout = (5*1000), -- 5s connect timeout
read_timeout = (15*1000), -- 15s connect timeout
status_codes = {["5xx"] = true, ["403"] = true}, -- 5xx and 403 responses are a fail
-- resty-http params
path = "/check",
headers = {
["Host"] = "domain.com",
["Accept-Encoding"] = "gzip"
}
}
})
-- Default check parameters
api:add_host("primary", {host = 123.123.123.123, port = 80, healthcheck = true})
TODO
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论