获取Nginx upstream所有主机

雪域幽狐 2018-11-03 09:32 阅读:6751


在某些情况下,需要获取Nginx upstream所有主机,比如中间件上预加载缓存。通过Lua获取upstream所有节点
local upstream = require "ngx.upstream"
local cjson = require "cjson"

local function getPeers(name)
    local peers = upstream.get_primary_peers(name)
    local server = {}
    for i, peer in ipairs(peers) do
        table.insert(server, peer.name)
    end
    return server
end

local function getPeerString(name)
    return cjson.encode(getPeers(name))
end

local publicMethod ={
    getPeers = getPeers,
    getPeerString = getPeerString
}

return publicMethod

本文主要参考:https://my.oschina.net/362228416/blog/1926650

0条评论

登陆后可评论