【问题标题】:RabbitMQ and RabbitMQBundle routing keys configuration to multi consumerRabbitMQ 和 RabbitMQBundle 路由键配置到多消费者
【发布时间】:2018-08-14 15:02:24
【问题描述】:

我在 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'])之类的东西,但我知道我可以让消费者监听多个路由键并使用通配符,但我不知道如何配置它。

这可能吗?

【问题讨论】:

    标签: rabbitmq symfony4


    【解决方案1】:

    我认为你可以这样做:

    • 如果只想存储DB,路由键为:notification.store
    • 如果你只是想发邮件,路由键是:notification.email
    • 如果你想两者都做,路由键是:notification.both

    然后,您的队列应该使用这些路由键绑定到交换:

    • store_notifications:[notification.store,notification.both]
    • email_notifications:[notification.email,notification.both]

    通过这样做,如果带有路由notification.store 的消息只需转到store_notificationsnotification.email 只需转到email_notifications。但是路由notification.both 的消息会进入两个队列。

    配置:

      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
    

    希望这会有所帮助。

    【讨论】:

    • 此外,您还需要从这些各自的队列中提取不同类型的消费者(即一种用于数据库,一种用于电子邮件),以防不明显。
    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多