MQ.php

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: brady
  5. * Date: 2017/12/6
  6. * Time: 14:42
  7. *
  8. * amqp协议操作类,可以访问rabbitMQ
  9. * 需先安装php_amqp扩展
  10. *
  11. */
  12.  
  13. class MQ
  14. {
  15. //配置
  16. public $configs = array();
  17.  
  18. //交换机名称
  19. public $exchange_name = '';
  20.  
  21. //队列名称
  22. public $queue_name = '';
  23.  
  24. //路由名称 注意 如果用同一个路由绑定交换机,当推送的时候,会同时推送到这几个key上 $q = new AMQPQueue($channel); $q->setName('queue3'); $q->setName('queue2'); $q->bind('exchange',$routingkey);
  25. public $route_key = '';
  26.  
  27. //是否持久化 默认true
  28. public $durable = true;
  29.  
  30. /*
  31. * 是否自动删除
  32. * exchange is deleted when all queues have finished using it
  33. * queue is deleted when last consumer unsubscribes
  34. *
  35. */
  36. public $auto_delete = false;
  37.  
  38. //镜像队列,打开后消息会在节点之间复制,有master和slave的概念
  39. public $mirror = false;
  40.  
  41. //连接
  42. private $_conn = NULL;
  43. //交换机对象
  44. private $_exchange = NULL;
  45. //信道对象
  46. private $_channel = NULL;
  47. //队列对象
  48. private $_queue = NULL;
  49.  
  50. /**
  51. * MQ constructor.
  52. * @configs array('host'=>$host,'port'=>5672,'username'=>$username,'password'=>$password,'vhost'=>'/')
  53. */
  54. public function __construct($configs=array(),$exchange_name='',$queue_name='',$route_key='')
  55. {
  56. $this->exchange_name = $exchange_name;
  57. $this->queue_name = $queue_name;
  58. $this->route_key = $route_key;
  59.  
  60. $this->set_configs($configs);
  61. }
  62.  
  63. /**
  64. * @desc 配置设置
  65. * @param $configs
  66. */
  67. public function set_configs($configs)
  68. {
  69. if(empty($configs) || !is_array($configs)){
  70. throw new Exception("your config is not array");
  71. }
  72.  
  73. if(empty($configs['host']) || empty($configs['username']) || empty($configs['password'])) {
  74. throw new Exception("your config is error");
  75. }
  76.  
  77. if(empty($configs['vhost'])){
  78. $configs['vhost'] = '/';
  79. }
  80.  
  81. if(empty($configs['port'])){
  82. $configs['port'] = '5672';
  83. }
  84.  
  85. $configs['login'] = $configs['username'];
  86. unset($configs['username']);
  87.  
  88. $this->configs = $configs;
  89.  
  90. }
  91.  
  92. /**
  93. * 设置是否持久化
  94. * @param $durable
  95. */
  96. public function set_durable($durable)
  97. {
  98. $this->durable = $durable;
  99. }
  100.  
  101. /**
  102. * 设置是否自动删除
  103. * @param $auto_delete boolean
  104. */
  105. public function set_auto_delete($auto_delete)
  106. {
  107. $this->auto_delete = $auto_delete;
  108. }
  109.  
  110. /**
  111. * 设置是否镜像
  112. * @param $mirror
  113. */
  114. public function set_mirror($mirror)
  115. {
  116. $this->mirror = $mirror;
  117. }
  118.  
  119. /**
  120. * 连接初始化
  121. */
  122. public function init()
  123. {
  124. //没有连接对象,进行连接 有不管 就不用每次都连接和初始化
  125. if(!$this->_conn){
  126. $this->_conn = new AMQPConnection($this->configs);
  127. $this->_conn->connect();
  128. $this->init_exchange_queue_route();
  129. }
  130. }
  131.  
  132. /**
  133. * 初始化 交换机 队列名 路由
  134. */
  135. public function init_exchange_queue_route()
  136. {
  137. if(empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)){
  138. throw new Exception("rabbitMQ exchage_name or queue_name or route_key is empty, please check is",'500');
  139. }
  140.  
  141. //channel
  142. $this->_channel = new AMQPChannel($this->_conn);//创建channel
  143.  
  144. //exchange
  145. $this->_exchange = new AMQPExchange($this->_channel);//创建交换机
  146. $this->_exchange->setName($this->exchange_name);//设置交换机名字
  147. $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);//交换机方式为direct
  148. if($this->durable) {
  149. $this->_exchange->setFlags(AMQP_DURABLE);//是否持久化
  150. }
  151. if($this->auto_delete){
  152. $this->_exchange->setFlags(AMQP_AUTODELETE);//是否自动删除
  153. }
  154. $this->_exchange->declareExchange();//申请交换机
  155.  
  156. //queue
  157. $this->_queue = new AMQPQueue($this->_channel);
  158. $this->_queue->setName($this->queue_name);
  159. if($this->durable){
  160. $this->_queue->setFlags(AMQP_DURABLE);
  161. }
  162. if($this->auto_delete){
  163. $this->_queue->setFlags(AMQP_AUTODELETE);
  164. }
  165. if($this->mirror){
  166. $this->_queue->setArgument('x-ha-policy','all');
  167. }
  168. $this->_queue->declareQueue();//申请queue
  169.  
  170. //绑定交换机
  171. $this->_queue->bind($this->exchange_name,$this->route_key);
  172. }
  173.  
  174. //关闭连接
  175. public function close()
  176. {
  177. if($this->_conn){
  178. $this->_conn->disconnect();
  179. }
  180. }
  181.  
  182. //断开连接
  183. public function __destruct()
  184. {
  185. // TODO: Implement __destruct() method.
  186. $this->close();
  187. }
  188.  
  189. //生产消息
  190. public function send($msg)
  191. {
  192. $this->init();
  193. if(!$this->_conn){
  194. throw new Exception("connect RabbitMQ failed when send message");
  195. }
  196.  
  197. if(is_array($msg)) {
  198. $msg = json_encode($msg);
  199. } else {
  200. $msg = trim(strval($msg));
  201. }
  202.  
  203. return $this->_exchange->publish($msg,$this->route_key);
  204. }
  205.  
  206. //消费消息 自动应答模式
  207. public function run_auto($funcation_name,$auto_ack = true)
  208. {
  209. $this->init();
  210. if(!$funcation_name || !$this->_queue) {
  211. throw new Exception("auto ack lose function_name or this->_queue");
  212. }
  213. while(true){
  214. if($auto_ack){
  215. $this->_queue->consume($funcation_name,AMQP_AUTOACK);
  216. } else {
  217. $this->_queue->consume($funcation_name);
  218. }
  219. }
  220.  
  221. }
  222.  
  223. }

  

my_pub.php

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: brady
  5. * Date: 2017/12/6
  6. * Time: 14:35
  7. */
  8.  
  9. require_once "MQ.php";
  10.  
  11. $configs = array(
  12. 'host'=>'192.168.33.30',
  13. 'port'=>5672,
  14. 'username'=>'title',
  15. 'password'=>'title',
  16. 'vhost'=>'/'
  17. );
  18. $number = 2;
  19. $config = [
  20. 'exchange_name'=>'brady',
  21. 'queue_name'=>"queue_".$number,
  22. 'route_key'=>"route_".$number
  23. ];
  24.  
  25. $mq = new MQ($configs,$config['exchange_name'],$config['queue_name'],$config['route_key']);
  26.  
  27. for($i=0;$i<10000;$i++){
  28. $res = $mq->send(date("Y-m-d H:i:s"));
  29. }

  my_consumer.php

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: brady
  5. * Date: 2017/12/6
  6. * Time: 19:41
  7. */
  8. require_once "MQ.php";
  9. set_time_limit(0);
  10. $configs = array(
  11. 'host'=>'192.168.33.30',
  12. 'port'=>5672,
  13. 'username'=>'title',
  14. 'password'=>'title',
  15. 'vhost'=>'/'
  16. );
  17. $number = 2;
  18. $config = [
  19. 'exchange_name'=>'brady',
  20. 'queue_name'=>"queue_".$number,
  21. 'route_key'=>"route_".$number
  22. ];
  23.  
  24. $mq = new MQ($configs,$config['exchange_name'],$config['queue_name'],$config['route_key']);
  25.  
  26. //用类的方式
  27. /*class A{
  28. function processMessage($envelope, $queue) {
  29. $msg = $envelope->getBody();
  30. $envelopeID = $envelope->getDeliveryTag();
  31. $pid = posix_getpid();
  32. file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
  33. $queue->ack($envelopeID);
  34. }
  35. }
  36. $a = new A();*/
  37.  
  38. class B{
  39. protected $_envelope;
  40. protected $_queue;
  41. public function __construct($envelope,$queue)
  42. {
  43. $this->_queue = $queue;
  44. $this->_envelope = $envelope;
  45.  
  46. }
  47.  
  48. public function test()
  49. {
  50. $msg = $this->_envelope->getBody();
  51. $envelopeID = $this->_envelope->getDeliveryTag();
  52. $pid = posix_getpid();
  53. file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
  54. $this->_queue->ack($envelopeID);
  55.  
  56. }
  57. }
  58.  
  59. //用函数的方式 直接在回调里面处理,也可以在回调里面,再load新的model 事务在model里面处理
  60. function processMessage($envelope, $queue) {
  61.  
  62. /* $msg = $envelope->getBody();
  63. $envelopeID = $envelope->getDeliveryTag();
  64. $pid = posix_getpid();
  65. file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
  66. $queue->ack($envelopeID); //如果设置为AMQP_AUTOACK 那么不需要该行也可以自动应答*/
  67.  
  68. $b = new B($envelope,$queue);
  69. $b->test();
  70. }
  71.  
  72. //$s = $ra->run(array($a,'processMessage'),false);
  73. $s = $mq->run_auto("processMessage",false);

  ci里面回调函数可以传对象 比如$this  $this->tb_users 或者赋值给一个变量后传

  1. public function test_get()
  2. {
  3. $obj = $this;
  4. set_time_limit(0);
  5. require_once APPPATH."/third_party/mq/RabbitMQ.php";
  6.  
  7. $configs = array(
  8. 'host'=>'192.168.33.30',
  9. 'port'=>5672,
  10. 'username'=>'title',
  11. 'password'=>'title',
  12. 'vhost'=>'/'
  13. );
  14. $number = 2;
  15. $config = [
  16. 'exchange_name'=>'brady',
  17. 'queue_name'=>"queue_".$number,
  18. 'route_key'=>"route_".$number
  19. ];
  20. $ra = new RabbitMQ($configs,$config['exchange_name'],$config['queue_name'],$config['route_key']);
  21. $ra->run(array($obj,'processMessage'),false);
  22. }

  后面再写篇用get手动获取和响应的ack

mq类----1的更多相关文章

  1. mq类----2

    手动应答方式 使用get my_consumer.php 消费者  生产者和上一篇 一样 <?php /** * Created by PhpStorm. * User: brady * Dat ...

  2. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  3. MSMQ消息队列 用法

    引言 接下来的三篇文章是讨论有关企业分布式开发的文章,这三篇文章筹划了很长时间,文章的技术并不算新,但是文章中使用到的技术都是经过笔者研究实践后总结的,正所谓站在巨人的肩膀上,笔者并不是巨人,但也希望 ...

  4. 企业数据总线(ESB)和注册服务管理(dubbo)的区别

    企业数据总线(ESB)和注册服务管理(dubbo)的区别 转载 2015年11月04日 09:05:14 7607  企业数据总线(ESB)和注册服务管理(dubbo)的区别 2015-03-09 0 ...

  5. Hadoop/Spark生态圈里的新气象

    令人惊讶的是,Hadoop在短短一年的时间里被重新定义.让我们看看这个火爆生态圈的所有主要部分,以及它们各自具有的意义. 对于Hadoop你需要了解的最重要的事情就是 ,它不再是原来的Hadoop. ...

  6. 电商技术中企业数据总线ESB和注册服务管理的区别

    一.概述 1.什么是ESB 就是企业数据总线的意思,他的核心功能就是兼容各种协议接口,可以将数据在各种协议之间进行流转,并且可以针对数据格式进行编排转换. 异构系统,功能繁多,复杂 代表性的项目有:J ...

  7. 【编码】封装RedisPubSub工具

    基本介绍 核心原理:利用Redis的List列表实现,发布事件对应rpush,订阅事件对应lpop 问题一:Redis不是自带Pub/Sub吗? redis自带的pub/sub有两个问题: 1.如果发 ...

  8. RabbitMQ,想说爱你不容易(附详细安装教程)

    前言 本文讲述的只是主要是 RabbitMQ 的入门知识,学习本文主要可以掌握以下知识点: MQ 的发展史 AMQP 协议 Rabbit MQ 的安装 Rabbit MQ 在 Java API 中的使 ...

  9. IBM WebSphere MQ的C#工具类以及源码(net)

    简单的介绍一下MQ常用的对象 Queue Manager 队列管理器 主要负责管理队列.通道等,类似与Oracle中的Oracle实例的概念,在一台服务器中可以定义多个Queue Manager. Q ...

随机推荐

  1. Spring.Net 能为我们做点什么

    本文内容 概述 背景 模块 使用场景 入门应用 Spring.NET 相关项目 本文正式开始前,我以目前所能想到的.此时此刻能想到的,先简单说下,为什么会有像 Spring.Net 这样的东西.首先, ...

  2. 洛谷 P1345 [USACO5.4]奶牛的电信Telecowmunication

    题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...

  3. 随记:UWP开发中怎么使当前页面拓展到标题栏

    public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); CoreAp ...

  4. itextsharp-5.2.1-修正无法签名大文件问题

    PDF文件格式几乎是所有开发平台或者业务系统都热爱的一种文档格式. 目前有很多优秀的开源PDF组件和类库.主要平时是使用.NET和Java开发,所以比较偏好使用iText,当然,它本身就很强大.iTe ...

  5. Unity3D中使用Projector生成阴影

    在Unity3D中使用Projector实现动态阴影 无意中看见一篇博客叙述使用Projector实现动态阴影可以在移动平台拥有非常好的性能,遂按照其想法实现了一遍,发现其中竟有许多细节,写下这篇博客 ...

  6. 在DataGridView控件中启用换行

    实现效果: 知识运用: DataGridView控件公共属性DefaultCellStyle的WrapMode属性 public DataGridViewTriState WrapMode {  ge ...

  7. 剑指offer64 数据流中的中位数

    priority_queue优先级队列,他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为数据类型, Conta ...

  8. SQL Server数据库字段类型说明

    SQL Server数据库字段类型说明 目前Sql Server 数据库一共有X个字段类型,大体分为9类,分别是字符串类型.二进制码字符串数据类型.Unincode字符串数据.整数类型.精确数据类型. ...

  9. 【转】MFC右键显示菜单之LoadMenu()

    如何在界面内单击右键弹出自己设置的菜单选项? 步骤如下: 1.在资源MENU里添加一个菜单资源,命名为IDR_POP_MENU. 2.在自己添加的菜单中添加事件,如事件1,事件2,事件3,分别添加响应 ...

  10. Angular - angularjs2 一些报错的概览(数据为json格式)

    {"Unterminated string literal.": "未终止的字符串文本.","Identifier expected.": ...