RabbitMQ安装与测试
1、下载Erlang(RabbitMQ是Erlang写的,这个2语言,哥是相当的不喜欢)
2、到www.rabbitmq.com下载最新程序,本人在用2.4.0
3、用记事本打开sbin目录下的rabbitmq-server,设置CONFIG_FILE配置文件路径,默认情况下是/etc/rabbitmq/rabbitmq.config(注意配置文件后缀必须是.config)
4、然后启动服务端(注意要使用管理员权限)
#sudo ./rabbitmq-server
5、下载rabbitmq-c和rabbitmq-codegen,把rabbitmq-codegen解压后改名为codegen,然后放到rabbitmq-c目录下,执行命令
# autoreconf -i && ./configure && make && sudo make install
6、下载PHP的AMQP扩展(http://code.google.com/p/php-amqp),编译的时候会报错,因为版本原因,代码没有即时更新,几处需要修改的地方如下:
1) 到http://code.google.com/p/php-amqp/issues/detail?id=2下载phpamqp.patch文件,放到amqp目录下,执行命令
# patch -p0 < amqp.c.patch (如何没有patch就 sudo pacman -S patch,修改了7处不兼容部分,具体内容可以直接查看amqp.c.patch)
2) 找到876行,将x.library_errno改为x.library_error;
# phpize && ./configure –with-amqp && make
# sudo cp modules/amqp.so /usr/lib/php/modules
7、测试代码
<?php
// create a connection ...
$connection = amqp_connection_popen("localhost", 5672 );
$user = "guest";
$pass = "guest";
$vhost = "/";
// login
$res = amqp_login($connection, $user, $pass, $vhost);
// open channel
$channel_id = 1;
$res = amqp_channel_open($connection, $channel_id);
$queue = 'MyQueue';
$exchange ="myexchange.direct";
$routing_key = "RoutingKey";
// optional: declare exchange, declare queue, bind queue
$res = amqp_exchange_declare($connection, $channel_id, $exchange, "direct");
$res = amqp_queue_declare($connection, $channel_id, $queue, $passive = false, $durable = false, $exclusive = false, $auto_delete = true);
$res = amqp_queue_bind($connection, $channel_id, $queue, $exchange, $routing_key);
// optinal, specify options for basic_publish()
$options = array(
"content_type" => "ContentType",
"content_encoding" => "ContentEncoding",
"delivery_mode" => 2,
"priority" => 1,
"correlation_id" => "correlation_id",
"reply_to" => "reply_to",
"expiration" => "tomorowww",
"message_id" => "id of the message",
"timestamp" => time(),
"type" => "type of the message",
"user_id" => "userId!",
"app_id" => "ApplicationId",
"cluster_id" => "ClusterId!"
);
// send the message to rabbitmq
$body = "Message Body";
$start = microtime(true);
for ($i = 0; $i < 10; $i++) {
$res = amqp_basic_publish($connection, $channel_id, $exchange, $routing_key, $body, false, false, $options);
}
$end = microtime(true);
echo "Total publish time: " . ($end - $start) ."\n";
?>