官方文档地址:http://www.rabbitmq.com/tutorials/tutorial-one-python.html
安装配置:http://www.ttlsa.com/linux/install-rabbitmq-on-linux/
1.hello world The simplest thing that doessomething
send.php
<?php //简单的发布消息 include_once __DIR__."/vendor/autoload.php"; use PhpAmqpLib\Connection\AMQPConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPConnection('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();
client.php
<?php
//简单的接收队列消息
include_once __DIR__."/vendor/autoload.php";
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPConnection('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();
new_task.php
<?php //队列分配发布 include_once __DIR__."/vendor/autoload.php"; use PhpAmqpLib\Connection\AMQPConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('task_queue', false, true, false, false); $data = implode(" ",array_splice($argv,1)); if(empty($data)) $data = 'hello world'; $msg = new AMQPMessage($data, array('delivery_mode'=>2)#定义分配模式 ); $channel->basic_publish($msg,'','task_queue'); echo " [x] Sent ", $data, "\n";
woker.php
<?php //队列分配接收 include_once __DIR__."/vendor/autoload.php"; use PhpAmqpLib\Connection\AMQPConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); //定义 $channel->queue_declare('task_queue', false, true, false, false); $data = implode(" ",array_splice($argv,1)); if(empty($data)) $data = 'hello world'; $msg = new AMQPMessage($data, array('delivery_mode'=>2)#定义分配模式 ); $channel->basic_publish($msg,'','task_queue'); echo " [x] Sent ", $data, "\n";
<?php //增加分配 include_once __DIR__."/vendor/autoload.php"; use PhpAmqpLib\Connection\AMQPConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->exchange_declare('logs', 'fanout', false, false, false); $data = implode(" ",array_splice($argv,1)); if(empty($data)) $data = 'hello world'; $msg = new AMQPMessage($data, array('delivery_mode'=>2)#make message persistent ); $channel->basic_publish($msg, 'logs'); echo " [x] Sent ", $data, "\n";
log_client.php
<?php //分配接收 require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPConnection; $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->exchange_declare('logs', 'fanout', false, false, false); list($queue_name, ,) = $channel->queue_declare("", false, false, true, false); $channel->queue_bind($queue_name, 'logs'); echo ' [*] Waiting for logs. 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();
4.Routing
<?php //根据severity不同发布 require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPConnection; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->exchange_declare('direct_logs', 'direct', false, false, false); $severity = $argv[1]; if(empty($severity)) $severity = "info"; $data = implode(' ', array_slice($argv, 2)); if(empty($data)) $data = "Hello World!"; $msg = new AMQPMessage($data); $channel->basic_publish($msg, 'direct_logs', $severity); echo " [x] Sent ",$severity,':',$data," \n"; $channel->close(); $connection->close(); ?>
log_direct_client.php
<?php //根据severity不同接收 require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPConnection; $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->exchange_declare('direct_logs', 'direct', false, false, false); list($queue_name, ,) = $channel->queue_declare("", false, false, true, false); $severities = array_slice($argv, 1); if(empty($severities )) { file_put_contents('php://stderr', "Usage: $argv[0] [info] [warning] [error]\n"); exit(1); } foreach($severities as $severity) { $channel->queue_bind($queue_name, 'direct_logs', $severity); } echo ' [*] Waiting for logs. To exit press CTRL+C', "\n"; $callback = function($msg){ echo ' [x] ',$msg->delivery_info['routing_key'], ':', $msg->body, "\n"; }; $channel->basic_consume($queue_name, '', false, true, false, false, $callback); while(count($channel->callbacks)) { $channel->wait(); } $channel->close(); $connection->close(); ?>
相关推荐
RabbitMq使用手册,介绍了rabbitmq的几种应用场景以及开发指导
springboot中rabbitmq使用demo,springboot中rabbitmq使用demo
MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队列协议)协议实现的消息队列,它是一种应用程序之间的通信方法,消息队列在分布式系统开 发中应用...
内容为自己总结的RabbitMQ的经验,内容大致有:安装顺序、关键文件路径、常用命令、集群注意事项等,有用的请收入
RabbitMQ使用手册 RabbitMQ安装。
两种简单Rabbitmq使用方案及其测试
springboot整合rabbitmq使用死信队列
RabbitMQ下载安装配置使用指南官方手册
RabbitMQ使用参考-YS.pdf
RabbitMQ使用参考-YS
rabbitmq使用环境【安装包、Erlang环境】
如题,C#的Demo项目:RabbitMQ封装和使用, 引用了RabbitMQ.Client 版本:3.6.9 RabbitMQ .NET客户端操作类库, 并简单展示了3种Exchange的使用
介绍RabbitMQ使用的教程
RabbitMQ使用规范
RabbitMQ客户连接池的Java实现。我们刚开始也是采用这种方式来实现的,但做压力测试时,发现这种每次新建Connection和新建Channel是非常耗时的,在大并发下,一般都要8毫秒左右,慢的话,好多都是几十毫秒。因此我们...
关于rabbitMQ的交换机使用方式!便于了解rabbitMQ的使用!
RabbitMQ连接池+SpringBoot实现。通过连接池实现将高效的管理RabbitMQ的Connection,并与springboot进行整合,实现消息发送,获取队列列表等功能。基于此可以进行更多功能的扩充。
JavaScript连接消息(RabbitMQ)
gitee仓库地址 https://gitee.com/ckl996/mq-demo 1.rabbitmq简单模式、工作模式、发布订阅模式 发布订阅模式: fanout广播模式 direct路由模式 topic主题订阅模式 资料内含docker部署mq文件及使用说明讲义
1:rabbitmq的命名规范 2:rabbitmq生产者开发规范 3:rabbitmq消费者开发规范