【问题标题】:rabbitmq publishing to exchange throws 404 even after assertExchange即使在 assertExchange 之后,rabbitmq 发布到交换也会抛出 404
【发布时间】:2021-10-23 20:28:31
【问题描述】:

我正在使用 RabbitMQ amqplib 包来在我的 express 应用程序中发出和订阅事件。

这就是我实现事件发射的方式:

初始化:

  const connection = await rabbitmq.connect(Config.RabbitMQConnectionString);
  this.channel = await connection.createChannel();

发射函数:

  public async emit(eventName: string, data: any): Promise<void> {
    const event = {
      data,
      eventName
    };

    this.channel.assertExchange(eventName, 'fanout', { durable: false });

    const serializedData = JSON.stringify(event);
    this.channel.publish(eventName, '', Buffer.from(serializedData));
  }

用法:

eventBus.emit('some.event.name', { someData: true } );

如您所见,在每个channel.publish 之前,我都会使用channel.assertExchange 确保交换存在,那么为什么会收到以下消息?

{"code":404,"classId":60,"methodId":40,"level":"error","message":"Channel 由服务器关闭:404(未找到),消息为“未找到 - 否 在 vhost '/' 中交换 'some.event.name'” 频道被服务器关闭: 404 (NOT-FOUND) 带有消息“NOT_FOUND - 没有交换 虚拟主机中的“some.event.name”

【问题讨论】:

    标签: node.js express events rabbitmq amqp


    【解决方案1】:

    原来缺少的是我没有等待assertExchange 任务完成。

    await this.channel.assertExchange(eventName, 'fanout', { durable: false });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多