PHP AMQP訊息佇列安裝配置

來源:互聯網
上載者:User

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/


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.