!!!This module is under heavy development, do not use in production environment.!!!
A lua module for OpenResty, can dynamically update the upstreams from etcd.
- openresty-1.9.11.1 and higher
- balancer_by_lua
- ngx.worker.id()
- lua-resty-http
- cjson
etcdctl set /v1/testing/services/my_test_service/10.1.1.1:8080 '{"weight": 3, "slow_start": 30, "checkurl": "/health"}'
etcdctl set /v1/testing/services/my_test_service/10.1.1.2:8080 '{"weight": 4, "status": "down"}'
etcdctl set /v1/testing/services/my_test_service/10.1.1.3:8080 '{"weight": 5}'
The default weight is 1, if not set in ETCD, or json parse error and so on.
lua_socket_log_errors off; # recommend
lua_shared_dict lreu-upstream 1m; # for storeage of upstreams
init_worker_by_lua_block {
local syncer = require "lreu.syncer"
syncer.init({
etcd_host = "127.0.0.1",
etcd_port = 2379,
etcd_path = "/v1/testing/services/",
storage = ngx.shared.lreu-upstream
})
-- init the picker with the shared storage(read only)
local picker = require "lreu.picker"
picker.init(ngx.shared.lreu-upstream)
}
upstream test {
server 127.0.0.1:2222; # fake server
balancer_by_lua_block {
local balancer = require "ngx.balancer"
local u = require "lreu.picker"
local s, err = u.rr("my_test_service")
if not s then
ngx.log(ngx.ERR, err)
return ngx.exit(500)
end
local ok, err = balancer.set_current_peer(s.host, s.port)
if not ok then
ngx.log(ngx.ERR, "failed to set current peer: " .. err)
return ngx.exit(500)
end
}
}
- Etcd cluster support.
- Add more load-balance-alg.
Upstream peers weight support.- Upstream health check support.
I have not thought about it yet.