Skip to content

Commit 98a655b

Browse files
committed
2 parents 20658f0 + fb07043 commit 98a655b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+12666
-76
lines changed

RabbitMQ/Lite.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
require_once __DIR__ . '/vendor/autoload.php';
4+
use PhpAmqpLib\Connection\AMQPStreamConnection;
5+
use PhpAmqpLib\Message\AMQPMessage;
6+
7+
8+
9+
class RabbitMQ_Lite{
10+
11+
private $connection;
12+
private $channel;
13+
14+
/**
15+
* RabbitMQ_Lite constructor.
16+
* 建立连接
17+
* @param $config
18+
*/
19+
function __construct(array $config) {
20+
$this->connection = new AMQPStreamConnection($config['host'], $config['port'], $config['user'], $config['password'], $config['vhost']);
21+
$this->channel = $this->connection->channel();
22+
23+
}
24+
25+
/**
26+
* 消息入队列
27+
* @param mixed $msg 消息
28+
* @param string $queue 队列名称
29+
*/
30+
function push($msg, $queue){
31+
$msg = serialize($msg);
32+
$message = new AMQPMessage($msg, array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
33+
$this->channel->basic_publish($message, '', $queue);
34+
}
35+
36+
/**
37+
* 消息出队列
38+
* @param string $queue 队列名称
39+
* @param $callback 处理消息的回调函数
40+
*/
41+
function pop($queue, $callback){
42+
$this->channel->basic_qos(null, 1, null);
43+
$this->channel->basic_consume($queue, '', false, false, false, false, $callback);
44+
45+
while(count($this->channel->callbacks)) {
46+
$this->channel->wait();
47+
}
48+
}
49+
50+
/**
51+
* 声明队列,不存在会去创建
52+
*/
53+
public function queue_declare($queue){
54+
$this->channel->queue_declare($queue, false, true, false, false);
55+
}
56+
57+
/**
58+
* 关闭连接
59+
*/
60+
function close(){
61+
$this->channel->close();
62+
$this->connection->close();
63+
}
64+
}
65+

RabbitMQ/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# PhalApi-RabbitMQ基于PhalApi专业队列拓展
2+
3+
![](http://webtools.qiniudn.com/master-LOGO-20150410_50.jpg)
4+
5+
## 前言
6+
7+
RabbitMQ一直都是队列中的标杆,这次有幸PhalApi也能啃上RabbitMQ真是一件大好事,感谢**@牧鱼人**提供基于php-amqplib/php-amqplib封装的PhalApi-RabbitMQ扩展
8+
9+
**关于RabbitMQ相关的安装集群配置可以参考笔者博客的MQ模块**
10+
11+
附上:
12+
13+
官网地址:[http://www.phalapi.net/](http://www.phalapi.net/ "PhalApi官网")
14+
15+
开源中国Git地址:[http://git.oschina.net/dogstar/PhalApi/tree/release](http://git.oschina.net/dogstar/PhalApi/tree/release "开源中国Git地址")
16+
17+
开源中国拓展Git地址:[http://git.oschina.net/dogstar/PhalApi-Library](http://git.oschina.net/dogstar/PhalApi-Library "开源中国Git地址")
18+
19+
20+
## 1.安装
21+
22+
使用PhalApi-RabbitMQ扩展和使用其他扩展也是一样简单,只需要把目录存放到Library即可进行使用
23+
24+
在Config中创建文件rabbitmq.php配置文件格式如下:
25+
26+
```
27+
return array(
28+
'servers' => array(
29+
'host' => '127.0.0.1',
30+
'port' => '5672',
31+
'user' => 'admin',
32+
'password' => 'admin',
33+
'vhost' => '/',
34+
)
35+
)
36+
```
37+
38+
## 2.使用RabbitMQ写入和处理消息
39+
40+
然后就可以进行实例化使用了:
41+
42+
```
43+
// 实例化RabbitMQ实例
44+
$rm = RabbitMQ_Lite(DI()->config->get('rabbitmq.servers'));
45+
46+
// 检查test队列是否存在,如果不存在则创建,频繁调用会带来较大性能消耗
47+
// 建议在出队列脚本处进行调用,写入队列不进行调用
48+
$rm->queue_declare("test");
49+
50+
// 向队列写入一条消息
51+
$rm->push("测试消息","test");
52+
53+
// 定义处理消息的方法
54+
$func = function ($msg){
55+
echo $msg;
56+
};
57+
// 处理任务(会阻塞进行)
58+
$rm->pop("test",$func);
59+
60+
```
61+
62+
63+
注:笔者能力有限有说的不对的地方希望大家能够指出,也希望多多交流!
64+
65+
**官网QQ交流群:①群:421032344 ②群:459352221 欢迎大家的加入!**

RabbitMQ/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"php-amqplib/php-amqplib": "2.6.*"
4+
}
5+
}

RabbitMQ/composer.lock

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RabbitMQ/vendor/autoload.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer' . '/autoload_real.php';
6+
7+
return ComposerAutoloaderInit291f5189b6fe349844454037aa66032b::getLoader();

0 commit comments

Comments
 (0)