1. 安装rabbitmq 之前首先
(1)安装erlang.:
下载erlang:
- 从Erlang的官网 http://www.erlang.org/download.html 下载最新的erlang安装包,Linux和MacOSX下载的版本是R15B01 Source File(72.0 MB)
- 我的是Mac OSX系统 所以我直接在 http://www.erlang-solutions.com/section/132/download-erlang-otp下载的对应版本的安装包,省的自己配置和安装了
- 然后解压下载的gz包 tar zxcf *.tar.gz
- cd 进入解压出来的文件夹
- 执行./configure --prefix=/opt/erlang 就会开始编译安装 会编译到 /opt/erlang 下 然后执行
- make 和 make install
- 编译完成以后,进入/opt/erlang,输入erl测试erlang是否安装成功。
- 修改/etc/profile文件,增加下面的环境变量:
- #set erlang environment
- export PATH=$PATH:/opt/erlang/bin
- source profile使得文件生效
yum install erlang
(2).安装python
yum install python -y
(3) 安装simplejson
yum -y install xmlto
yum -y install python-simplejson
(4)安装rabbitmq
1.下载:
# wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.2/rabbitmq-server-generic-unix-3.4.2.tar.gz
2.解压缩:
# tar -zxf rabbitmq-server-generic-unix-3.4.2.tar.gz
3.移动这个目录到 /usr/local下并且重命名为rabbitmq:
# mv rabbitmq-server-generic-unix-3.4.2 /usr/local/rabbitmq
4.打开/etc/profile文件,在文件最后添如下两行环境变量
#set rabbitmq environment
export PATH=$PATH:/usr/local/rabbitmq/sbin
5.使环境变量生效:
# source /etc/profile
6.安装rabbitmq网页管理插件:
# cd /usr/local/rabbitmq/sbin/
#./rabbitmq-plugin enable rabbitmq-management
7.启动rabbitmq:
# cd /usr/local/rabbitmq/sbin
# ./rabbitmq-server -detached (可以实现后台运行)
8.查看启动是否成功: # netstat -tunlp | grep beam
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 3308/beam.smp
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 3308/beam.smp
tcp 0 0 :::5672 :::* LISTEN 3308/beam.smp
可以看到启动成功: 15672是rabbimq网页管理监听端口,5672是php客户端使用的端口,在浏览器中输入localhost:15672,可以看到如下页面:
输入用户名guest和密码guest即可通过网页管理rabbitmq。
9. 关闭rabbitmq : # cd /usr/local/rabbitmq/sbin 然后:#./rabbitmqctl stop
(5)php客户端使用rabbitmq
composer是PHP用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件,具体可参考composer权威网站:http://www.phpcomposer.com/
1)下载 Composer 的可执行文件# curl -sS https://getcomposer.org/installer | php# chmod u+x composer.phar# mv composer.phar /usr/local/bin/composer2)声明php依赖关系 在php项目下创建 composer.json 文件,描述了项目的依赖关系。
{
"reqire": {
"videlalvaro/php-amqplib": "v2.1.0"
}
}
3.使用 Composer
在与composer.json文件相同的目录下运行如下代码:#
composer install
4.这时候在composer.json相同的目录下生成vendor目录,这个目录中是composer.json文件中指定的php依赖库下载后的放置位置。
# ls
amqp index.php receive_logs.php vendor
Application new_task.php receive_logs_topic.php worker.php
composer.json phpinfo.php send.php
emit_log_direct.php Public test.php
emit_log.php README.md ThinkPHP
-----------------------------------------------------------------------------------------------------------------------
以上就搭建好了php和rabbitmq的使用环境,下面是怎么使用php作为客户端和rabbitmq通信。
先读这篇文章:http://blog.youkuaiyun.com/sun305355024sun/article/details/41913105再读这几篇文章:http://blog.youkuaiyun.com/wind_324/article/category/2165209
在官网介绍hello world这篇文章时候send.php和receive.php代码可能有错误,这时候,可以从刚才根据composer.json文件生成的依赖库vendor/videlalvaro/php-amqplib/demo目录下找到amqp_publisher.php和amqp_consumer.php这两个demo文件,复制到/usr/local/apache/htdocs目录下,不要使用官网上的send.php和receive.php文件。
打开 amqp_publisher.php文件进行修改,修改后的代码如下:
<?php
include(__DIR__ . '/vendor/autoload.php');
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
define('HOST', '127.0.0.1');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');
$exchange = 'router';
$queue = 'msgs';
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
/*
The following code is the same both in the consumer and the producer.
In this way we are sure we always have a queue to consume from and an
exchange where to publish messages.
*/
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$ch->queue_declare($queue, false, true, false, false);
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$ch->exchange_declare($exchange, 'direct', false, true, false);
$ch->queue_bind($queue, $exchange);
$msg_body = implode(' ', array_slice($argv, 1));
$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
$ch->basic_publish($msg, $exchange);
$ch->close();
$conn->close();
amqp_consumer.php进行修改后的代码:
<?php
include(__DIR__ . '/vendor/autoload.php');
use PhpAmqpLib\Connection\AMQPConnection;
define('HOST', '127.0.0.1');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');
$exchange = 'router';
$queue = 'msgs';
$consumer_tag = 'consumer';
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
/*
The following code is the same both in the consumer and the producer.
In this way we are sure we always have a queue to consume from and an
exchange where to publish messages.
*/
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$ch->queue_declare($queue, false, true, false, false);
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$ch->exchange_declare($exchange, 'direct', false, true, false);
$ch->queue_bind($queue, $exchange);
function process_message($msg)
{
echo "\n--------\n";
echo $msg->body;
echo "\n--------\n";
<pre name="code" class="php"> $msg->delivery_info['channel']->
basic_ack($msg->delivery_info['delivery_tag']);
// Send a message with the string "quit" to cancel the consumer.
if ($msg->body === 'quit') {
$msg->delivery_info['channel']->
basic_cancel($msg->delivery_info['consumer_tag']);
}
}
/*
queue: Queue from where to get the messages
consumer_tag: Consumer identifier
no_local: Don't receive messages published by this consumer.
no_ack: Tells the server if the consumer will acknowledge the messages.
exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
nowait:
callback: A PHP Callback
*/
$ch->basic_consume($queue, $consumer_tag, false, false, false, false, 'process_message');
function shutdown($ch, $conn)
{
$ch->close();
$conn->close();
}
register_shutdown_function('shutdown', $ch, $conn);
// Loop as long as the channel has callbacks registered
while (count($ch->callbacks)) {
$ch->wait();
}
测试 一下:
打开消费者监听:
[root@ip10 htdocs]# php amqp_consumer.php
发布者发布消息:
[root@ip10 htdocs]# php amqp_publisher.php 'lsi';
[root@ip10 htdocs]# php amqp_publisher.php 'zhangsan'
可以看到监听者打印出了这两个消息:
[root@ip10 htdocs]# php amqp_consumer.php
--------
lsi
--------
--------
zhangsan
--------
在localhost:15672 网页管理系统中可以看到有个msgs的队列(刚才创建的),并且在messages中看到total为2,因为我们刚才发布的两条消息,所以在这个队列中有2个消息。
本文详细介绍了在Linux环境下手动安装rabbitmq的过程,包括先安装erlang,然后是python和simplejson,最后使用Composer来管理PHP的依赖库,以确保rabbitmq的顺利运行。
1177

被折叠的 条评论
为什么被折叠?



