封装类如下:

  1. <?php
  2. /*
  3. * amqp协议操作类,可以访问rabbitMQ
  4. * 需先安装php_amqp扩展
  5. */
  6. class RabbitMQCommand{
  7.  
  8. public $configs = array();
  9. //交换机名称
  10. public $exchange_name = '';
  11. //队列名称
  12. public $queue_name = '';
  13. //路由名称
  14. public $route_key = '';
  15. /*
  16. * 持久化,默认True
  17. */
  18. public $durable = True;
  19. /*
  20. * 自动删除
  21. * exchange is deleted when all queues have finished using it
  22. * queue is deleted when last consumer unsubscribes
  23. *
  24. */
  25. public $autodelete = False;
  26. /*
  27. * 镜像
  28. * 镜像队列,打开后消息会在节点之间复制,有master和slave的概念
  29. */
  30. public $mirror = False;
  31.  
  32. private $_conn = Null;
  33. private $_exchange = Null;
  34. private $_channel = Null;
  35. private $_queue = Null;
  36.  
  37. /*
  38. * @configs array('host'=>$host,'port'=>5672,'username'=>$username,'password'=>$password,'vhost'=>'/')
  39. */
  40.  
  41. public function __construct($configs = array(), $exchange_name = '', $queue_name = '', $route_key = '') {
  42. $this->setConfigs($configs);
  43. $this->exchange_name = $exchange_name;
  44. $this->queue_name = $queue_name;
  45. $this->route_key = $route_key;
  46. }
  47.  
  48. private function setConfigs($configs) {
  49. if (!is_array($configs)) {
  50. throw new Exception('configs is not array');
  51. }
  52. if (!($configs['host'] && $configs['port'] && $configs['username'] && $configs['password'])) {
  53. throw new Exception('configs is empty');
  54. }
  55. if (empty($configs['vhost'])) {
  56. $configs['vhost'] = '/';
  57. }
  58. $configs['login'] = $configs['username'];
  59. unset($configs['username']);
  60. $this->configs = $configs;
  61. }
  62.  
  63. /*
  64. * 设置是否持久化,默认为True
  65. */
  66.  
  67. public function setDurable($durable) {
  68. $this->durable = $durable;
  69. }
  70.  
  71. /*
  72. * 设置是否自动删除
  73. */
  74.  
  75. public function setAutoDelete($autodelete) {
  76. $this->autodelete = $autodelete;
  77. }
  78. /*
  79. * 设置是否镜像
  80. */
  81. public function setMirror($mirror) {
  82. $this->mirror = $mirror;
  83. }
  84.  
  85. /*
  86. * 打开amqp连接
  87. */
  88.  
  89. private function open() {
  90. if (!$this->_conn) {
  91. try {
  92. $this->_conn = new AMQPConnection($this->configs);
  93. $this->_conn->connect();
  94. $this->initConnection();
  95. } catch (AMQPConnectionException $ex) {
  96. throw new Exception('cannot connection rabbitmq',500);
  97. }
  98. }
  99. }
  100.  
  101. /*
  102. * rabbitmq连接不变
  103. * 重置交换机,队列,路由等配置
  104. */
  105.  
  106. public function reset($exchange_name, $queue_name, $route_key) {
  107. $this->exchange_name = $exchange_name;
  108. $this->queue_name = $queue_name;
  109. $this->route_key = $route_key;
  110. $this->initConnection();
  111. }
  112.  
  113. /*
  114. * 初始化rabbit连接的相关配置
  115. */
  116.  
  117. private function initConnection() {
  118. if (empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)) {
  119. throw new Exception('rabbitmq exchange_name or queue_name or route_key is empty',500);
  120. }
  121. $this->_channel = new AMQPChannel($this->_conn);
  122. $this->_exchange = new AMQPExchange($this->_channel);
  123. $this->_exchange->setName($this->exchange_name);
  124.  
  125. $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);
  126. if ($this->durable)
  127. $this->_exchange->setFlags(AMQP_DURABLE);
  128. if ($this->autodelete)
  129. $this->_exchange->setFlags(AMQP_AUTODELETE);
  130. $this->_exchange->declare();
  131.  
  132. $this->_queue = new AMQPQueue($this->_channel);
  133. $this->_queue->setName($this->queue_name);
  134. if ($this->durable)
  135. $this->_queue->setFlags(AMQP_DURABLE);
  136. if ($this->autodelete)
  137. $this->_queue->setFlags(AMQP_AUTODELETE);
  138. if ($this->mirror)
  139. $this->_queue->setArgument('x-ha-policy', 'all');
  140. $this->_queue->declare();
  141.  
  142. $this->_queue->bind($this->exchange_name, $this->route_key);
  143. }
  144.  
  145. public function close() {
  146. if ($this->_conn) {
  147. $this->_conn->disconnect();
  148. }
  149. }
  150.  
  151. public function __sleep() {
  152. $this->close();
  153. return array_keys(get_object_vars($this));
  154. }
  155.  
  156. public function __destruct() {
  157. $this->close();
  158. }
  159.  
  160. /*
  161. * 生产者发送消息
  162. */
  163. public function send($msg) {
  164. $this->open();
  165. if(is_array($msg)){
  166. $msg = json_encode($msg);
  167. }else{
  168. $msg = trim(strval($msg));
  169. }
  170. return $this->_exchange->publish($msg, $this->route_key);
  171. }
  172. /*
  173. * 消费者
  174. * $fun_name = array($classobj,$function) or function name string
  175. * $autoack 是否自动应答
  176. *
  177. * function processMessage($envelope, $queue) {
  178. $msg = $envelope->getBody();
  179. echo $msg."\n"; //处理消息
  180. $queue->ack($envelope->getDeliveryTag());//手动应答
  181. }
  182. */
  183. public function run($fun_name, $autoack = True){
  184. $this->open();
  185. if (!$fun_name || !$this->_queue) return False;
  186. while(True){
  187. if ($autoack) $this->_queue->consume($fun_name, AMQP_AUTOACK);
  188. else $this->_queue->consume($fun_name);
  189. }
  190. }
  191.  
  192. }

生产者代码:

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

  

消费者代码:

  1. <?php
  2. error_reporting(0);
  3. include_once('RabbitMQCommand.php');
  4.  
  5. $configs = array('host'=>'192.168.0.156','port'=>5672,'username'=>'xp','password'=>'xp','vhost'=>'/');
  6. $exchange_name = 'class-e-1';
  7. $queue_name = 'class-q-1';
  8. $route_key = 'class-r-1';
  9. $ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);
  10.  
  11. class A{
  12. function processMessage($envelope, $queue) {
  13. $msg = $envelope->getBody();
  14. $envelopeID = $envelope->getDeliveryTag();
  15. $pid = posix_getpid();
  16. file_put_contents("/app/bossadmin/log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
  17. $queue->ack($envelopeID);
  18. }
  19. }
  20. $a = new A();
  21.  
  22. $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. 新建虚拟SAN

    在SCVMM中,在每台主机上新建虚拟SAN(所使用的FC适配器必须一起用NPIV,否则不可用),新建完成后,在其主机的Hyper-V管理器中也可以看到 每台主机上的虚拟SAN名称必须相同    之后就 ...

  2. mongodb地理位置索引

    初始化集合(经度在前,纬度在后) ? 1 2 3 mongos> db.checkins.insert({ "_id" : "101", "lo ...

  3. 【VBA研究】变量定义的类型和实际赋值类型

    作者:iamlaosong VBA中变量能够先定义后使用,也能够不定义直接使用.假设模块前面加了Option Explicit语句,则变量必须先定义后使用. 只是.实验发现.VBA对变量类型没有进行严 ...

  4. Could not find class &#39;****&#39;, referenced from method #####

    找不到类,多半也是和第三方的jar包有关. 将找不到的类.在下图中的地方勾选出来.假设jar太多.有的类有冲突的话,须要明白其先后顺序. 请外一篇和第三方jar有关的异常的文章. Conversion ...

  5. [linux]date命令时间戳和时间之间的转换

    非常多时候我们查看数据库的数据,或者是一些别人系统中的数据须要用时间戳来查询.或者查询出来的结果是个时间戳. 还有时候,查询条件须要输入时间戳. 我之前的办法就是用在线工具来完毕,后来用mac了.我觉 ...

  6. oracle db mos文章 翻译系列

    http://blog.csdn.net/msdnchina/article/details/38377125

  7. careercup-排序和查找 11.3

    11.3 给定一个排序后的数组,包含n个整数,但这个数组已被旋转很多次,次数不详.请编写代码找出数组中的某个元素.可以假定数组元素原先是按从小到大的顺序排序的. 解法: 可以直接从开始一个一个比较,也 ...

  8. 场景类(CCSence)

    场景与流程控制 在图2-1中,每一个节点中显示的内容相对不变.通常,我们把这些内容相对不变的游戏元素集合称作场景(scene),把游戏在场景之间切换的过程叫做流程控制(flow control). 在 ...

  9. Flex学习教程网站地址

    http://www.985school.com/flex/complex_controls.html

  10. air2调用本地exe的文章

    流传了两种配置app.xml的方法,分别是: <supportedProfiles>extendedDesktop</supportedProfiles>  <suppo ...