1、使用Docker安裝部署RabbitMQphp
一、docker search rabbitmq:managementhtml
二、docker pull rabbitmq:managementgit
注:若是docker pull rabbitmq 後面不帶management,啓動rabbitmq後是沒法打開管理界面的,因此咱們要下載帶management插件的rabbitmq.github
三、建立rabbitmq容器的shell腳本(建立容器、啓動服務、添加admin用戶)web
#/bin/sh docker run --name rabbitmq --publish 5671:5671 --publish 5672:5672 --publish 4369:4369 --publish 25672:25672 --publish 15671:15671 --publish 15672:15672 -dit rabbitmq:management /bin/bash \ && docker exec -d rabbitmq /bin/bash -c "rabbitmq-server --detached" && echo "starting..." \ && docker exec -d rabbitmq /bin/bash -c "sleep 10s; rabbitmqctl add_user admin 123456 && rabbitmqctl set_user_tags admin administrator && rabbitmqctl set_permissions -p '/' admin '.*' '.*' '.*'" \ && docker ps
四、中止容器的shell腳本docker
#/bin/sh docker stop rabbitmq && docker rm rabbitmq && echo "success"
五、容器啓動以後就能夠訪問web 管理端了 http://宿主機IP:15672,默認用戶爲guest(密碼也是 guest),但只能訪問localhost。shell
2、PHP安裝擴展AMQPbash
1. 安裝rabbitmq-c,rabbitmq-c是一個用於C語言的,與AMQP server進行交互的client庫。php7
下載地址:https://github.com/alanxz/rabbitmq-c函數
unzip rabbitmq-c-master.zip cd rabbitmq-c-master mkdir /opt/rabbitmq-c mkdir build && cd build cmake -DCMAKE_INSTALL_PREFIX=/opt/rabbitmq-c .. cmake --build . --target install
2. 從https://pecl.php.net/package/amqp下載 amqp-1.9.3.tgz
wget https://pecl.php.net/get/amqp-1.9.3.tgz tar -zxvf amqp-1.9.3.tgz cd amqp-1.9.3 /opt/php72/bin/phpize ./configure --with-php-config=/opt/php72/bin/php-config --with-amqp --with-librabbitmq-dir=/opt/rabbitmq-c make && make install
3. 而後在php.ini添加 extension=amqp.so,而後執行/opt/php72/bin/php -m,看列出的已安裝擴展是否存在amqp。
3、測試
1. 新建rabbit_consumer.php做爲消費者
<?php //配置信息 $conn_args = array( 'host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest', 'vhost'=>'/' ); $e_name = 'e_linvo'; //交換機名 $q_name = 'q_linvo'; //隊列名 $k_route = 'key_1'; //路由key //建立鏈接和channel $conn = new AMQPConnection($conn_args); if (!$conn->connect()) { die("Cannot connect to the broker!\n"); } $channel = new AMQPChannel($conn); //建立交換機 $ex = new AMQPExchange($channel); $ex->setName($e_name); $ex->setType(AMQP_EX_TYPE_DIRECT); //direct類型 $ex->setFlags(AMQP_DURABLE); //持久化 echo "Exchange Status:".$ex->declare()."\n"; //建立隊列 $q = new AMQPQueue($channel); $q->setName($q_name); $q->setFlags(AMQP_DURABLE); //持久化 echo "Message Total:".$q->declare()."\n"; //綁定交換機與隊列,並指定路由鍵 echo 'Queue Bind: '.$q->bind($e_name, $k_route)."\n"; //阻塞模式接收消息 echo "Message:\n"; while(True){ $q->consume('processMessage'); //$q->consume('processMessage', AMQP_AUTOACK); //自動ACK應答 } $conn->disconnect(); /** * 消費回調函數 * 處理消息 */ function processMessage($envelope, $queue) { $msg = $envelope->getBody(); echo $msg."\n"; //處理消息 $queue->ack($envelope->getDeliveryTag()); //手動發送ACK應答 } ?>
2. 新建rabbit_publisher.php做爲生產者
<?php //配置信息 $conn_args = array( 'host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest', 'vhost'=>'/' ); $e_name = 'e_linvo'; //交換機名 //$q_name = 'q_linvo'; //無需隊列名 $k_route = 'key_1'; //路由key //建立鏈接和channel $conn = new AMQPConnection($conn_args); if (!$conn->connect()) { die("Cannot connect to the broker!\n"); } $channel = new AMQPChannel($conn); //建立交換機對象 $ex = new AMQPExchange($channel); $ex->setName($e_name); date_default_timezone_set("Asia/Shanghai"); //發送消息 //$channel->startTransaction(); //開始事務 for($i=0; $i<5; ++$i){ sleep(1);//休眠1秒 //消息內容 $message = "TEST MESSAGE!".date("h:i:sa"); echo "Send Message:".$ex->publish($message, $k_route)."\n"; } //$channel->commitTransaction(); //提交事務 $conn->disconnect(); ?>
3. 運行生產者,輸出結果
4. 運行消費者,輸出結果
參考:
https://blog.csdn.net/liyuejin/article/details/78410586
https://www.cnblogs.com/zhming26/p/6141028.html
https://www.cnblogs.com/miketwais/p/RabbitMQ.html