我是RabbitMQ的新手,并想知道如何设置系统以最好地满足我的需求。
假设我有5个消费者流程(C1-5),而C1-3只需要一个来消费该消息。 C4和C5 也需要接收消息。
如何配置rabbitMQ来实现这一目标?
我考虑过在C1-3前弹出另一个消费者来简单地推送到标准队列,而C1-3会消耗掉,但我想知道这是否是额外的工作而且RabbitMQ有更好的方法来解决这个问题?
非常感谢任何建议。
亲切的问候, FAB
答案 0 :(得分:2)
这很容易做到,只需将C1-C3连接到同一队列即可。不需要额外的消费者/发布者路线。
(使用PHP和videlalvaro/php-amqplib 。)
宣布粉丝交换" events" 并向其发布消息。
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
$exchange = 'events';
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->exchange_declare($exchange, 'fanout', false, false, false);
$data = implode(' ', array_slice($argv, 1));
if(empty($data)) $data = "Something happened!";
$msg = new AMQPMessage($data);
$channel->basic_publish($msg, $exchange);
echo " [x] Sent ", $data, "\n";
$channel->close();
$connection->close();
附加到指定的" pool" 队列。消息将在接收者之间分发。
use PhpAmqpLib\Connection\AMQPConnection;
$exchange = 'events';
$queue = 'pool';
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->exchange_declare($exchange, 'fanout', false, false, false);
$channel->queue_declare($queue);
$channel->queue_bind($queue, $exchange);
echo ' [*] Waiting for events. To exit press CTRL+C', "\n";
$callback = function($msg) {
echo ' [x] ', $msg->body, "\n";
};
$channel->basic_consume($queue, '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
这些接收器中的每一个都有自己的队列和自己的消息副本。
use PhpAmqpLib\Connection\AMQPConnection;
$exchange = 'events';
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->exchange_declare($exchange, 'fanout', false, false, false);
list($queue_name, ,) = $channel->queue_declare('');
$channel->queue_bind($queue_name, $exchange);
echo ' [*] Waiting for events. To exit press CTRL+C', "\n";
$callback = function($msg) {
echo ' [x] ', $msg->body, "\n";
};
$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();