效果如图
php-queen | classic | D | D | ? | 0 | 0% | running | 2,615,208 | 0 | 20,750 | 2,615,208 | 2,615,208 | 65 MiB | 0 B | 527 kiB | 65 MiB | 65 MiB | 17,081/s | |||
php-queen_1 | classic | D | D | ? | 0 | 0% | running | 2,473,310 | 0 | 18,977 | 2,473,310 | 2,473,310 | 61 MiB | 0 B | 482 kiB | 61 MiB | 61 MiB | 17,082/s |
使用工具库:
https://github.com/php-amqplib/php-amqplib
php源码:
<?php require_once __DIR__.'/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Exchange\AMQPExchangeType; use PhpAmqpLib\Message\AMQPMessage; define('HOST', getenv('TEST_RABBITMQ_HOST') ? getenv('TEST_RABBITMQ_HOST') : 'localhost'); define('PORT', getenv('TEST_RABBITMQ_PORT') ? getenv('TEST_RABBITMQ_PORT') : 5672); define('USER', getenv('TEST_RABBITMQ_USER') ? getenv('TEST_RABBITMQ_USER') : 'guest'); define('PASS', getenv('TEST_RABBITMQ_PASS') ? getenv('TEST_RABBITMQ_PASS') : 'guest'); define('VHOST', '/'); define('AMQP_DEBUG', getenv('TEST_AMQP_DEBUG') !== false ? (bool)getenv('TEST_AMQP_DEBUG') : false); class mq{ protected $connection = null; public function __construct(){ $this->connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST); } public function send(){ $exchange = 'router'; $queue = 'php-queen_1'; $connection = $this->connection; $channel = $connection->channel(); /* The following code is the same both in the consumer and the producer. In this way we are sure we always have a queue to consume from and an exchange where to publish messages. */ /* name: $queue passive: false durable: true // the queue will survive server restarts exclusive: false // the queue can be accessed in other channels auto_delete: false //the queue won't be deleted once the channel is closed. */ $channel->queue_declare($queue, false, true, false, false); /* name: $exchange type: direct passive: false durable: true // the exchange will survive server restarts auto_delete: false //the exchange won't be deleted once the channel is closed. */ $channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false); $channel->queue_bind($queue, $exchange); for($i=0;$i<10000000;$i++){ $messageBody ="I am a msg from php script"; $message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)); $channel->basic_publish($message, $exchange); } $channel->close(); $connection->close(); } } $mq = new mq(); $mq->send();