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 <UserName> <Password>
delete_user <UserName>
change_password <UserName> <NewPassword>
list_users
add_vhost <VHostPath>
delete_vhost <VHostPath>
list_vhosts
set_permissions [-p <VHostPath>] <UserName> <Regexp> <Regexp> <Regexp>
clear_permissions [-p <VHostPath>] <UserName>
list_permissions [-p <VHostPath>]
list_user_permissions <UserName>
list_queues [-p <VHostPath>] [<QueueInfoItem> ...]
list_exchanges [-p <VHostPath>] [<ExchangeInfoItem> ...]
list_bindings [-p <VHostPath>]
list_connections [<ConnectionInfoItem> ...]
測試代碼:
入訊息佇列
<?php
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');
?>
出訊息佇列
<?php
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 為21是非持久化)
代碼配置:
$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/