消息队列的实践php,php消息队列处理实践 ,利用AMQP和redis两种方法
一:利用AMQP方法
類 amqp.php
e_name = $e_name;
$this->q_name = $q_name;
$this->k_route = $k_route;
//創建連接和channel
$this->conn = new AMQPConnection($config);
if (!$this->conn->connect()) {
return array('error_code' => 1,'msg'=>'Cannot connect to the broker!' );
}
$this->channel = new AMQPChannel($this->conn);
$this->CreateExchange();
$this->CreateQueue();
}
//創建交換機
public function CreateExchange()
{
$ex = new AMQPExchange($this->channel);
$ex->setName($this->e_name);
$ex->setType(AMQP_EX_TYPE_DIRECT); //direct類型
$ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); //持久化
//echo "Exchange Status:".$ex->declare()."\n"; //隊列內容總數
$ex->declare();
$this->ex = $ex;
}
//創建隊列
public function CreateQueue()
{
$q = new AMQPQueue($this->channel);
$q->setName($this->q_name);
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); //持久化
//echo "Message Total:".$this->q->declare()."\n";
//綁定交換機與隊列,并指定路由鍵
//echo "queue status: ".$q->declare();
//echo "\n";
//echo 'Queue Bind: '.$q->bind($this->e_name, $this->k_route)."\n";
//echo "\n";
$q->bind($this->e_name, $this->k_route);
}
//發送消息
public function send($msg)
{
//$this->CreateExchange();
//$this->CreateQueue();
$message=json_encode($msg);
$this->channel->startTransaction();
//echo "send: ".$this->ex->publish($message, $this->k_route); //將你的消息通過制定routingKey發送
$status = $this->ex->publish($message, $this->k_route);
$this->channel->commitTransaction();
$this->conn->disconnect();
return array('status'=>$status);
}
//獲取消息
public function get()
{
$q = new AMQPQueue($this->channel);
$q->setName($this->q_name);
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
//$q->delete();刪除隊列
$return=array();
while($a=$q->declare())
{
//echo "queue status: ".$a;
//echo "==========\n";
$messages = $q->get(AMQP_AUTOACK);
$return[]=json_decode($messages->getBody(),true);
//echo "\n";
}
$this->conn->disconnect();
return $return;
}
}
配置文件:
config.php
return array(
'amqp'=>array(
array(
'host' => 'localhost',
'port' => '5672',
'vhost' => '/',
'user' => 'guest',
'password' => 'guest'
)
),
);
接收并處理文件:
get.php
require_once('amqp.php');
$config = require('config.php');
$config_qmqp = $config['amqp'];
$e_name = 'e_guest'; //交換機名
$k_route = 'k_route_sendemail'; //路由key
$q_name = 'q_guest_sendemail'; //隊列名
$amqp = new Amqp($config_qmqp,$e_name,$q_name,$k_route);
$re = $amqp->get();
加入隊列文件:
send.php
require_once('amqp.php');
$e_name = 'e_guest'; //交換機名
$k_route = 'k_route_feedpush'; //路由key
$q_name = 'q_guest_feedpush'; //隊列名
$config = config('amqp');
$amqp = new Amqp(config('amqp'),$e_name,$q_name,$k_route);
$msg = array('test','123');
$re = $amqp->send($msg);
二:利用redis做消息隊列處理
//redis出隊列POP
function actionRedisPop()
{
$redis = new Redis;
$redis->connect('cloud_redis',9002);
while ($usr = $redis->rPop('list_test')) {
$array = json_decode($usr,true);
print_R($array);
}
}
//redis入隊列push
function actionRedisPush()
{
$redis = new Redis;
$redis->connect('cloud_redis',9002);
$data = array('list_name'=>'usr','value'=>date('Y-m-d H:i:s'));
$json = json_encode($data);
var_dump($redis->lPush('list_test', $json));
}
總結
以上是生活随笔為你收集整理的消息队列的实践php,php消息队列处理实践 ,利用AMQP和redis两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 织梦后台上传文章的php文件是那个,如何
- 下一篇: php 超过100m文件上传,科技常识: