centos 安装 rabbitmq
1、rabbitmq是erlang语言开发的,安装前首先需要安装erlang
# yum install erlang -y // 直接安装可能报错,
# yum install ncurses-devel // 安装依赖
官方下载erlang包
# wget http://erlang.org/download/otp_src_19.0.tar.gz
解压安装erlang
# tar xzvf otp_src_19.0.tar.gz
# cd otp_src_19.0
# ./configure
# make && make install
安装完毕后测试运行一下:
#erl
1>1+2.
3
2>halt(). // 退出
2、安装rabbitmq,yum安装
# rpm -Uvh http://mirrors.sohu.com/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm
# rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
# yum install rabbitmq-server -y
官方下载: wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.5/rabbitmq-server-3.6.5.zip
解压安装
# tar xzvf rabbitmq-server-3.6.5.zip
# cd rabbitmq-server-3.6.5
# make
# make TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin MAN_DIR=/usr/rabbitmq/man DOC_INSTALL_DIR=/usr/local/rabbitmq/doc install
3、安装完成后需要配置:
/usr/local/rabbitmq/sbin目录下文件说明:
rabbitmq-env // 环境配置
rabbitmq-defaults // 默认参数设置
rabbitmqctl // 管理工具
rabbitmq-plugins // 插件管理工具
rabbitmq-server // rabbitmq服务
3.1、添加自定义配置文件
RABBITMQ_NODE_IP_ADDRESS=192.168.2.2 // 自定义地址
RABBITMQ_NODE_PORT=3333 // 自定义端口号
3.2、启动:
# rabbitmq-server -detached
报错:ERROR: epmd error for host "bogon": timeout (timed out)
修改/etc/hosts
127.0.0.1 bogon
重启即可。
3.3、启用rabbitmq web管理插件,安装sbin目录下:
./rabbitmq-plugins enable rabbitmq_management
然后重启rabbitmq-server
登录web管理页面
http://192.168.2.2:15672
登录用户:guest/guest
3.4、列出所有插件
# ./rabbitmq-plugins list
3.5、安装php的rabbitmq扩展:
# yum install librabbitmq-devel.x86_64 -y
# wget http://pecl.php.net/get/amqp-1.7.1.tgz
# tar zxvf amqp-1.7.1.tgz
# cd amqp-1.7.1
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-amqp
# make
# make install
# vim /usr/local/php/etc/php.ini
extension=amqp.so
重启php-fpm
# /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf
php操作demo
1. 创建队列
<?php
//连接RabbitMQ
$conn_args = array( 'host'=>'192.168.2.2' , 'port'=> '3333', 'login'=>'guest' , 'password'=> 'guest','vhost' =>'/');
$conn = new \AMQPConnection($conn_args);
$conn->setTimeout(1);
$conn->connect();
//创建exchange名称和类型
$channel = new \AMQPChannel($conn);
$ex = new \AMQPExchange($channel);
$ex->setName('ttlsa_exchange');
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$ex->declare();
//创建queue名称,使用exchange,绑定routingkey
$q = new \AMQPQueue($channel);
$q->setName('ttlsa_queue');
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$q->declare();
$q->bind('ttlsa_exchange', 'ttlsa_routingkey');
//消息发布
$channel->startTransaction();
$message = json_encode(array('Hello World!','DIRECT'));
$ex->publish($message, 'ttlsa_routingkey');
$channel->commitTransaction();
$conn->disconnect();
?>
2. 获取队列信息:
<?php
//连接RabbitMQ
$conn_args = array( 'host'=>'192.168.2.2' , 'port'=> '3333', '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('ttlsa_queue');
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$q->declare();
$q->bind('ttlsa_exchange', 'ttlsa_routingkey');
//消息获取
$messages = $q->get(AMQP_AUTOACK) ;
if ($messages){
var_dump(json_decode($messages->getBody(), true ));
}
$conn->disconnect();
?>