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