最新消息:

lua-resty-beanstalkd 模块教程

lua-resty-beanstalkd admin 3537浏览 0评论

本文涉及几个名词:

下面是读写的示例:

  1. 生产者(发布消息),主要那应用了两个协议:use/put,代码如下:
location /t {
    content_by_lua '
        local beanstalkd = require "resty.beanstalkd"

        local bean, err = beanstalkd:new()

        local ok, err = bean:connect("127.0.0.1", 11300)
        if not ok then
            ngx.say("1: failed to connect: ", err)
            return
        end

        local ok, err = bean:use("default")
        if not ok then
            ngx.say("2: failed to use tube: ", err)
            return
        end

        local id, err = bean:put("hello")
        if not id then
            ngx.say("3: failed to put: ", err)
            return
        end

        ngx.say("put: ", id)

        bean:close()
    ';
}
  1. 消费者(消费消息),主要用了三个协议:watch/reserve/delete,代码如下:
location /t {
    content_by_lua '
        local beanstalkd = require "resty.beanstalkd"

        local bean, err = beanstalkd:new()

        local ok, err = bean:connect("127.0.0.1", 11300)
        if not ok then
            ngx.say("1: failed to connect: ", err)
            return
        end

        local ok, err = bean:watch("default")
        if not ok then
            ngx.say("2: failed to watch: ", err)
            return
        end

        local id, data = bean:reserve()
        if not id then
            ngx.say("3: failed to reserve: ", id)
            return
        else
            ngx.say("1: reserve: ", id)
            local ok, err = bean:delete(id)
            if not ok then
                ngx.say("4: failed to delete: ", id)
                return
            end
            ngx.say("2: delete: ", id)
        end

        bean:close()
    ';
}

为了提升性能,可以调用 set_keepalive(提升50%以上性能),需要注意:

  1. 代码中不要调用 xx:close 主动关闭连接。
  2. 在对象使用后调用 xx:set_keepalive,而不是初始化 new/connect 时候调用此方法。

转载请注明:爱开源 » lua-resty-beanstalkd 模块教程

您必须 登录 才能发表评论!