Rabbit为AMQP 1.0消息节省了额外的字节

问题描述 投票:1回答:1

我有一个环境,其中一些AMQP 1.0和一些AMQP 0.9.1客户端需要写入/读取RabbitMQ队列。我启用了AMQP 1.0 Rabbit插件并且它正在工作,但我在每个AMQP 1.0消息的主体中获得额外的字节。

我通过使用rhea(typescript)通过AMQP1.0发送消息:

const connection: Connection = new Connection (
  {
    host: 'localhost',
    port: 5672,
    id: 'my_id',
    reconnect: true
  }
);

const senderName = "sender01";
const senderOptions: SenderOptions = {
  name: senderName,
  target: {
    address: "target.queue"
  },
  onError: (context: EventContext) => {},
  onSessionError: (context: EventContext) => {}
};

await connection.open();
const sender: Sender = await connection.createSender(senderOptions);
sender.send({
  body: JSON.stringify({"one": "two", "three": "four"}),
  content_encoding: 'UTF-8',
  content_type: 'application/json'
});
console.log("sent");
await sender.close();

await connection.close();
console.log("connection closed");

此示例有效,但这是存储在队列中的内容:

enter image description here

base64编码的消息是AFN3oRx7Im9uZSI6InR3byIsInRocmVlIjoiZm91ciJ9,解码后变为:

Sw{"one":"two","three":"four"}

有一个额外的Sw,我没有发送。

我尝试使用官方RabbitMQ库(使用AMQP 0.9.1)设置一个java客户端,以查看这些额外的字节是否已发送到客户端:

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.basicConsume(
  "target.queue",
  true,
  (consumerTag, delivery) -> {
    String message = new String(delivery.getBody(), "UTF-8");
    System.out.println(" [x] Received '" + message + "'");
  },
  ignored -> {}
);

这是输出:

 [x] Received ' Sw�{"one":"two","three":"four"}'

奇怪的是,如果我尝试使用AMQP 1.0客户端使用完全相同的消息,那些额外的字节不会出现在接收的消息体中,只有在使用AMQP 1.0发布并使用AMQP 0.9.1进行预订时才会出现额外的字节。

这是为什么?在使用两个AMQP版本时,有什么方法可以避免额外的字节数吗?


UPDATE

我也试过SwiftMQ:

int nMsgs = 100;

int qos = QoS.AT_MOST_ONCE;
AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
String host = "localhost";
int port = 5672;
String queue = "target.queue";

try {

  Connection connection = new Connection(ctx, host, port, false);
  connection.setContainerId(UUID.randomUUID().toString());
  connection.setIdleTimeout(-1);
  connection.setMaxFrameSize(1024 * 4);
  connection.setExceptionListener(Exception::printStackTrace);
  connection.connect();
  {

    Session session = connection.createSession(10, 10);
    Producer p = session.createProducer(queue, qos);
    for (int i = 0; i < nMsgs; i++) {
      AMQPMessage msg = new AMQPMessage();
      System.out.println("Sending " + i);
      msg.setAmqpValue(new AmqpValue(new AMQPString("{\"one\":\"two\",\"three\":\"four\"}")));
      p.send(msg);
    }
    p.close();
    session.close();
  }
  connection.close();
} catch (Exception e) {
  e.printStackTrace();
}

问题仍然存在,但第一个字节改变了,现在我得到:

[x] Received '□�□□□□□□□w�{"one":"two","three":"four"}'
rabbitmq amqp
1个回答
0
投票

请参阅this response,其中阐明了如何编码有效负载并避免这些额外的字节:

如果AMQP 1.0客户端向0-9-1客户端发送消息并在“数据部分”中将其有效负载编码为二进制(即不在amqp-sequence部分,而不是在amqp-value部分中),则0-9-1客户端将获得没有任何额外字节的完整有效负载


注意:RabbitMQ团队监控rabbitmq-users mailing list,有时只回答StackOverflow上的问题。

© www.soinside.com 2019 - 2024. All rights reserved.