当前位置:网站首页>Openresty Lua resty balancer dynamic load balancing
Openresty Lua resty balancer dynamic load balancing
2022-07-24 02:45:00 【o_ Guatian Lixia_ o】
openresty lua-resty-balancer Dynamic load balancing
lua-resty-balancer:https://github.com/openresty/lua-resty-balancer.git
ngx.balancer:https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/balancer.md
lua-resty-balancer explain
This library is still under early development and is still experimental.
* lua-resty-balancer It is still in the early development stage , And in the experimental stage
chash、roundRoubin
Both resty.chash and resty.roundrobin have the same apis
* resty.chash、resty.roubdrobin Have the same api
# resty.chash、resty.roubdrobin Module reference
local resty_chash = require "resty.chash"
local resty_roundrobin = require "resty.roundrobin"
new: Create an object instance
Grammar format :obj, err = class.new(nodes)
Instantiates an object of this class. The class value is returned
by the call require "resty.chash".
* Create an object instance
The id should be table.concat({host, string.char(0), port}) like the
nginx chash does, when we need to keep consistency with nginx chash.
* When needed with ngxin chash When consistent ,id To work with nginx chash bring into correspondence with
* id ==> table.concat({host, string.char(0), port})
The id can be any string value when we do not need to keep consistency
with nginx chash. The weight should be a non negative integer
* If you don't want to be with ngin chash bring into correspondence with ,id It could be any string
* The weight should be a non negative number
reinit: Reinitialize the object
Grammar format :obj:reinit(nodes)
Reinit the chash obj with the new nodes
* Reinitialize with a new node chash object
set: Set up id The weight of
Grammar format :obj:set(id, weight)
Set weight of the id
* Set up id The weight of
delete: Delete id node
Grammar format :obj:delete(id)
Delete the id
* Delete id node
incr: increase id Weight of nodes
Grammar format :obj:incr(id, weight?)
Increments weight for the id by the step value weight(default to 1)
* increase id Weight of nodes ,weight If not set , The default is 1
decr: Reduce id Weight of nodes
Grammar format :obj:decr(id, weight?)
Decrease weight for the id by the step value weight(default to 1).
* Reduce id Weight of nodes ,weight If not set , The default is 1
find: Use key lookup id node
Grammar format :id, index = obj:find(key)
Find an id by the key, same key always return the same id in the same obj.
* Use key lookup id,
* In the same object , same key Always return the same id
The second return value index is the index in the chash circle of
the hash value of the key
* index: Nodes in consistency hash The index position in the ring
next: Get the next node of the current node
Grammar format :id, new_index = obj:next(old_index)
If we have chance to retry when the first id(server) doesn't
work well, then we can use obj:next to get the next id.
* If you can try again , The first node is not working , You can get the next node of the current node
The new id may be the same as the old one
* New nodes id Possible and long nodes id identical
Example
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
}
# Uniformity hash
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))
}
}
# polling
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;
}
}
ngx.balancer explain
边栏推荐
- [diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu
- Vscade connects to the server. The password is correct, but it has been unable to connect
- go errors
- 自定义log注解,请求抓取
- [brother hero July training] day 23: dictionary tree
- (6) Decorator extension [email protected] Principle of use
- C language exercises
- Doodle Icons - 一组免费商用的涂鸦风格图标库,可爱轻快又独特
- Unity timeline tutorial
- Doodle icons - a free commercial graffiti style icon library, cute, light and unique
猜你喜欢
![[datasets] - downloading some datasets of flyingthings3d optical flow](/img/00/5d87b378ebab49e9dc400d8e3634f3.png)
[datasets] - downloading some datasets of flyingthings3d optical flow

Skywalking distributed system application performance monitoring tool - upper

Soft test --- fundamentals of programming language (Part 1)

"Why should we do IVX?"—— Interview with IVX CEO Meng Zhiping to understand IVX corporate culture

Rylstim Screen Recorder

Wonderful! The description of meituan Octo distributed service management system is too clear

Essential skills for programmers -- breakpoint debugging (idea version)

Attack and defense world web practice area (view_source, get_post, robots)

Diversity of SIGIR '22 recommendation system papers
![[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import](/img/4b/c24ac8a11d15285a49d7b3b9bde4e3.png)
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part2): Atlas data preparation and import
随机推荐
Some consulting questions and answers raised by friends who lack programming foundation and want to change careers in ABAP development post
如何获取步态能量图gei
SSM的技术论坛含前后台
【补题日记】[2022杭电暑期多校1]C-Backpack
Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power
[datasets] - downloading some datasets of flyingthings3d optical flow
理解加载class到JVM的时机
Jina AI and datawhale jointly launched a learning project!
攻防世界WEB练习区(weak_auth、simple_php、xff_referer)
No coding is required, and the "asynchronous request reply" mode is automatically implemented
Fasterrcnn sample code test 1: make anchor_ generator = None
Attack and defense world web practice area (webshell, command_execution, simple_js)
Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire
Share an API Gateway project based on ABP and yarp
Zone d'entraînement Web d'attaque et de défense (View source, get Post, robots)
【补题日记】[2022牛客暑期多校1]D-Mocha and Railgun
Mysql database, grouping function
The solution of using non root user management in secure stand-alone database under general machine environment
SSM family financial management personal financial management system accounting system
Pyg uses messagepassing to build GCN to realize node classification