RabbitMQ和RabbitMQBundle路由密钥配置到多消费者

RabbitMQ和RabbitMQBundle路由密钥配置到多消费者,rabbitmq,symfony4,Rabbitmq,Symfony4,我在Symfomy4中使用RabbitMqBundle 我想要实现的是发布消息(在我的情况下是通知),并通过路由键选择是将消息存储在Db中还是通过电子邮件发送,或者两者兼而有之 我专注于主题交换,但我不知道如何达到这个目标,也许我不完全了解RabbitMQ的机制,但我对它完全陌生 这是我的配置 old_sound_rabbit_mq: connections: default: #url: '%env(RABBITMQ_URL)%' url: 'amqp://

我在Symfomy4中使用RabbitMqBundle

我想要实现的是发布消息(在我的情况下是通知),并通过路由键选择是将消息存储在Db中还是通过电子邮件发送,或者两者兼而有之

我专注于主题交换,但我不知道如何达到这个目标,也许我不完全了解RabbitMQ的机制,但我对它完全陌生

这是我的配置

old_sound_rabbit_mq:
  connections:
    default:
      #url: '%env(RABBITMQ_URL)%'
      url: 'amqp://guest:guest@localhost:5672'
      vhost:    '/'
      lazy:     false
      connection_timeout: 3
      read_write_timeout: 3
  producers:
    notifications:
      connection: default
      exchange_options: {name: 'notifications', type: topic}
  consumers:
    store_notifications:
      connection: default
      exchange_options: {name: 'notifications', type: topic}
      queue_options:
        name: 'notifications'
        routing_keys:
        - 'notification.store'
        # - 'notification.*' # this will match everything
      callback: App\Consumer\Notification\DbHandler
    email_notifications:
      connection: default
      exchange_options: {name: 'notifications', type: topic}
      queue_options:
        name: 'notifications'
        routing_keys:
        - 'notification.email'
      callback: App\Consumer\Notification\EmailHandler
在这种情况下,我可以只将消息发布到其中一个路由键:notification.storenotification.email

我想要发布($msg,['notification.store','notification.email']),但我知道我可以让消费者使用通配符收听多个路由键,但我不知道如何配置它

这可能吗?

我想你可以通过以下方式来实现:

  • 如果您只想存储数据库,那么路由键是:notification.store
  • 如果您只想发送电子邮件,则路由密钥为:notification.email
  • 如果要同时执行这两项操作,则路由键为:notification.both
然后,您的队列应使用以下路由密钥绑定到exchange:

  • 商店通知:[notification.store,notification.both]
  • 电子邮件通知:[通知。电子邮件,通知。两者]
这样做,如果邮件带有路由
notification.store
只需转到
store\u notifications
notification.email
只需转到
email\u notifications
。但是带有路由
通知的消息。两个
都会进入两个队列

配置:

  consumers:
    store_notifications:
      connection: default
      exchange_options: {name: 'notifications', type: topic}
      queue_options:
        name: 'notifications'
        routing_keys:
        - 'notification.store'
        - 'notification.both'
      callback: App\Consumer\Notification\DbHandler
    email_notifications:
      connection: default
      exchange_options: {name: 'notifications', type: topic}
      queue_options:
        name: 'notifications'
        routing_keys:
        - 'notification.email'
        - 'notification.both'
      callback: App\Consumer\Notification\EmailHandler

希望这能有所帮助。

此外,您还需要从这些队列中提取不同类型的消费者(即一个用于数据库,一个用于电子邮件),以防这不明显。