lua-resty-redis-cluster-fast

配置二進位制安裝包倉庫

首先我們需要配置二進位制安裝包的倉庫,按照以下命令進行配置。(命令中的 CLIENT_TOKEN 需要替換成訂閱郵件中的有效 Token)

針對 OpenResty 1.21.4.x

curl -o get-xray-priv-lib-repo.sh https://pkg2.openresty.com.cn/scripts/get-xray-priv-lib-repo.sh

sudo bash get-xray-priv-lib-repo.sh -l coro-nginx-module-1.21.4 -t CLIENT_TOKEN

sudo bash get-xray-priv-lib-repo.sh -l coro-hiredis-nginx-module-1.21.4 -t CLIENT_TOKEN

針對 OpenResty 1.25.3.x

curl -o get-xray-priv-lib-repo.sh https://pkg2.openresty.com.cn/scripts/get-xray-priv-lib-repo.sh

sudo bash get-xray-priv-lib-repo.sh -l coro-nginx-module-1.25.3 -t CLIENT_TOKEN

sudo bash get-xray-priv-lib-repo.sh -l coro-hiredis-nginx-module-1.25.3 -t CLIENT_TOKEN

除了上面配置的倉庫,我們還依賴 openresty 的開源倉庫,如果此前未配置過開源倉庫,可以參考這個頁面進行配 置。

安裝

針對 OpenResty-1.21.4.x

使用 yum 作為包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 等作業系統,執行以下命令進行私有庫的安裝。

sudo yum install -y lua-resty-redis-cluster-fast-1.21.4

使用 dnf 作為包管理器的 Fedora 等作業系統,執行以下命令進行私有庫的安裝。

sudo dnf install -y lua-resty-redis-cluster-fast-1.21.4

使用 apt 作為包管理器的 Ubuntu/Debian 等作業系統,執行以下命令進行私有庫的安裝。

sudo apt-get install -y lua-resty-redis-cluster-fast-1.21.4

針對 OpenResty 1.25.3.x

使用 yum 作為包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 等作業系統,執行以下命令進行私有庫的安裝。

sudo yum install -y lua-resty-redis-cluster-fast-1.25.3

使用 dnf 作為包管理器的 Fedora 等作業系統,執行以下命令進行私有庫的安裝。

sudo dnf install -y lua-resty-redis-cluster-fast-1.25.3

使用 apt 作為包管理器的 Ubuntu/Debian 等作業系統,執行以下命令進行私有庫的安裝。

sudo apt-get install -y lua-resty-redis-cluster-fast-1.25.3

升級 OpenResty

如果此前已經安裝過了 openresty,請執行以下更新命令:

sudo yum upgrade -y openresty

使用

Nginx 配置

在使用前需要在配置檔案 nginx.conf 中需要新增以下這些配置項,用以載入 lua-resty-redis-cluster-fast 相關的動態模組。

    ...

    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_module.so";
    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_hiredis_module.so";

    http {
        coro_preload /usr/local/openresty/openssl111/lib/libcrypto.so;
        coro_preload /usr/local/openresty/openssl111/lib/libssl.so;
        coro_preload /usr/local/openresty-plus/hiredis/lib/libhiredis.so;
        coro_preload /usr/local/openresty-plus/hiredis/lib/libhiredis_ssl.so;

        coro_stack_size  32768;
        ...
    }

Lua 介面

除了 connectset_keepalive 介面發生了變化,該庫的其他介面和開源的 lua-resty-redis 保持一致,以下是一個簡單的例子:

    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_module.so";
    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_hiredis_module.so";

    http {
        coro_preload /usr/local/openresty/openssl111/lib/libcrypto.so;
        coro_preload /usr/local/openresty/openssl111/lib/libssl.so;
        coro_preload /usr/local/openresty-plus/hiredis/lib/libhiredis.so;
        coro_preload /usr/local/openresty-plus/hiredis/lib/libhiredis_ssl.so;
        coro_stack_size  32768;

        server {
            location /test {
                # need to specify the resolver to resolve the hostname
                resolver 8.8.8.8;

                content_by_lua_block {
                    local redis = require "resty.redis.cluster.fast"
                    local red = redis:new()

                    red:set_timeouts(1000, 1000, 1000) -- 1 sec

                    -- connect via ip address directly
                    local ok, err = red:connect("127.0.0.1", 6379, {
                        cluster = "cluster",
                        other_nodes = {
                            { "127.0.0.1", 6380 },
                            { "127.0.0.1", 6381 },
                            { "127.0.0.1", 6382 },
                        },
                        no_slaves = true
                    })

                    if not ok then
                        ngx.say("failed to connect: ", err)
                        return
                    end

                    ok, err = red:set("dog", "an animal")
                    if not ok then
                        ngx.say("failed to set dog: ", err)
                        return
                    end

                    ngx.say("set result: ", ok)

                    local res, err = red:get("dog")
                    if not res then
                        ngx.say("failed to get dog: ", err)
                        return
                    end

                    if res == ngx.null then
                        ngx.say("dog not found.")
                        return
                    end

                    ngx.say("dog: ", res)

                    local ok, err = red:set_keepalive()
                    if not ok then
                        ngx.say("failed to set keepalive: ", err)
                        return
                    end

                    -- or just close the connection right away:
                    -- local ok, err = red:close()
                    -- if not ok then
                    --     ngx.say("failed to close: ", err)
                    --     return
                    -- end
                }
            }
        }
    }

connectset_keepalive 介面的說明如下:

connect

TODO:目前不支援 unix domain socket 和 SSL.

syntax: ok, err = red:connect(host, port, options_table)

連線到 Redis 入口節點所監聽的主機和埠。在實際連線到遠端後端之前,該方法將始終查詢連線池中由該方法的先前呼叫建立的匹配的空閒連線。

options_table 引數是一個 Lua table,包括這些引數:

  • cluster

    為正在使用的 Redis 叢集連線池指定一個自定義名稱,這是一個必填欄位。

  • pool_size

    指定連線池的大小,預設是 128。

  • other_nodes

    指定可以連線的其他 Redis 節點,當與入口節點的連線失敗時,它將嘗試連線到其他節點。

    other_nodes 引數是一個 Lua 表,用於儲存多個節點的配置資訊,例如:

    {
        { "127.0.0.1", 6379 },
        { "127.0.0.1", 6380 },
        { "127.0.0.1", 6381 },
    }
    
  • no_slaves

    是否禁止從 Redis slave 節點讀取資料,預設為 false

set_keepalive

syntax: ok, err = red:set_keepalive()

為了相容開源的 lua-resty-redis 庫的 set_keepalive(max_idle_timeout, pool_size) 方法,我們仍然允許傳入兩個引數,但我們會忽略傳入的引數。

將當前 Redis 連線放入到 cosocket 連線池中。

只在你要呼叫 close 方法的地方呼叫此方法。呼叫此方法將立即把當前的 Redis 物件變成 closed 狀態。除 connect() 外,對當前物件的任何後續操作都將返回 closed 錯誤。