无法使用 Node.js 连接到 Apache ActiveMQ
Unable to connect to Apache ActiveMQ with Node.js
背景
正在尝试通过 amqp10 使用 AMQP 1.0 协议连接到我的 Apache ActiveMQ 代理。我正在使用以下代码(改编自自述文件中的原始示例):
const AMQPClient = require("amqp10").Client;
const Promise = require("bluebird");
//Fix from: https://github.com/noodlefrenzy/node-amqp10/issues/241
const activeMQPolicy = require("amqp10").Policy;
const client = new AMQPClient(activeMQPolicy.ActiveMQ);
const setUp = () => {
return Promise.all([
client.createReceiver("amq.topic"),
client.createSender("amq.topic")
]);
};
client.connect("amqp://localhost")
.then(setUp)
.spread(function (receiver, sender) {
receiver.on("errorReceived", function (err) {
if (err) {
console.log(`failed with error: ${err}`);
return;
}
receiver.on("message", message => console.log(`Rx message: ${message.body}`));
return sender.send({ key: "Value" });
});
})
.error( err => console.log("error: ", err));
错误
执行失败,出现以下应用程序错误:
Workspace/activemq/node_modules/amqp10/lib/frames.js:64
stream.write(buffer, callback);
^
TypeError: Cannot read property 'write' of null
at Object.frames.writeFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/frames.js:64:9)
at Connection.sendFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:329:10)
at Connection._sendCloseFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:491:8)
at Connection._receiveAny (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:413:12)
at Connection._receiveData (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:358:8)
at NetTransport. (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:516:38)
at emitOne (events.js:96:13)
at NetTransport.emit (events.js:191:7)
at Socket. (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/transport/net_transport.js:26:49)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:560:20)
当 ActiveMQ 拒绝请求并显示以下消息时:
WARN | Connection attempt from non AMQP v1.0 client. AMQP,0,1,0,0
WARN | Transport Connection to: tcp://127.0.0.1:53188 failed: org.apache.activemq.transport.amqp.AmqpProtocolException: Connection from client using unsupported AMQP attempted
我试过的
我尝试实施此问题中描述的修复:
但是也没用。还尝试在 url 中指定端口 5672,但没有成功。
问题
此时我非常确定应用程序写入失败,因为 ActiveMQ 拒绝请求无效。
- 关于如何解决这个问题有什么想法吗?
最有可能的问题是客户端没有在连接上使用基于 SASL 的身份验证请求,而只是尝试直接连接。代理默认要求客户端在其打开握手时使用 SASL,即使身份验证机制仅为 'Anonymous',以便可以根据任何现有的代理安全策略验证客户端。
您可以通过将 wireFormat.allowNonSaslConnections=true
添加到代理的 AMQP TransportConnector 配置来禁用此检查,然后查看是否可以连接。这可能是让您继续前进的短期解决方案,但是在生产代理上您不应该使用该选项,并且应该有一个有效的安全策略和用户/密码身份验证。
阅读代理配置 here。
解决方案
在 ActiveMQ 论坛中询问后,事实证明@Tim 是正确的。引用用户 Gordon Sim-2:
As of 5.14 (...) ActiveMQ requires a SASL layer, even if you use
ANONYMOUS to authenticate. The examples for clients often don't
explicitly authenticate in any way (...)
要解决此问题,有两种选择。
选项 1 - 更改代码
第一个是改代码,正是我要找的:
To have the amqp10 client use a SASL layer, just specify a
saslMechanism override on the connect e.g.
client.connect(uri, {'saslMechanism':'ANONYMOUS'})
这种方法是由另一个名为 rhea
的 AMQP 1.0 Node.js 库的创建者建议的,我强烈建议您在阅读本文时检查它。
选项 2 - 更改代理配置文件
第二个,正如蒂姆建议的那样,是更改代理配置文件。如果您选择这种方法,我建议您阅读他的 post,因为它更完整。
背景
正在尝试通过 amqp10 使用 AMQP 1.0 协议连接到我的 Apache ActiveMQ 代理。我正在使用以下代码(改编自自述文件中的原始示例):
const AMQPClient = require("amqp10").Client;
const Promise = require("bluebird");
//Fix from: https://github.com/noodlefrenzy/node-amqp10/issues/241
const activeMQPolicy = require("amqp10").Policy;
const client = new AMQPClient(activeMQPolicy.ActiveMQ);
const setUp = () => {
return Promise.all([
client.createReceiver("amq.topic"),
client.createSender("amq.topic")
]);
};
client.connect("amqp://localhost")
.then(setUp)
.spread(function (receiver, sender) {
receiver.on("errorReceived", function (err) {
if (err) {
console.log(`failed with error: ${err}`);
return;
}
receiver.on("message", message => console.log(`Rx message: ${message.body}`));
return sender.send({ key: "Value" });
});
})
.error( err => console.log("error: ", err));
错误
执行失败,出现以下应用程序错误:
Workspace/activemq/node_modules/amqp10/lib/frames.js:64 stream.write(buffer, callback); ^
TypeError: Cannot read property 'write' of null at Object.frames.writeFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/frames.js:64:9) at Connection.sendFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:329:10) at Connection._sendCloseFrame (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:491:8) at Connection._receiveAny (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:413:12) at Connection._receiveData (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:358:8) at NetTransport. (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/connection.js:516:38) at emitOne (events.js:96:13) at NetTransport.emit (events.js:191:7) at Socket. (/home/pedro/Workspace/activemq/node_modules/amqp10/lib/transport/net_transport.js:26:49) at emitOne (events.js:96:13) at Socket.emit (events.js:191:7) at readableAddChunk (_stream_readable.js:178:18) at Socket.Readable.push (_stream_readable.js:136:10) at TCP.onread (net.js:560:20)
当 ActiveMQ 拒绝请求并显示以下消息时:
WARN | Connection attempt from non AMQP v1.0 client. AMQP,0,1,0,0 WARN | Transport Connection to: tcp://127.0.0.1:53188 failed: org.apache.activemq.transport.amqp.AmqpProtocolException: Connection from client using unsupported AMQP attempted
我试过的
我尝试实施此问题中描述的修复:
但是也没用。还尝试在 url 中指定端口 5672,但没有成功。
问题
此时我非常确定应用程序写入失败,因为 ActiveMQ 拒绝请求无效。
- 关于如何解决这个问题有什么想法吗?
最有可能的问题是客户端没有在连接上使用基于 SASL 的身份验证请求,而只是尝试直接连接。代理默认要求客户端在其打开握手时使用 SASL,即使身份验证机制仅为 'Anonymous',以便可以根据任何现有的代理安全策略验证客户端。
您可以通过将 wireFormat.allowNonSaslConnections=true
添加到代理的 AMQP TransportConnector 配置来禁用此检查,然后查看是否可以连接。这可能是让您继续前进的短期解决方案,但是在生产代理上您不应该使用该选项,并且应该有一个有效的安全策略和用户/密码身份验证。
阅读代理配置 here。
解决方案
在 ActiveMQ 论坛中询问后,事实证明@Tim 是正确的。引用用户 Gordon Sim-2:
As of 5.14 (...) ActiveMQ requires a SASL layer, even if you use ANONYMOUS to authenticate. The examples for clients often don't explicitly authenticate in any way (...)
要解决此问题,有两种选择。
选项 1 - 更改代码
第一个是改代码,正是我要找的:
To have the amqp10 client use a SASL layer, just specify a saslMechanism override on the connect e.g.
client.connect(uri, {'saslMechanism':'ANONYMOUS'})
这种方法是由另一个名为 rhea
的 AMQP 1.0 Node.js 库的创建者建议的,我强烈建议您在阅读本文时检查它。
选项 2 - 更改代理配置文件
第二个,正如蒂姆建议的那样,是更改代理配置文件。如果您选择这种方法,我建议您阅读他的 post,因为它更完整。