随笔 - 137  文章 - 1  trackbacks - 0
<2020年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿

随笔分类

随笔档案

收藏夹

调试技巧

搜索

  •  

最新评论

阅读排行榜

评论排行榜

[1]Install some packages.
# install from EPEL

[root@dlp ~]# 
yum --enablerepo=epel -y install composer php-bcmath
[2]Install AMQP client library.
[cent@dlp ~]$ 
composer require php-amqplib/php-amqplib 

Using version ^2.6 for php-amqplib/php-amqplib ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev)   - Installing php-amqplib/php-amqplib (v2.6.3)     Downloading: 100%  Writing lock file Generating autoload files  
[cent@dlp ~]$ 
composer install 

Loading composer repositories with package information Installing dependencies (including require-dev) from lock file Nothing to install or update Generating autoload files 
[3]This is an example of sending message on PHP.
For example, connect with RabbitMQ user "serverworld", virtualhost "my_vhost".
[cent@dlp ~]$ 
vi send_msg.php
<?php require_once __DIR__ . '/vendor/autoload.php';  use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage;  $connection = new AMQPStreamConnection('127.0.0.1', 5672, 'serverworld', 'password', '/my_vhost');  $channel = $connection->channel(); $channel->queue_declare('Hello_World', false, false, false, false);  $msg = new AMQPMessage('Hello RabbitMQ World!'); $channel->basic_publish($msg, '', 'Hello_World'); echo " [x] Sent 'Hello_World'\n";  $channel->close(); $connection->close(); ?>  
[cent@dlp ~]$ 
php send_msg.php 

 [x] Sent 'Hello_World' 
[4]This is an example of receiving message on PHP.
[cent@node01 ~]$ 
vi receive_msg.php
<?php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection;  $connection = new AMQPStreamConnection('127.0.0.1', 5672, 'serverworld', 'password', '/my_vhost'); $channel = $connection->channel();  $channel->queue_declare('Hello_World', false, false, false, false);  echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";  $callback = function($msg) {     echo " [x] Received ", $msg->body, "\n"; };  $channel->basic_consume('Hello_World', '', false, true, false, false, $callback);  while(count($channel->callbacks)) {     $channel->wait(); } ?>  
[cent@node01 ~]$ 
php receive_msg.php 

 [*] Waiting for messages. To exit press CTRL+C  [x] Received Hello RabbitMQ World!
posted on 2017-11-16 17:53 长戟十三千 阅读(278) 评论(0)  编辑 收藏 引用 所属分类: 编程技巧随笔

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理