lua-resty-upstream

OpenResty/LuaJIT 的纯 Lua Nginx 上游管理

$ opm get toruneko/lua-resty-upstream

名称

lua-resty-upstream - 纯 Lua 动态 Nginx 上游模块

状态

此库被认为已准备好投入生产。

构建状态: [!Travis](https://travis-ci.org/toruneko/lua-resty-upstream)

描述

此库需要使用 ngx_lua 模块 构建的 Nginx 和 LuaJIT 2.0

依赖项

概述

        # nginx.conf:
    
        lua_package_path "/path/to/lua-resty-upstream/lib/?.lua;;";
        lua_shared_dict upstream    1m;
        lua_shared_dict monitor 1m;
        
        server {
            location = /t {
                content_by_lua_block {
                    local upstream = require "resty.upstream"
                    upstream.init({
                        cache = "upstream",
                        cache_size = 100
                    })
                    -- update foo.com upstream
                    local ok = upstream.update_upstream("foo.com", {
                        version = 1,
                        hosts = {
                            {
                                name = "127.0.0.1:8080", 
                                host = "127.0.0.1", 
                                port = 8080, 
                                weight = 100, 
                                max_fails = 3, 
                                fail_timeout = 10, 
                                default_down = false
                            }
                        }
                    })
                    if not ok then
                        return
                    end
    
                    local monitor = require "resty.upstream.monitor"
                    local ok, err = monitor.spawn_checker({
                        shm = "monitor",
                        upstream = "foo.com",
                        type = "http",
                        http_req = "HEAD /status HTTP/1.0\r\nHost: foo.com\r\n\r\n",
                        -- if required "lua-resty-http"
                        -- http_req = {
                        --     method = "HEAD",
                        --     path = "/ok.htm"
                        --     headers = {
                        --         Host = "foo.com"
                        --     }
                        -- }
                        interval = 2000,
                        timeout = 1000,
                        fall = 3,
                        rise = 2,
                        valid_statuses = {200, 302},
                        concurrency = 10,
                    })
                    if not ok then
                        upstream.delete_upstream("foo.com")
                        ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
                        return
                    end
                }
            }
        }
        

方法

要加载此库,

  1. 您需要在 ngx_lua 的 lua_package_path 指令中指定此库的路径。例如,lua_package_path "/path/to/lua-resty-upstream/lib/?.lua;;";

  2. 您可以使用 require 将库加载到本地 Lua 变量中

        local upstream = require "resty.upstream"

init

语法: upstream.init(config)

阶段: init_by_lua

使用配置初始化上游管理

    lua_shared_dict upstream  1m;


    local config = {
        cache = "upstream",
        cache_size = 1000
    }

cache_size 表示上游的最大数量,当存储的上游数量超过 cache_size 时,LRU 将生效。

update_upstream

语法: ok = upstream.update_upstream(u, data)

使用名称 udata 中更新上游或创建新的上游。成功时返回 true。

    local ok = upstream.update_upstream("foo.com", {
        version = 1,
        hosts = {
            {
                name = "127.0.0.1:8080", 
                host = "127.0.0.1", 
                port = 8080,            -- default value
                weight = 100,           -- default value
                max_fails = 3,          -- default value
                fail_timeout = 10,      -- default value, 10 second
                default_down = false    -- default value
            }
        }
    })
    if not ok then
        return
    end

子模块 lua-resty-upstream-balancer 的权重、max_fails、fail_timeout 选项。

子模块 lua-resty-upstream-monitor 的 default_down 选项。

delete_upstream

语法: upstream:delete_upstream(u)

删除上游名称为 u 的上游

作者

Jianhao Dai (toruneko) <[email protected]>

版权和许可

此模块在 MIT 许可下获得许可。

版权所有 (C) 2018,由 Jianhao Dai (toruneko) <[email protected]>

保留所有权利。

只要满足以下条件,就可以重新分发和使用源代码和二进制形式的软件,无论是否修改。

  • 源代码的再分发必须保留上述版权声明、本条件列表和以下免责声明。

  • 二进制形式的再分发必须在随分发提供的文档和/或其他材料中复制上述版权声明、本条件列表和以下免责声明。

本软件由版权持有人和贡献者“按现状”提供,任何明示或暗示的担保,包括但不限于适销性和特定用途适用性的暗示担保,均予以放弃。在任何情况下,版权持有人或贡献者均不对因使用本软件而产生的任何直接、间接、意外、特殊、示范性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润的损失;或业务中断)负责,无论损害是基于合同、严格责任或侵权(包括疏忽或其他)的任何理论,即使已被告知可能发生这种损害。

另请参阅

  • ngx_lua 模块: https://github.com/openresty/lua-nginx-module

作者

toruneko

许可证

mit

依赖项

版本