Oldsound rabbitmq捆绑多个消费者配置

时间:2016-07-12 08:37:48

标签: symfony configuration rabbitmq

我正在尝试使用https://github.com/php-amqplib/RabbitMqBundle和Symfony2框架实现RabbitMQ。

我设法让这个东西与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 app / console rabbitmq:consumer -d soccer_team_stat_form我得到:

  

[Symfony的\元器件\ DependencyInjection \异常\ ServiceNotFoundException的]   您已请求不存在的服务   “old_sound_rabbit_mq.soccer_team_stat_form_consumer”。

我尝试了使用multiple_consumers配置键的各种组合,但没有成功。我错过了什么?

1 个答案:

答案 0 :(得分:1)

如果routing_keybinding_key都未设置,则direct交换将表现得像fanout,并根据我的&#将消息发送到它知道的所有队列39;从您的配置中看到,您最好使用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关键字。