function_M.init_worker() sync_backends() -- when worker starts, sync backends without delay local _, err = ngx.timer.every(BACKENDS_SYNC_INTERVAL, sync_backends) if err then ngx.log(ngx.ERR, string.format("error when setting up timer.every for sync_backends: %s", tostring(err))) end end
localfunctionsync_backends() local backends_data = configuration.get_backends_data()
local new_backends, err = cjson.decode(backends_data)
local balancers_to_keep = {} for _, new_backend inipairs(new_backends) do sync_backend(new_backend) balancers_to_keep[new_backend.name] = balancers[new_backend.name] end end
local configuration_data = ngx.shared.configuration_data local certificate_data = ngx.shared.certificate_data
local _M = {}
function_M.get_backends_data() return configuration_data:get("backends") end
function_M.call() if ngx.var.request_method ~= "POST"and ngx.var.request_method ~= "GET"then ngx.status = ngx.HTTP_BAD_REQUEST ngx.print("Only POST and GET requests are allowed!") return end
if ngx.var.request_uri == "/configuration/servers"then handle_servers() return end
if ngx.var.request_uri == "/configuration/general"then handle_general() return end
if ngx.var.uri == "/configuration/certs"then handle_certs() return end
if ngx.var.request_uri ~= "/configuration/backends"then ####只接受以上4类型URL ngx.status = ngx.HTTP_NOT_FOUND ngx.print("Not found!") return end
local backends = fetch_request_body()
local success, err = configuration_data:set("backends", backends)
end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
### fetch_request_body(),从此函数可以看出此函数是一个外部调用,可以得出原始的数据来源为外部触发的POST,可以查询Call()函数的引用位置 localfunctionfetch_request_body() ngx.req.read_body() ###防止ngx.req.get_body_data()返回nil,显示执行一下 local body = ngx.req.get_body_data()
ifnot body then local file_name = ngx.req.get_body_file() ###读取cache file
local file = io.open(file_name, "rb") ifnot file then returnnil end
####nginx.conf 查看nginx配置文件中显示调用call()函数的位置为当前server切url /configuration 符合函数要求,在查找外部调用的代码(基本可以定位为控制器的逻辑控制) server { listen unix:/tmp/nginx-status-server.sock; set $proxy_upstream_name"internal";
keepalive_timeout 0; gzip off;
access_log off;
location /configuration { # this should be equals to configuration_data dict client_max_body_size 10m; client_body_buffer_size 10m; proxy_buffering off;
function_M.balance() local balancer = get_balancer() ifnot balancer then return end
local peer = balancer:balance() ifnot peer then ngx.log(ngx.WARN, "no peer was returned, balancer: " .. balancer.name) return end
ngx_balancer.set_more_tries(1)
local ok, err = ngx_balancer.set_current_peer(peer) ####设置server信息 ifnot ok then ngx.log(ngx.ERR, string.format("error while setting current upstream peer %s: %s", peer, err)) end end
localfunctionget_balancer() if ngx.ctx.balancer then return ngx.ctx.balancer end
local backend_name = ngx.var.proxy_upstream_name ###获取当前request上下文中共享的变量proxy_upstream_name
local balancer = balancers[backend_name] ###获取balancers信息由sync_backend()函数定时轮询 ifnot balancer then return end
if route_to_alternative_balancer(balancer) then local alternative_backend_name = balancer.alternative_backends[1] ngx.var.proxy_alternative_upstream_name = alternative_backend_name
balancer = balancers[alternative_backend_name] end