dou31797719 2018-08-16 11:10
浏览 59

PHP中的RabbitMQ使用者

I'm implementing the Notifications system using RabbitMQ and would like to hear the best practices for the following issues.

RabbitMQ server is installed on a localhost.

My code for Producer:

    // ... Initialize connection

    $this->channel->exchange_declare('my_exchange', 'direct', false, false, false);

    // Detect routing key
    $parameters = $notification->getParameters();

    $routing_key = !empty($parameters['consumer']) ? $parameters['consumer'] : 'admin.0';

    $message = new AMQPMessage($notification->getMessage(), ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);

    $this->channel->basic_publish($message, 'my_exchange', $routing_key);

    // ... Close connection

My code for Consumer:

// ... Initialize connection

$channel->exchange_declare('my_exchange', 'direct', false, false, false);

list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);

// Bind admin routing key
$channel->queue_bind($queue_name, 'my_exchange', "admin.0");

// Bind seller routing keys
foreach ($sellers_ids_from_db as $seller_id) {
    $channel->queue_bind($queue_name, 'my_exchange', "seller.{$seller_id}");
}

// Bind customer routing keys
foreach ($customers_ids_from_db as $customer_id) {
    $channel->queue_bind($queue_name, 'my_exchange', "customer.{$customer_id}");
}

echo " [*] Waiting for logs. To exit press CTRL+C
";

$channel->basic_consume($queue_name, '', false, false, false, false, function ($msg) {
    echo ' [x] ', $msg->delivery_info['routing_key'], ':', $msg->body, "
";
});

while (count($channel->callbacks)) {
    $channel->wait();
}

// ... Close connection
  1. What is the correct way to initialize a Consumer?

According to the docs, a Consumer should be stored in a seperate php script and should be called from the terminal (e.g. php Consumer.php), but what if we don't have access to it (common issue on a shared hosting)? First solution that comes in mind is to execute php from the code (using exec('php Consumer.php')), but is it a good practice? Still, execs could also be forbidden, what is the solution then?

Or maybe there is a way to somehow have a Consumer in a class method and call it?

  1. How many queues should I have?

There are three groups of users that should receive notifications: admins, sellers and customers. Currently, I have a queue for each user in each group (e.g. queues admin.1, admin.2, seller.1, seller.2 etc.). Using this approach, I have to start a Consumer only once, but the problem is that I have to restart it each time a new user is added to any group. Is there a better implementation for this?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • duanduo0520 2019-03-12 11:04
    关注
    1. Yes you can call consumer from class method.. Best way is create a shell script and call consumer in a infinite loop.
    2. You can create multiple queues and every queue create connection with separated consumer. "my_exchange" area you need to set a blank value for creating multiple queue.
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器