话不多说直接上代码。
publisher.php
<?php
require "rabbitmq.php";
class Publisher extends mqService
{
public function __construct($exchangeName,$queueName,$routeKey)
{
parent::__construct($exchangeName,$queueName,$routeKey);
}
public function callback($envelope, $queue){
}
}
$publisher = new Publisher('ex1','','route1');
for($i=0;$i<5;$i++){
//暂无业务操作 以模拟发送代替
$publisher->sendMessage('hello wolrd');
}
consume.php
<?php
require "rabbitmq.php";
class Consumer extends mqService
{
public function __construct($exchangeName,$queueName,$routeKey)
{
parent::__construct($exchangeName,$queueName,$routeKey);
}
//重写虚基类中的虚拟方法、
public function callback($envelope, $queue){
sleep(1);
echo $envelope->getBody()."\n";
//显式确认,队列收到消费者显式确认后,会删除该消息
$queue->ack($envelope->getDeliveryTag());
//自行编写业务逻辑...
}
}
$consumer = new Consumer('ex1','qu1','route1');
$consumer->dealMessage();
rabbitmq.php
<?php
/**
* rabbitMq虚基类
*/
abstract class mqService{
//配置
protected $config = [
'host' => '/',
'vhost' => '/',
'port' => /,
'login' => '/',
'password' => '/'
];
protected $connection; //链接
protected $channel; //通道
protected $ex; //交换机
public $queue; //消费者队列
protected $exchangeName = ''; //交换机名
protected $queueName = ''; //队列名
protected $routeKey = ''; //路由键
protected $exchangeType = 'direct'; //交换机类型
protected $delivery_mode = 2; //消息类型
//初始化
public function __construct($exchangeName, $queueName, $routeKey,$exchangeType = 'direct', $config = array())
{
$this->exchangeName = empty($exchangeName) ? '' : $exchangeName;
$this->queueName = empty($queueName) ? '' : $queueName;
$this->routeKey = empty($routeKey) ? '' : $routeKey;
$this->exchangeType = empty($exchangeType) ? '' : 'direct';
if (!empty($config)) {
$this->setMQConfig($config);
}
//创建链接
$this->connection = new AMQPConnection($this->config);
if(!$this->connection->connect())
die('Connect errory : Can not connect to the broker');
//在链接中创建通道与交换机
$this->channel = new AMQPChannel($this->connection);
$this->ex = new AMQPExchange($this->channel);
//设置交换机
$this->setExchange($exchangeName,$exchangeType);
//消费者名称存在时设置队列
if($this->queueName){
$this->queue = new AMQPQueue($this->channel);
$this->setQueue();
}
}
/**
* 发送消息
* mix : msg
*/
public function sendMessage($msg){
// wait service logic
echo 'Send Message : ' . $this->ex->publish(json_encode($msg),$this->routeKey,AMQP_NOPARAM,['deliver_mode' => $this->delivery_mode]) . PHP_EOL;
}
//处理消息
public function dealMessage(){
// wait service logic
$this->queue->consume(function($envelope, $queue){
$this->callback($envelope, $queue);
});
}
//申明消费者中的虚函数
abstract public function callback($envelope, $queue);
//设置队列
protected function setQueue(){
$this->queue->setName($this->queueName);
//设置队列持久化
$this->queue->setFlags(AMQP_DURABLE);
//声明队列
$this->queue->declareQueue();
//交换机与队列通过routeKey进行绑定
$this->queue->bind($this->exchangeName,$this->routeKey);
}
//设置交换机
protected function setExchange($name,$type){
//AMQP_EX_TYPE_DIRECT:直连交换机
//AMQP_EX_TYPE_FANOUT:扇形交换机
//AMQP_EX_TYPE_HEADERS:头交换机
//AMQP_EX_TYPE_TOPIC:主题交换机
$this->ex->setName($name);
$this->ex->setType($type);
$this->ex->setFlags(AMQP_DURABLE);
$this->ex->declareExchange();
}
//重设mq配置
protected function setMQConfig($config){
if(!is_array($config))
die('Type error:config not a array');
foreach($config as $k => $v){
$this->config[$k] = $v;
}
}
}
使用结果如下
生产者
消费者