1、apt-get install rabbitmq-server

2、service rabbitmq-server restart

3、wget https://github.com/alanxz/rabbitmq-c/tarball/0.2

4、 tar zxf 0.2

5、wget https://github.com/rabbitmq/rabbitmq-codegen/tarball/master

6、tar zxf master

7、mv rabbitmq-rabbitmq-codegen-0a95a69/ alanxz-rabbitmq-c-f8f4fc7/codegen

8、cd alanxz-rabbitmq-c-f8f4fc7/

9、autoreconf -i && ./configure && make && make install

10、wget http://pecl.php.net/get/amqp-1.0.4.tgz

11、tar zxf amqp-1.0.4.tgz

12、/usr/bin/phpize5

13、./configure --with-php-config=/usr/bin/php-config5 --with-amqp;

14、make && make install

15、vim /etc/php5/apache2/php.ini

16、extension = "amqp.so"

17、service apache2 restart

命令行配置php.ini,不然命令行找不到AMQPConnection类

vim /etc/php5/cli/php.ini

extension = "amqp.so"


浏览器访问地址:

http://localhost:55672

http://localhost:55672/api/

PORT 4369: Erlang makes use of a Port Mapper Daemon (epmd) for resolution of node names in a cluster. Nodes must be able to reach each other and the port mapper daemon for clustering to work.

PORT 35197set by inet_dist_listen_min/max Firewalls must permit traffic in this range to pass between clustered nodes

PORT 55672RMQ Management console

PORT 5672RMQ main port.


添加用户:

rabbitmqctl add_user rainbird password

添加权限:

rabbitmqctl set_permissions -p "/" rainbird ".*" ".*" ".*"

删除测试用户:

rabbitmqctl delete_user guest


所有指令列表(很简单的英文):

add_user

delete_user

change_password

list_users

add_vhost

delete_vhost

list_vhosts

set_permissions [-p ]

clear_permissions [-p ]

list_permissions [-p ]

list_user_permissions

list_queues [-p ] [ ...]

list_exchanges [-p ] [ ...]

list_bindings [-p ]

list_connections [ ...]


测试代码:

入消息队列

ini_set('display_errors', 1);
//连接RabbitMQ
$conn_args = array( 'host'=>'localhost' , 'port'=> '5672', 'login'=>'guest' , 'password'=> 'guest','vhost' =>'/');
$conn = new AMQPConnection($conn_args);
$conn->connect();
//创建exchange名称和类型
$channel = new AMQPChannel($conn);
$ex = new AMQPExchange($channel);
$ex->setName('exchange_chenhw');
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$ex->declare();
//创建queue名称,使用exchange,绑定routingkey
$q = new AMQPQueue($channel);
$q->setName('queue_chen');
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$q->declare();
$q->bind('exchange_chenhw', 'routingkey_chenhw');
//消息发布
$channel->startTransaction();
$message = "INSERT INTO `member`(`name`, `sex`) values('chenhw".rand(0,999)."', 1)";
echo $message;
$ex->publish($message, 'routingkey_chenhw');
$channel->commitTransaction();
$conn->disconnect();
var_dump('succ');
?>

出消息队列

require_once('mysql.php');
//连接RabbitMQ
$conn_args = array( 'host'=>'localhost' , 'port'=> '5672', 'login'=>'guest' , 'password'=> 'guest','vhost' =>'/');
$conn = new AMQPConnection($conn_args);
$conn->connect();
//设置queue名称,使用exchange,绑定routingkey
$channel = new AMQPChannel($conn);
$q = new AMQPQueue($channel);
$q->setName('queue_chenhw');
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$q->declare();
$q->bind('exchange_chenhw', 'routingkey_chenhw');
//消息获取
while(true) {
$messages = $q->get(AMQP_AUTOACK) ;
if ($messages){
$sql = $messages->getBody();
mysql_query($sql, $db);
}
sleep(1);
}
$conn->disconnect();
mysql_close($db);
var_dump('succ');
?>


RabbitMQ支持消息的持久化,消息队列持久化包括3个部分:
(1)exchange持久化,在声明时指定durable为true
(2)queue持久化,在声明时指定durable为true
(3)消息持久化,在投递时指定delivery_mode 为2(1是非持久化)

代码配置:

$ex->setFlags(AMQP_DURABLE);

$q->setFlags(AMQP_DURABLE);

$ex->publish($message, 'routingkey_chenhwa', AMQP_NOPARAM, array('delivery_mode' => 2));


重启rabbitmq-server数据有效

参考文献:http://dc0127.blog.163.com/blog/static/11217896201322394228794/