封装类如下:

<?php
/*
* amqp协议操作类,可以访问rabbitMQ
* 需先安装php_amqp扩展
*/
class RabbitMQCommand{ public $configs = array();
//交换机名称
public $exchange_name = '';
//队列名称
public $queue_name = '';
//路由名称
public $route_key = '';
/*
* 持久化,默认True
*/
public $durable = True;
/*
* 自动删除
* exchange is deleted when all queues have finished using it
* queue is deleted when last consumer unsubscribes
*
*/
public $autodelete = False;
/*
* 镜像
* 镜像队列,打开后消息会在节点之间复制,有master和slave的概念
*/
public $mirror = False; private $_conn = Null;
private $_exchange = Null;
private $_channel = Null;
private $_queue = Null; /*
* @configs array('host'=>$host,'port'=>5672,'username'=>$username,'password'=>$password,'vhost'=>'/')
*/ public function __construct($configs = array(), $exchange_name = '', $queue_name = '', $route_key = '') {
$this->setConfigs($configs);
$this->exchange_name = $exchange_name;
$this->queue_name = $queue_name;
$this->route_key = $route_key;
} private function setConfigs($configs) {
if (!is_array($configs)) {
throw new Exception('configs is not array');
}
if (!($configs['host'] && $configs['port'] && $configs['username'] && $configs['password'])) {
throw new Exception('configs is empty');
}
if (empty($configs['vhost'])) {
$configs['vhost'] = '/';
}
$configs['login'] = $configs['username'];
unset($configs['username']);
$this->configs = $configs;
} /*
* 设置是否持久化,默认为True
*/ public function setDurable($durable) {
$this->durable = $durable;
} /*
* 设置是否自动删除
*/ public function setAutoDelete($autodelete) {
$this->autodelete = $autodelete;
}
/*
* 设置是否镜像
*/
public function setMirror($mirror) {
$this->mirror = $mirror;
} /*
* 打开amqp连接
*/ private function open() {
if (!$this->_conn) {
try {
$this->_conn = new AMQPConnection($this->configs);
$this->_conn->connect();
$this->initConnection();
} catch (AMQPConnectionException $ex) {
throw new Exception('cannot connection rabbitmq',500);
}
}
} /*
* rabbitmq连接不变
* 重置交换机,队列,路由等配置
*/ public function reset($exchange_name, $queue_name, $route_key) {
$this->exchange_name = $exchange_name;
$this->queue_name = $queue_name;
$this->route_key = $route_key;
$this->initConnection();
} /*
* 初始化rabbit连接的相关配置
*/ private function initConnection() {
if (empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)) {
throw new Exception('rabbitmq exchange_name or queue_name or route_key is empty',500);
}
$this->_channel = new AMQPChannel($this->_conn);
$this->_exchange = new AMQPExchange($this->_channel);
$this->_exchange->setName($this->exchange_name); $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);
if ($this->durable)
$this->_exchange->setFlags(AMQP_DURABLE);
if ($this->autodelete)
$this->_exchange->setFlags(AMQP_AUTODELETE);
$this->_exchange->declare(); $this->_queue = new AMQPQueue($this->_channel);
$this->_queue->setName($this->queue_name);
if ($this->durable)
$this->_queue->setFlags(AMQP_DURABLE);
if ($this->autodelete)
$this->_queue->setFlags(AMQP_AUTODELETE);
if ($this->mirror)
$this->_queue->setArgument('x-ha-policy', 'all');
$this->_queue->declare(); $this->_queue->bind($this->exchange_name, $this->route_key);
} public function close() {
if ($this->_conn) {
$this->_conn->disconnect();
}
} public function __sleep() {
$this->close();
return array_keys(get_object_vars($this));
} public function __destruct() {
$this->close();
} /*
* 生产者发送消息
*/
public function send($msg) {
$this->open();
if(is_array($msg)){
$msg = json_encode($msg);
}else{
$msg = trim(strval($msg));
}
return $this->_exchange->publish($msg, $this->route_key);
}
/*
* 消费者
* $fun_name = array($classobj,$function) or function name string
* $autoack 是否自动应答
*
* function processMessage($envelope, $queue) {
$msg = $envelope->getBody();
echo $msg."\n"; //处理消息
$queue->ack($envelope->getDeliveryTag());//手动应答
}
*/
public function run($fun_name, $autoack = True){
$this->open();
if (!$fun_name || !$this->_queue) return False;
while(True){
if ($autoack) $this->_queue->consume($fun_name, AMQP_AUTOACK);
else $this->_queue->consume($fun_name);
}
} }

生产者代码:

<?php
set_time_limit(0);
include_once('RabbitMQCommand.php'); $configs = array('host'=>'192.168.0.156','port'=>5672,'username'=>'xp','password'=>'xp','vhost'=>'/');
$exchange_name = 'class-e-1';
$queue_name = 'class-q-1';
$route_key = 'class-r-1';
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);
for($i=0;$i<=10000000;$i++){
$ra->send(date('Y-m-d H:i:s',time()));
}
exit();

  

消费者代码:

<?php
error_reporting(0);
include_once('RabbitMQCommand.php'); $configs = array('host'=>'192.168.0.156','port'=>5672,'username'=>'xp','password'=>'xp','vhost'=>'/');
$exchange_name = 'class-e-1';
$queue_name = 'class-q-1';
$route_key = 'class-r-1';
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key); class A{
function processMessage($envelope, $queue) {
$msg = $envelope->getBody();
$envelopeID = $envelope->getDeliveryTag();
$pid = posix_getpid();
file_put_contents("/app/bossadmin/log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
$queue->ack($envelopeID);
}
}
$a = new A(); $s = $ra->run(array($a,'processMessage'),false);

测试结果:

开了6个生产者,6个消费者,生产6000W条数据,执行了4个小时,消费者基本即时处理完毕

TOP:

总结:

RabbitMQ在PHP消费端请求连接后,如果有消息会主动轮询各个消费端,这让php作为守护进程的性能还可以。实际运行较长的时间,cpu内存等数据也都还可以。

而httpsqs需要消费端不断去请求httpsqs服务,守护进程的性能损耗就比较高。

RabbitMQ PHP操作类,守护进程及相关测试数据的更多相关文章

  1. linux 创建守护进程的相关知识

    linux 创建守护进程的相关知识 http://www.114390.com/article/46410.htm linux 创建守护进程的相关知识,这篇文章主要介绍了linux 创建守护进程的相关 ...

  2. day36-进程操作实例,守护进程,方法,属性

    #1.server端跟多个client端聊天: #异步操作,主进程负责接收client的连接,子进程负责跟client聊天. #每接收一个连接,就创建一个子进程,子进程之间的数据是隔离的,互不影响,所 ...

  3. Linux守护进程详解(init.d和xinetd) [转]

    一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台 的守护进程来执行的 ...

  4. Linux守护进程详解(init.d和xinetd)

    一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程来执行的. ...

  5. linux守护进程编写实践

    主要参考:http://colding.bokee.com/5277082.html (实例程序是参考这的) http://wbwk2005.blog.51cto.com/2215231/400260 ...

  6. 多进程编程之守护进程Daemonize

    1.守护进程 守护进程(daemon)是一类在后台运行的特殊进程,用于执行特定的系统任务.很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭.另一些只在需要的时候才启动,完成任务后就自动结束. ...

  7. Linux 守护进程和超级守护进程(xinetd)

    一 .Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程来执行的 ...

  8. PHP7 网络编程(二)daemon守护进程

    前言 在一个多任务的计算机操作系统中,守护进程(英语:daemon,/ˈdiːmən/或/ˈdeɪmən/)是一种在后台执行的计算机程序.此类程序会被以进程的形式初始化.守护进程程序的名称通常以字母“ ...

  9. php rabbitmq操作类及生产者和消费者实例代码 转

    注意事项: 1.accept.php消费者代码需要在命令行执行 2.'username'=>'asdf','password'=>'123456' 改成自己的帐号和密码 RabbitMQC ...

随机推荐

  1. myeclipse中使用gradle开发项目

    gradle可以直接使用maven的代码库,并且支持编程,可以说是maven的加强版.今天我们学习下,如何在MyEclipse下使用gradle开发项目.我们的开发环境:myeclipse 2015, ...

  2. android系统架构图

    android的系统架构和其操作系统一样,采用了分层的架构.从架构图看,android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和Linux核心层. 1.应用程序 Andr ...

  3. 【05】了解C++默默编写并调用那些函数

    1.如果没有声明copy构造方法,copy赋值操作符,和析构方法,编译器会自动生成这些方法,且是inline. 2.如果没有声明任何构造方法,编译器会自动生成一个default构造方法,且是inlin ...

  4. vmtouch - the Virtual Memory Toucher

    https://hoytech.com/vmtouch/ [root@localhost ~]# git clone git://github.com/hoytech/vmtouch.git 正克隆到 ...

  5. mysql脚本mysql_safe解释、mysql.sock文件、mysql_install_db

    1.首先解释下,启动mysql时为何会调用mysql_safe脚本来启动mysql [root@localhost ~]# /etc/init.d/mysqld start 正在启动 mysqld: ...

  6. IE下onchange事件不立即执行

    做前端开发免不了为浏览器的兼容而劳神,所以坚持把发现的浏览器兼容问题做做总结,是很有意义的. 比如IE8及以下的浏览器的onchange事件实在该控件失去焦点之后才执行的,也就是要点一下空白的地方,才 ...

  7. jqery筛选

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. jQuery中的html,val,text区别

    在项目开发中,写jQuery代码有时候会搞混淆一下东西,现在写一下demo来列出jQuery的.html(),.text(),.val()的区别. 1. html()取得第一个匹配元素的内容,简单来说 ...

  9. Asp.Net MVC是否针对每次请求都重新创建一个控制器实例

    一.Asp.Net MVC是否针对每次请求都重新创建一个控制器实例 默认情况下,答案是确定的. ControllerBuilder类 ControllerBuilder.Current用户获取默认的控 ...

  10. VS2015 Cordova Ionic移动开发(一)

    一.Windows环境配置 1.如果已经安装VS2015,打开[工具]-[选项]找到Cordova选项: 运行依赖关系查看器,用来检测开发环境是否完整. 如果检测显示: 那么就是环境配置完成了.可以直 ...