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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题