redis rsmq worker无法从redis队列中获取消息

时间:2018-04-25 14:16:39

标签: node.js docker redis

我正在尝试使用redis + node + docker编写rsmq worker集成。

我创建了三个独立的容器

  1. producer
  2. redis
  3. 消费者
  4. 所有三个容器都在同一台机器上工作(localhost)。当我要从生产者容器向redis队列发送消息时,它成功运行,但消费者无法自动从同一队列中提取消息。

    但是,我已经完成了所有三个容器并验证了网络配置。这是ping。我使用rsmq-worker作为redis使用者。

    令我惊讶的是,它适用于以下两种情况

    1. 我将我的使用者移动到与REDIS容器相同的容器中并且它会提取消息。但是,正如我所提到的,当消费者处于与REDIS不同的容器中时,它不起作用。

    2. 此外,当我有三个单独的容器并且我使用rsmq-cli并尝试从消费者容器接收-h redis-Ip -q simple-test时,它会获取消息

    3. 我认为rsmq-worker中存在容器问题。以前有人遇到过这个吗?有什么建议吗?

      我附上代码也是为了参考。

        

      Consumer.js

      var express = require('express');
      var app = express();
      var bodyParser = require('body-parser');
      var RedisSMQ = require("rsmq");
      var rsmq = new RedisSMQ({ host: "18.232.206.185", port: 6379, ns: "rsmq" });
      var RSMQWorker = require("rsmq-worker");
      var rsmqTestWorker = new RSMQWorker("simple-test", { timeout: '0' });
      var router = express.Router();
      
      var allowCrossDomain = function (req, res, next) {
      
          res.header('Access-Control-Allow-Origin', '*');
          res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
          res.header('Access-Control-Allow-Headers', 'Content-Type,userId,x-onehop-token,businessId,corporateId');
          if (req.method == 'OPTIONS') {
              res.status(200).end();
          } else {
              console.log("------------------Req URL-----------------");
              console.log("Req.URL :: ", req.path);
              console.log("------------------Req method-----------------");
              console.log("Req.params :: ", req.method);
              console.log("--------------------------------------------");
              next();
          }
          // next();
      };
      app.use(allowCrossDomain);
      app.use(bodyParser.json({
          limit: '50mb'
      }));
      //app.use(bodyParser.json());
      app.use(bodyParser.urlencoded({
          limit: '50mb',
          extended: true,
          parameterLimit: 100000000
      }));
      
      rsmqTestWorker.on("message", function(msg, next, id) {
          // process your message
          console.log("rsmqTestWorker process Message id : " + id);
          console.log(msg);
          next();
      
      });
      
      // optional error listeners
      rsmqTestWorker.on('error', function(err, msg) {
          console.log("rsmqTestWorker ERROR", err, msg.id);
      });
      rsmqTestWorker.on('exceeded', function(msg) {
          console.log("rsmqTestWorker EXCEEDED", msg.id);
      });
      rsmqTestWorker.on('timeout', function(msg) {
          console.log("rsmqTestWorker TIMEOUT", msg.id, msg.rc);
      });
      
      var server = app.listen(5000, function () {
          console.log('Server listening on port 5000');
          rsmqTestWorker.start();
      });
      
        

      Producer.js

      var express = require('express');
      var app = express();
      var bodyParser = require('body-parser');
      var RedisSMQ = require('rsmq');
      var rsmq = new RedisSMQ({
          "host": '18.232.206.185',
          "port": 6379,
          ns: 'rsmq'
      });
      var allowCrossDomain = function (req, res, next) {
      
          res.header('Access-Control-Allow-Origin', '*');
          res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
          res.header('Access-Control-Allow-Headers', 'Content-Type,userId,x-onehop-token,businessId,corporateId');
          if (req.method == 'OPTIONS') {
              res.status(200).end();
          } else {
              console.log("------------------Req URL-----------------");
              console.log("Req.URL :: ", req.path);
              console.log("------------------Req method-----------------");
              console.log("Req.params :: ", req.method);
              console.log("--------------------------------------------");
              next();
          }
          // next();
      };
      app.use(allowCrossDomain);
      app.use(bodyParser.json({
          limit: '50mb'
      }));
      //app.use(bodyParser.json());
      app.use(bodyParser.urlencoded({
          limit: '50mb',
          extended: true,
          parameterLimit: 100000000
      }));
      
      app.get("/send-sms", (req, res) => {
          rsmq.sendMessage({ qname: "simple-test", message: "Hello World" }, function (err, resp) {
              if (resp) {
                  console.log("Message sent. ID:", resp);
                  res.status(200).send(JSON.stringify({ status: "success" ,resp:resp }));
              }
          });
      });
      
      var server = app.listen(4000, function () {
          console.log('Server listening on port 4000');
      
          rsmq.createQueue({
              qname: "simple-test",
              maxsize: -1
          }, function (error, resp) {
              if (resp === 1) {
                  console.log("simple-test queue created");
              } else {
                  console.log(error);
              }
          });
      });
      

1 个答案:

答案 0 :(得分:0)

这似乎是一个开发问题,而不是Docker问题。你有没有尝试过容器外的2个微服务?