在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):openresty/lua-resty-balancer开源软件地址(OpenSource Url):https://github.com/openresty/lua-resty-balancer开源编程语言(OpenSource Language):Lua 61.9%开源软件介绍(OpenSource Introduction):Namelua-resty-chash - A generic consistent hash implementation for OpenResty/LuaJIT lua-resty-roundrobin - A generic roundrobin implementation for OpenResty/LuaJIT Table of Contents
StatusThis library is still under early development and is still experimental. DescriptionThis Lua library can be used with Synopsis lua_package_path "/path/to/lua-resty-chash/lib/?.lua;;";
lua_package_cpath "/path/to/lua-resty-chash/?.so;;";
init_by_lua_block {
local resty_chash = require "resty.chash"
local resty_roundrobin = require "resty.roundrobin"
local server_list = {
["127.0.0.1:1985"] = 2,
["127.0.0.1:1986"] = 2,
["127.0.0.1:1987"] = 1,
}
-- XX: we can do the following steps to keep consistency with nginx chash
local str_null = string.char(0)
local servers, nodes = {}, {}
for serv, weight in pairs(server_list) do
-- XX: we can just use serv as id when we doesn't need keep consistency with nginx chash
local id = string.gsub(serv, ":", str_null)
servers[id] = serv
nodes[id] = weight
end
local chash_up = resty_chash:new(nodes)
package.loaded.my_chash_up = chash_up
package.loaded.my_servers = servers
local rr_up = resty_roundrobin:new(server_list)
package.loaded.my_rr_up = rr_up
}
upstream backend_chash {
server 0.0.0.1;
balancer_by_lua_block {
local b = require "ngx.balancer"
local chash_up = package.loaded.my_chash_up
local servers = package.loaded.my_servers
-- we can balancer by any key here
local id = chash_up:find(ngx.var.arg_key)
local server = servers[id]
assert(b.set_current_peer(server))
}
}
upstream backend_rr {
server 0.0.0.1;
balancer_by_lua_block {
local b = require "ngx.balancer"
local rr_up = package.loaded.my_rr_up
-- Note that Round Robin picks the first server randomly
local server = rr_up:find()
assert(b.set_current_peer(server))
}
}
server {
location /chash {
proxy_pass http://backend_chash;
}
location /roundrobin {
proxy_pass http://backend_rr;
}
} MethodsBoth newsyntax: Instantiates an object of this class. The The The local nodes = {
-- id => weight
server1 = 10,
server2 = 2,
}
local resty_chash = require "resty.chash"
local chash = resty_chash:new(nodes)
local id = chash:find("foo")
ngx.say(id) reinitsyntax: Reinit the chash obj with the new setsyntax: Set deletesyntax: Delete the incrsyntax: Increments weight for the decrsyntax: Decrease weight for the findsyntax: Find an id by the The second return value nextsyntax: If we have chance to retry when the first The new InstallationFirst you need to run # nginx.conf
http {
lua_package_path "/path/to/lua-resty-chash/lib/?.lua;;";
lua_package_cpath "/path/to/lua-resty-chash/?.so;;";
...
} Ensure that the system account running your Nginx ''worker'' proceses have
enough permission to read the PerformanceThere is a benchmark script I got the result when I run
AuthorDejiang Zhu (doujiang24) [email protected]. Copyright and LicenseThis module is licensed under the BSD license. Copyright (C) 2015-2016, by Yichun Zhang (agentzh) [email protected], CloudFlare Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. See Also
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论