rabbitmq简单运用
生活随笔
收集整理的這篇文章主要介紹了
rabbitmq简单运用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?php
/*** 生產(chǎn)者*/$connection = new AMQPConnection(['host' => '192.168.23.130','port' => 5672,'login' => 'rabuser','password' => '123456'
]);$connection->connect() or die('連接失敗');try{$exchange_name = 'trades';$route_key = '/trade';//投遞消息到中間件$channel = new AMQPChannel($connection);//創(chuàng)建消息通道$exchange = new AMQPExchange($channel);//通過通道連接交換幾//設(shè)置通道名稱$exchange->setName($exchange_name);$data = json_encode(['time'=>time()]);//發(fā)布消息到交換機(jī)中$exchange->publish($data,$route_key);}catch (AMQPChannelException $e){var_dump($e);
}
<?php /*** 消費(fèi)者*/$connection = new AMQPConnection(['host' => '192.168.23.130','port' => 5672,'login' => 'rabuser','password' => '123456' ]);$connection->connect() or die('連接失敗');try{$exchange_name = 'trades';$route_key = '/trade';$queue_name = 'queue';//投遞消息到中間件$channel = new AMQPChannel($connection);//創(chuàng)建消息通道$exchange = new AMQPExchange($channel);//通過通道連接交換幾//設(shè)置通道名稱$exchange->setName($exchange_name);//三種獲取消息的模式,直連模式,主題模式,廣播模式$exchange->setType(AMQP_EX_TYPE_DIRECT);//聲明$exchange->declareExchange();//聲明隊(duì)列綁定交換機(jī)路由$queue = new AMQPQueue($channel);$queue->setName($queue_name);$queue->declareQueue();//綁定監(jiān)聽獲取數(shù)據(jù)$queue->bind($exchange_name,$route_key);//消費(fèi)數(shù)據(jù),默認(rèn)阻塞監(jiān)聽獲取數(shù)據(jù)$queue->consume(function ($event,$queue){//獲取數(shù)據(jù)$msg = $event->getBody();var_dump($msg);var_dump($queue);//回應(yīng)ACK$queue->ack($event->getDeliveryTag());});}catch (AMQPChannelException $e){var_dump($e); }
轉(zhuǎn)載于:https://www.cnblogs.com/pangxiaox/p/11070926.html
總結(jié)
以上是生活随笔為你收集整理的rabbitmq简单运用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。