NodeJS AMQP客户端无法连接 [英] NodeJS AMQP Client can't connect

查看:524
本文介绍了NodeJS AMQP客户端无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近两天我发疯了,我无法通过持久交换和持久队列在NodeJS客户端上建立连接.

I am going crazy for last two days, i cannot make connection on NodeJS client with durable exchange and durable queue.

因此,PHP代码创建并发送消息:

So PHP code creates and send message:

<?php 
$connection = new AMQPConnection(array(
'host' => 'localhost',
'vhost' => 'bvh',
'port' => 5672,
'login' => 'bizneus',
'password' => 'lozinkus'
));
    //$connection = new AMQPConnection();
    $connection->connect();
    if (!$connection->isConnected()) {
        die('Not connected :(' . PHP_EOL);
    }
    // Open Channel
    $channel    = new AMQPChannel($connection);
    // Declare exchange
    $exchange   = new AMQPExchange($channel);
    $exchange->setName('biznea_e_1');       
    $exchange->setType('fanout');
    $exchange->setFlags(AMQP_DURABLE);

    $exchange->declare();
    // Create Queue
    $queue      = new AMQPQueue($channel);
    $queue->setName('notify');
    $queue->setFlags(AMQP_DURABLE);
    $queue->declare();

    $message    = $exchange->publish(json_encode($s), 'kljuc');
    if (!$message) {
        echo 'Message not sent', PHP_EOL;
    } else {
        echo 'Message sent!', PHP_EOL;
    }

    if ($connection->isConnected()) {
       $connection->disconnect();
    }

在屏幕上显示已发送消息.

On screen it says that messege is sent.

接下来的事情是NodeJS客户端,它应该获取消息,但不能:

Next thing is NodeJS client, which should get messages, but it can't:

var amqp = require('amqp');

var conParam = { 
  host: 'localhost', 
  port: 5672,  
  login: 'bizneus',   
  password: 'lozinkus',    
  vhost: 'bvh'  
}
var connection = amqp.createConnection(conParam);
connection.on('ready', function(){

     var exchange = connection.exchange('biznea_e_1');
      var queue = connection.queue('notify');
      queue.bind('biznea_e_1', 'kljuc');

                queue.subscribe( {ack:true}, function(message){

                    var dj = JSON.parse(message.data.toString());

                    console.log(JSON.stringify(dj));

                    queue.shift();
            });
});

但我收到此错误

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: PRECONDITION_FAILED - cannot redeclare exchange 'biznea_e_1' in vhost 'bvh' with different type, durable, internal or autodelete value
    at Exchange._onMethod (/home/zijad/node_modules/amqp/amqp.js:1824:15)
    at Exchange.Channel._onChannelMethod (/home/zijad/node_modules/amqp/amqp.js:1365:14)
    at Connection._onMethod (/home/zijad/node_modules/amqp/amqp.js:922:28)
    at AMQPParser.self.addListener.parser.onMethod (/home/zijad/node_modules/amqp/amqp.js:797:12)
    at AMQPParser._parseMethodFrame (/home/zijad/node_modules/amqp/amqp.js:442:10)
    at frameEnd (/home/zijad/node_modules/amqp/amqp.js:187:16)
    at frame (/home/zijad/node_modules/amqp/amqp.js:172:14)
    at AMQPParser.header [as parse] (/home/zijad/node_modules/amqp/amqp.js:159:14)
    at AMQPParser.execute (/home/zijad/node_modules/amqp/amqp.js:231:21)
    at Connection.<anonymous> (/home/zijad/node_modules/amqp/amqp.js:837:12)

我试图删除var exchange = connection.exchange('biznea_e_1');那条线,但是它不能声明队列.

I tried to remove var exchange = connection.exchange('biznea_e_1'); that line but than it cannot declare queue.

我只想从PHP发送消息到NodeJS deamon,仅此而已!

I just want to send messages from PHP to NodeJS deamon and that is all!

帮助:)

推荐答案

尝试以下操作:在node.js代码中,使用与在PHP中相同的参数 EXACTLY 声明交换和队列.代码.例如耐用的.这样可以解决您的问题.

Try this: In the node.js code, declare the exchanges and queues with EXACTLY the same parameters as you did in your PHP code. e.g. durable. This may solve your problem.

干杯!

这篇关于NodeJS AMQP客户端无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆