Nginx uses Lua scripts to access rabbitmq message queues

Source: Internet
Author: User

We found a nginx Lua Script: LUA-resty-rabbitmqstomp, which allows nginx to access the rabbitmq Message Queue through the Lua script. This script connects to the rabbitmq stomp adapter through the stomp protocol, for pub/sub messages

For more information about how to install and use rabbitmq-Stomp, see rabbitmq stomp adapter.

For how to install and use the nginx-Lua module, see lampgc Architecture Evolution, and then lnmlgc.

For more information about the stomp protocol, see here:

Stomp official English Protocol Version 1.1

Stomp protocol learning and practice-Version 1.1

Stomp official English Protocol version 1.2

Stomp protocol specification-version 1.2


Nginx Configuration:

        location /rabbitmq {            lua_code_cache off;            content_by_lua_file conf/rabbitmq.lua;        }

The rabbitmq. Lua source code is as follows. The script simply implements pub a message and then sub the message. set_timeout (60000) indicates that if the sub message does not have a message, wait for 60 s to time out.

local strlen =  string.lenlocal json = require "json"local rabbitmq = require "rabbitmqstomp"local mq, err = rabbitmq:new()if not mq then      returnendmq:set_timeout(60000)local ok, err = mq:connect {                    host = "127.0.0.1",                    port = 61613,                    username = "guest",                    password = "guest",                    vhost = "/"                }if not ok then    returnendngx.log(ngx.INFO, "Connect: " .. "OK")local msg = {key="value1", key2="value2"}local headers = {}headers["destination"] = "/queue/my_queue"headers["receipt"] = "msg#1"headers["app-id"] = "luaresty"headers["persistent"] = "true"headers["content-type"] = "application/json"local ok, err = mq:send(json.encode(msg), headers)if not ok then    returnendngx.log(ngx.INFO, "Published: " .. json.encode(msg))local headers = {}headers["destination"] = "/queue/my_queue"headers["persistent"] = "true"headers["id"] = "123"local ok, err = mq:subscribe(headers)if not ok then    returnendlocal data, err = mq:receive()if not data then    returnendngx.log(ngx.INFO, "Consumed: " .. data)ngx.header.content_type = "text/plain";ngx.say(data);local headers = {}headers["persistent"] = "true"headers["id"] = "123"local ok, err = mq:unsubscribe(headers)local ok, err = mq:set_keepalive(10000, 10000)if not ok then    returnend

Specifically, the push function can be implemented: the sender calls send to send messages to rabbitmq, and the receiver sets a timeout call to subscribe messages (similar to long polling)


Nginx uses Lua scripts to access rabbitmq message queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.