2016-07-12 155 views
0

我試圖用https://github.com/php-amqplib/RabbitMqBundle和Symfony2框架實現RabbitMQ。Oldsound rabbitmq bundle多個消費者配置

我已經設法讓事情與1個生產者和1個消費者一起工作,但問題是當我使用多個消費者。

這是我的配置:

old_sound_rabbit_mq: 
    connections: 
     default: 
      host:  'localhost' 
      port:  5672 
      user:  'guest' 
      password: 'guest' 
      vhost: '/' 
      lazy:  false 
      connection_timeout: 3 
      read_write_timeout: 3 

      # requires php-amqplib v2.4.1+ and PHP5.4+ 
      keepalive: false 

      # requires php-amqplib v2.4.1+ 
      heartbeat: 0 

      #requires php_sockets.dll 
#   use_socket: true # default false 
    producers: 
     soccer_team_stat: 
      connection:  default 
      exchange_options: {name: 'soccer_team_stat_ex', type: direct} 
      queue_options: {name: 'soccer_team_stat_qu'} 
     soccer_team_stat_form: 
      connection: default 
      exchange_options: {name: 'soccer_team_stat_ex', type: direct} 
      queue_options: {name: 'soccer_team_stat_form_qu'} 
    consumers: 
     soccer_team_stat: 
      connection:  default 
      exchange_options: {name: 'soccer_team_stat_ex', type: direct} 
      queue_options: {name: 'soccer_team_stat_qu'} 
      callback:   myapp.soccer_team_stat.consume 
     soccer_team_stat_form: 
      connection:  default 
      exchange_options: {name: 'soccer_team_stat_ex', type: direct} 
      queue_options: {name: 'soccer_team_stat_form_qu'} 
      callback:   myapp.soccer_team_stat_form.consume 

服務定義:

<services> 

     <service class="MyApp\EtlBundle\Producers\SoccerTeamStatProducer" id="myapp.soccer_team_stat.produce"> 
      <argument type="service" id="old_sound_rabbit_mq.soccer_team_stat_producer"/> 
     </service> 

     <service class="MyApp\EtlBundle\Producers\SoccerTeamStatProducer" id="myapp.soccer_team_stat_form.produce"> 
      <argument type="service" id="old_sound_rabbit_mq.soccer_team_stat_producer"/> 
     </service> 

     <service class="MyApp\EtlBundle\Consumers\SoccerTeamStatConsumer" id="myapp.soccer_team_stat.consume"> 
      <argument type="service" id="service_container"/> 
     </service> 

     <service class="MyApp\EtlBundle\Consumers\SoccerTeamStatFormConsumer" id="myapp.soccer_team_stat_form.consume"> 
      <argument type="service" id="service_container"/> 
     </service> 

    </services> 

而且在PHP應用程序/控制檯的RabbitMQ:消費者-d soccer_team_stat_form我得到:

[Symfony的\ Component \ DependencyInjection \ Exception \ ServiceNotFoundException] 您已請求不存在的服務 「old_sound_rabbit_mq.soccer_team_stat_form_consumer」。

我試過各種組合,包括使用multiple_consumers配置鍵但沒有成功。我錯過了什麼?

回答

1

如果沒有的routing_keybinding_key設置,direct交流會像fanout和郵件發送到它知道這樣基於什麼我從你的配置看到所有隊列,你最好使用fanout所以不喜歡下面。

old_sound_rabbit_mq: 
    connections: 
     default: 
      host:  %rabbit_mq_host% 
      port:  %rabbit_mq_port% 
      user:  %rabbit_mq_user% 
      password: %rabbit_mq_pswd% 
      vhost: /
      lazy:  true 
    producers: 
     soccer_team_stat: 
      connection:  default 
      exchange_options: { name: 'soccer_team_stat_ex', type: fanout } 
     soccer_team_stat_form: 
      connection:  default 
      exchange_options: { name: 'soccer_team_stat_form_ex', type: fanout } 
    consumers: 
     soccer_team_stat: 
      connection:  default 
      exchange_options: { name: 'soccer_team_stat_ex', type: fanout } 
      queue_options: { name: 'soccer_team_stat_qu' } 
      callback:   myapp.soccer_team_stat.consume 
     soccer_team_stat_form: 
      connection:  default 
      exchange_options: { name: 'soccer_team_stat_form_ex', type: fanout } 
      queue_options: { name: 'soccer_team_stat_form_qu' } 
      callback:   myapp.soccer_team_stat_form.consume 

RabbitMQ fanout example with symfony including 2 Producer & 2 Exchange & 2 Queue & N Worker & 2 Consumer是完整的例子(實際上是完全回答你的問題/已取得的你想要做什麼版本),顯示事情是如何的symfony應用程序中完成的。我建議你遵循那裏使用的模式。非常容易遵循和維護。如果您想要更多示例,只需在該博客中搜索RabbitMQ關鍵字即可。