Symfony2和RabbitMqBundle.无法发布消息

Gab*_*iak 5 rabbitmq symfony

我试图从这里使用syfmony2框架与RabbitMqBundle

我确信我的rabbitmq服务器已启动并正在运行,我正在根据github上提供的文档执行配置和发布者代码.不幸的是,我不能将任何消息添加到队列中.

我确信我的rabbitmq服务器正常运行.我有相应的队列命名为symfony配置文件.

谁有任何线索有什么问题?

在此先感谢您的任何建议.

chm*_*iuk 10

好吧......尝试这个简单的例子

# app/config.yml
old_sound_rabbit_mq:
    connections: %rabbitmq_connections%
    producers: %rabbitmq_producers%
    consumers: %rabbitmq_consumers%

parameters:
    # connection parameters
    rabbitmq_connections:
        default: { host: 'localhost', port: 5672, user: 'guest', password: 'guest', vhost: '/' }

    # define producers
    rabbitmq_producers:
        sample:
            connection:         default
            exchange_options:   {name: 'exchange_name', type: direct, auto_delete: false, durable: true}

    # define consumers
    rabbitmq_consumers:
        sample:
            connection:         default
            exchange_options:   {name: 'exchange_name', type: direct, auto_delete: false, durable: true}
            queue_options:      {name: 'sample', auto_delete: false}
            callback:           rabbitmq.callback.service
Run Code Online (Sandbox Code Playgroud)

那么你应该定义你的回调服务.随意把它放进去app/config.yml

services:
    rabbitmq.callback.service:
        class: RabbitMQ\Callback\Service
Run Code Online (Sandbox Code Playgroud)

是的.你应该写这个回调服务.这里是简单的实现.应该足以理解和检查它是否适合您.

namespace RabbitMQ\Callback;

use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Message\AMQPMessage;

class Service implements ConsumerInterface 
{
    public function execute(AMQPMessage $msg)
    {
        var_dump(unserialize($msg->body));
    }
}     
Run Code Online (Sandbox Code Playgroud)

然后你应该启动rabbitmq服务器,运行消费者并检查是否添加了新的交换和队列.要运行测试用户,你应该运行

app/console rabbitmq:consumer sample --route="sample"
Run Code Online (Sandbox Code Playgroud)

在您的控制器中(您要将消息发送到rabbitMQ的下一个代码

# get producer service
$producer = $this->get('old_sound_rabbit_mq.sample_producer');
# publish message
$producer->publish(serialize(array('foo'=>'bar','_FOO'=>'_BAR')), 'sample');
Run Code Online (Sandbox Code Playgroud)

希望它或多或少清楚,并将帮助您使用rabbitmq.

PS:如果你有rabbitmq管理插件,它更容易调试.如果你没有,使用控制台命令,如rabbitmqctl检查队列/交换/消费者等...

并且很高兴看到生产者/消费者的配置.回调服务代码也是如此.