php-rabbitmq安装、打印hello word

服务器Ubuntu16.04

1.安装服务器端

  apt-cache search rabbitmq

  apt install rabbitmq-server

2.using the php-amqplib Client 

  { "require": { "php-amqplib/php-amqplib": ">=2.6.1" } }
  composer install

3. send.php  

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');

echo " [x] Sent 'Hello World!'\n";

$channel->close();
$connection->close();
?>

 

4.receive.php

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$callback = function($msg) {
  echo " [x] Received ", $msg->body, "\n";
};
$channel->basic_consume('hello', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
    $channel->wait();
}
$channel->close();
$connection->close();
?>

5.终端运行代码实现消息传递

 

http://www.rabbitmq.com/tutorials/tutorial-one-php.html

 

 
posted @ 2017-07-31 16:57  tl_luo  阅读(176)  评论(0编辑  收藏  举报