php 连接rabbitmq两种方案二 amqserver

类文件 class_ampser.php

<?php
/**
* RabbitMQ 调用服务
* @author Patrick Wang
*/
// use util\mq\PhpAmqpLib\Connection\AMQPConnection;
// use util\mq\PhpAmqpLib\Message\AMQPMessage;

class AMQPService
{
private $connected = FALSE;
private $conn = NULL;
public $error = FALSE;
private $arguments = array();

private $host;
private $port;
private $user;
private $pass;
private $vhost;

private $message_template;

public function __construct()
{
global $C;
$this->host = $C->MQ_HOST;
$this->port = $C->MQ_PORT;
$this->user = $C->MQ_USER;
$this->pass = $C->MQ_PASS;
$this->vhost = $C->MQ_VHOST;

$this->message_template = new message_template();
$this->arguments = array('content_type'=>'text/plain',
'content_encoding'=>null,
// 'message_id'=>'',
// 'user_id'=>54, //这个值是mq内部的user,比如guest
'app_id'=>0,
'delivery_mode'=>'2',
'priority'=> null,
'timestamp'=>time(),
'expiration'=>'',
'type'=>'message',
'reply_to'=>'');
}

public function connect($db_name)
{
if( ! $this->connected){
//创建连接和channel
$this->conn = new AMQPConnection($this->host, $this->port, $this->user, $this->pass, $this->vhost);
if (!$this->conn->connect()) {
die("Cannot connect to the broker!\n");
}
$this->channel = $this->conn->channel();
}
return TRUE;
}

/**
* php amqp扩展调用方法
* 发布消息(交换机、队列和路由KEY,3个名称不要重复)
* @param $exchangeName :交换机名称
* @param $queueName :队列名称
* @param $routeKey : 路由KEY
* @param $message :消息正文
* @param null $arguments 参数
* $arguments基本参数:content_type text/plain
* content_encoding NULL
* message_id NULL
* user_id NULL
* app_id NULL
* delivery_mode NULL //delivery_mode=2表示持久化
* priority NULL
* timestamp NULL
* expiration NULL
* type NULL
* reply_to
*
* @return bool
* @throws AMQPConnectionException
* @throws AMQPExchangeException
* @throws Exception
*/
public function amqp_publish($exchangeName, $queueName, $routeKey = 'iplanner', $message = '', $arguments = null){
//连接RabbitMQ
error_reporting(0);
if(!class_exists("AMQPConnection")){
return true;
}
$conn_args = array( 'host'=>$this->host , 'port'=> $this->port, 'login'=>$this->user , 'password'=> $this->pass,'vhost' =>$this->vhost);

$this->conn = new AMQPConnection($conn_args);

// $this->conn->connect();
if($this->conn->connect()){
//创建exchange名称和类型
$channel = new AMQPChannel($this->conn);
$ex = new AMQPExchange($channel);
$ex->setName($exchangeName);
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE); //AMQP_DURABLE | AMQP_AUTODELETE
try{
$ex->declare();
}catch(Exception $ex){
error_reporting(0);
}

//创建queue名称,使用exchange,绑定routingkey
$q = new AMQPQueue($channel);
$q->setName($queueName);
$q->setFlags(AMQP_DURABLE); //AMQP_DURABLE | AMQP_AUTODELETE
try{
$q->declare();
}catch(Exception $ex){
error_reporting(0);
}

$q->bind($exchangeName, $routeKey);
//消息发布
$channel->startTransaction();
$this->arguments['headers'] = $arguments;
$ex->publish($message, $routeKey, AMQP_MANDATORY, $this->arguments); //第三个参数可选:AMQP_MANDATORY,AMQP_IMMEDIATE
$channel->commitTransaction();
if (!$this->conn->disconnect()) {
throw new Exception('Could not disconnect');
}
}

}

public function __destruct()
{
// $this->conn->disconnect();
}


/**
* 注册成功后提醒用户邀请用户
* @author patrick wang
* */
public function send_tips_invite_user($company_id, $user_id, $ip, $subject, $context = FALSE)
{
$msg = new stdClass();
$msg->company_id = $company_id;
$msg->user_id = $user_id;
$msg->type = 'tip_invite_user';
$msg->subject = $subject;
$msg->message = $context;
$msg->ip = $ip;

$this->amqp_publish('iplanner.e.supply', 'iplanner.supply', 'iplanner.key.supply', json_encode($msg));
}

//接收
public function receive($exchangeName, $queueName, $routeKey = 'iplanner'){


//连接RabbitMQ
error_reporting(0);
if(!class_exists("AMQPConnection")){
return true;
}
$conn_args = array( 'host'=>$this->host , 'port'=> $this->port, 'login'=>$this->user , 'password'=> $this->pass,'vhost' =>$this->vhost);
$this->conn = new AMQPConnection($conn_args);

if($this->conn->connect()){
$channel = new AMQPChannel($this->conn);
$ex = new AMQPExchange($channel);
$ex->setName($exchangeName);
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE); //AMQP_DURABLE | AMQP_AUTODELETE
//echo 'Exchange Status: ' . $ex->declareExchange() . "\n";
//创建queue名称,使用exchange,绑定routingkey
$q = new AMQPQueue($channel);
$q->setName($queueName);
$q->setFlags(AMQP_DURABLE); //AMQP_DURABLE | AMQP_AUTODELETE
//echo 'Message Total: ' . $q->declareQueue() . "\n";

$q->bind($exchangeName, $routeKey);
//var_dump("Waiting for message...");
while(True) {
$q->consume(function ($envelope,$queue){
$msg = $envelope->getBody();
if(isset($msg) && $msg > 0) {
$close = new db_gl_platform();
$s = $close->close_accounts();
echo $s;
}
$queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
});
}
}
if (!$this->conn->disconnect()) {
throw new Exception('Could not disconnect');
}
}

}
?>


生产者调用
$mq_service = new AMQPService();
$mq_service->amqp_publish('ddd', 'ddd','ddd', $change_id);
$logger = get_logger('push');
$logger->debug('推送成功:'.$change_id);

消费者调用
$mq_service = new AMQPService();
$mq_service->receive('ddd', 'ddd','ddd');

posted @ 2021-05-26 11:07  zda龙  阅读(290)  评论(0编辑  收藏  举报