Sky3

nginx的ngx_http_upstream_check_module的解析

This project is maintained by wangfakang

对NGX_HTTP_UPSTREAM_CHECK_MODULE的分析:

内容:

一:配置说明

二:细节注意点

三:基本原理

四:使用技巧

五:关键问题

一.配置说明:

http {
    lua_package_path "/path/to/lua-resty-upstream-healthcheck/lib/?.lua;;";

    # sample upstream block:
    upstream foo.com {
        server 127.0.0.1:12354;
        server 127.0.0.1:12355;
        server 127.0.0.1:12356 backup;
    }   

    # the size depends on the number of servers in upstream {}: 
    lua_shared_dict healthcheck 1m; 

    lua_socket_log_errors off;

    init_worker_by_lua '
        local hc = require "resty.upstream.healthcheck"

        local ok, err = hc.spawn_checker{
            shm = "healthcheck",  -- defined by "lua_shared_dict"
            upstream = "foo.com", -- defined by "upstream"
            type = "http",

            -- if you put this Lua snippet in separate .lua file,
            -- then you should write this instead: http_req = "GET /status HTTP/1.0\r\nHost: foo.com\r\n\r\n",
            http_req = "GET /status HTTP/1.0\\r\\nHost: foo.com\\r\\n\\r\\n",
                    -- raw HTTP request for checking

            interval = 2000,  -- run the check cycle every 2 sec
            timeout = 1000,   -- 1 sec is the timeout for network operations
            fall = 3,  -- # of successive failures before turning a peer down
            rise = 2,  -- # of successive successes before turning a peer up
            valid_statuses = {200, 302},  -- a list valid HTTP status code
            concurrency = 10,  -- concurrency level for test requests
        }
        if not ok then
            ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
            return
        end

        -- Just call hc.spawn_checker() for more times here if you have
        -- more upstream groups to monitor. One call for one upstream group.
        -- They can all share the same shm zone without conflicts but they
        -- need a bigger shm zone for obvious reasons.
    ';

    server {
        ...

        # status page for all the peers:
        location = /status {
            access_log off;
            allow 127.0.0.1;
            deny all;

            default_type text/plain;
            content_by_lua '
                local hc = require "resty.upstream.healthcheck"
                ngx.say("Nginx Worker PID: ", ngx.worker.pid())
                ngx.print(hc.status_page())
            ';
        }
    }
}

上面的配置就启用了upstream的健康检查。

注意此模块是agentzh使用nxg_http_lua_upstream_module中的set_peer_down来实现的健康检查.和tengine实现的是不一样的.

二.细节注意点:

三.基本原理:

四.使用技巧:


local function get_lock(ctx)
    local dict = ctx.dict
    local key = "l:" .. ctx.upstream

    -- the lock is held for the whole interval to prevent multiple
    -- worker processes from sending the test request simultaneously.
    -- here we substract the lock expiration time by 1ms to prevent
    -- a race condition with the next timer event.     
   -- 利用其过期机制 相当于设置了一个自动过期的锁         
    local ok, err = dict:add(key, true, ctx.interval - 0.001)
    if not ok then
        if err == "exists" then
            return nil 
        end
        errlog("failed to add key \"", key, "\": ", err)
        return nil 
    end 
    return true
end

五.下面有几个关键问题:

1.如何保证每一个work子进程只执行一次相应的修改?      

2.如何知道哪一个peer进行了相应的修改?

有问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

感激

chunshengsterATgmail.com

关于作者

Linux\nginx\golang\c\c++爱好者

欢迎一起交流 一起学习#