http://www.bubuko.com/infodetail-1087728.html
1、安装erlang 语言环境
RabbitMQ是基于Erlang的,所以首先必须配置Erlang环境.
yum -y install make gcc gcc-c++ kernel-devel m4 ncurses ncurses-devel openssl-devel perl
Erlang官网 http://www.erlang.org/ http://www.erlang.org/download.html
cd /usr/local/src
wget http://www.erlang.org/download/otp_src_17.4.tar.gz
tar -zxvf otp_src_17.4.tar.gz
cd otp_src_17.4; ./configure --prefix=/usr/local/erlang; make; make install
修改/etc/profile文件,增加下面的环境变量:
#set erlang environment
export PATH=$PATH:/usr/local/erlang/bin
. /etc/profile
安装完成以后,执行erl看是否能打开eshell,用’halt().’退出,注意后面的点号,那是erlang的结束符。
[root@localhost src]# erl
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V6.1 (abort with ^G)
2> 9+3.
12
3> halt().
2、安装RabbitMQ
下载源码
cd /usr/local/src
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.3/rabbitmq-server-3.4.3.tar.gz
yum install xmlto -y # 安装依赖文件 否则编译时报错:line 1: xmlto: command not found
tar -zxvf rabbitmq-server-3.4.3.tar.gz
cd rabbitmq-server-3.4.3
make TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin MAN_DIR=/usr/local/rabbitmq/man DOC_INSTALL_DIR=/usr/local/rabbitmq/doc
make TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin MAN_DIR=/usr/local/rabbitmq/man DOC_INSTALL_DIR=/usr/local/rabbitmq/doc install
添加系统环境变量:
修改/etc/profile文件,增加下面的环境变量:
export PATH=$PATH:/usr/local/erlang/bin:/usr/local/rabbitmq/sbin
source /etc/profile
另外:下载bin文件更简单,解压就可以使用。
启动服务
运行rabbitmq:
/usr/rabbitmq/sbin/rabbitmq-server -detached #默认监听端口5672、25672
查看rabbitmq状态:
/usr/rabbitmq/sbin/rabbitmqctl status
关闭rabbitmq:
/usr/rabbitmq/sbin/rabbitmqctl stop
查看RabbitMQ服务启动的状态
添加启动脚本脚本
把以下内容保存到 /etc/init.d/rabbitmq-server
#!/bin/sh
#
# rabbitmq-server RabbitMQ broker
#
# chkconfig: - 80 05
# description: Enable AMQP service provided by RabbitMQ
#
### BEGIN INIT INFO
# Provides: rabbitmq-server
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Description: RabbitMQ broker
# Short-Description: Enable AMQP service provided by RabbitMQ broker
### END INIT INFO
# Source function library.
. /etc/init.d/functions
export HOME=/root
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/erlang/bin
NAME=rabbitmq-server
#DAEMON=/usr/sbin/${NAME}
#CONTROL=/usr/sbin/rabbitmqctl
DAEMON=/usr/local/rabbitmq/sbin/${NAME}
CONTROL=/usr/local/rabbitmq/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=rabbitmq
ROTATE_SUFFIX=
INIT_LOG_DIR=/usr/local/rabbitmq/logs
PID_FILE=/var/run/rabbitmq/pid
START_PROG="daemon"
LOCK_FILE=/var/lock/subsys/$NAME
test -x $DAEMON || exit 0
test -x $CONTROL || exit 0
RETVAL=0
set -e
[ -f /etc/default/${NAME} ] && . /etc/default/${NAME}
ensure_pid_dir () {
PID_DIR=`dirname ${PID_FILE}`
if [ ! -d ${PID_DIR} ] ; then
mkdir -p ${PID_DIR}
chown -R ${USER}:${USER} ${PID_DIR}
chmod 755 ${PID_DIR}
fi
}
remove_pid () {
rm -f ${PID_FILE}
rmdir `dirname ${PID_FILE}` || :
}
start_rabbitmq () {
status_rabbitmq quiet
if [ $RETVAL = 0 ] ; then
echo RabbitMQ is currently running
else
RETVAL=0
ensure_pid_dir
set +e
RABBITMQ_PID_FILE=$PID_FILE $START_PROG $DAEMON \
> "${INIT_LOG_DIR}/startup_log" \
2> "${INIT_LOG_DIR}/startup_err" \
0<&- &
$CONTROL wait $PID_FILE >/dev/null 2>&1
RETVAL=$?
set -e
case "$RETVAL" in
0)
echo SUCCESS
if [ -n "$LOCK_FILE" ] ; then
touch $LOCK_FILE
fi
;;
*)
remove_pid
echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\}
RETVAL=1
;;
esac
fi
}
stop_rabbitmq () {
status_rabbitmq quiet
if [ $RETVAL = 0 ] ; then
set +e
$CONTROL stop ${PID_FILE} > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err
RETVAL=$?
set -e
if [ $RETVAL = 0 ] ; then
remove_pid
if [ -n "$LOCK_FILE" ] ; then
rm -f $LOCK_FILE
fi
else
echo FAILED - check ${INIT_LOG_DIR}/shutdown_log, _err
fi
else
echo RabbitMQ is not running
RETVAL=0
fi
}
status_rabbitmq() {
set +e
if [ "$1" != "quiet" ] ; then
$CONTROL status 2>&1
else
$CONTROL status > /dev/null 2>&1
fi
if [ $? != 0 ] ; then
RETVAL=3
fi
set -e
}
rotate_logs_rabbitmq() {
set +e
$CONTROL rotate_logs ${ROTATE_SUFFIX}
if [ $? != 0 ] ; then
RETVAL=1
fi
set -e
}
restart_running_rabbitmq () {
status_rabbitmq quiet
if [ $RETVAL = 0 ] ; then
restart_rabbitmq
else
echo RabbitMQ is not runnning
RETVAL=0
fi
}
restart_rabbitmq() {
stop_rabbitmq
start_rabbitmq
}
case "$1" in
start)
echo -n "Starting $DESC: "
start_rabbitmq
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
stop_rabbitmq
echo "$NAME."
;;
status)
status_rabbitmq
;;
rotate-logs)
echo -n "Rotating log files for $DESC: "
rotate_logs_rabbitmq
;;
force-reload|reload|restart)
echo -n "Restarting $DESC: "
restart_rabbitmq
echo "$NAME."
;;
try-restart)
echo -n "Restarting $DESC: "
restart_running_rabbitmq
echo "$NAME."
;;
*)
echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2
RETVAL=1
;;
esac
exit $RETVAL
########################################
useradd -M -s /sbin/nologin rabbitmq
mkdir /usr/local/rabbitmq/logs
chmod +x /etc/init.d/rabbitmq-server
chkconfig rabbitmq-server on
rabbitmq配置
一般情况下,RabbitMQ的默认配置就足够了。如果希望特殊设置的话,有两个途径:
一个是环境变量的配置文件 /etc/rabbitmq/rabbitmq-env.conf ;
一个是配置信息的配置文件 /etc/rabbitmq/rabbitmq.conf;
注意,这两个文件默认是没有的,如果需要必须自己创建。
rabbitmq-env.conf
这个文件的位置是确定和不能改变的,位于:/etc/rabbitmq目录下(这个目录需要自己创建)。
文件的内容包括了RabbitMQ的一些环境变量,常用的有:
#RABBITMQ_NODE_PORT= //端口号
#HOSTNAME=
RABBITMQ_NODENAME=mq
RABBITMQ_CONFIG_FILE= //配置文件的路径
RABBITMQ_MNESIA_BASE=/rabbitmq/data //需要使用的MNESIA数据库的路径
RABBITMQ_LOG_BASE=/rabbitmq/log //log的路径
RABBITMQ_PLUGINS_DIR=/rabbitmq/plugins //插件的路径
具体的列表见:http://www.rabbitmq.com/configure.html#define-environment-variables
rabbitmq.config
这是一个标准的erlang配置文件。它必须符合erlang配置文件的标准。
它既有默认的目录,也可以在rabbitmq-env.conf文件中配置。
文件的内容详见:http://www.rabbitmq.com/configure.html#config-items
安装web插件管理界面
mkdir /etc/rabbitmq/
rabbitmq-plugins enable rabbitmq_management # 启动端口15672
运行如下的命令,增加用户admin,密码admin
[root@localhost sbin]# rabbitmqctl add_user admin admin
Creating user "admin" ...
...done.
[root@localhost sbin]# rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...
...done.
[root@localhost sbin]# ./rabbitmqctl list_users
Listing users ...
admin [administrator]
guest [administrator]
...done.
创建vhost
rabbitmqctl add_vhost <vhostpath>
list_vhosts [<vhostinfoitem> ...]
delete_vhost <vhostpath>
list_vhosts [<vhostinfoitem> ...]
set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
list_permissions [-p <vhostpath>]
clear_parameter [-p <vhostpath>] <component_name> <key>
如:
rabbitmqctl add_vhost test_vhost
rabbitmqctl set_permissions -p test_vhost ".*" ".*" ".*"
浏览器上输入http://127.0.0.1:15672/登录管理界面了,(http://127.0.0.1:55672会跳转到 http://127.0.0.1:15672 3.0版本之前的):
使用登录的名户名和密码默认都算guest,登录后的页面如下(guest只能从 localhost 登录):
RabbitMQ提供的HTTP API接口
RabbitMQ还提供了HTTP API接口,这样可以通过编程方式监控RabbitMQ的运行状态,HTTP API接口的地址为:http://serverip:15672/api/
NET下RabbitMQ实践[配置篇]
NET下RabbitMQ实践[示例篇]
NET下RabbitMQ实践[WCF发布篇]
NET下RabbitMQ实践[实战篇]
RabbitMQ C# Tutorial
[Erlang 0086] RabbitMQ 集群: 从零开始
安装 php-rabbit: RabbitMQ 的 PHP 扩展
RabbitMQ 官方提供了三种 PHP 可用的扩展,分别是:
php-amqp
http://code.google.com/p/php-amqp/
php-rabbit
http://code.google.com/p/php-rabbit/
php-amqplib
http://code.google.com/p/php-amqplib/
这里我选择使用 php-rabbit,因为这个项目的 Activity 比较高,名字也比较权威 :)
安装步骤:
下载 RabbitMQ-C 客户端库 librabbitmq:
http://hg.rabbitmq.com/rabbitmq-c/
下载 RabbitMQ 协议代码生成工具:
http://hg.rabbitmq.com/rabbitmq-codegen/
注意 librabbitmq 的版本号,使用最新版本可能会导致下面编译 php-rabbit 时出错:
/bin/sh /work/setup/php-rabbit/libtool �Cmode=compile cc -I. -I/work/setup/php-rabbit -DPHP_ATOM_INC -I/work/setup/php-rabbit/include -I/work/setup/php-rabbit/main -I/work/setup/php-rabbit -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /work/setup/php-rabbit/rabbit.c -o rabbit.lo
mkdir .libs
cc -I. -I/work/setup/php-rabbit -DPHP_ATOM_INC -I/work/setup/php-rabbit/include -I/work/setup/php-rabbit/main -I/work/setup/php-rabbit -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /work/setup/php-rabbit/rabbit.c -fPIC -DPIC -o .libs/rabbit.o
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_class___construct’:
/work/setup/php-rabbit/rabbit.c:227: warning: ‘php_std_error_handling’ is deprecated (declared at /usr/local/php/include/php/main/php.h:295)
/work/setup/php-rabbit/rabbit.c:230: warning: ‘php_std_error_handling’ is deprecated (declared at /usr/local/php/include/php/main/php.h:295)
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_class_isConnected’:
/work/setup/php-rabbit/rabbit.c:329: warning: ‘php_std_error_handling’ is deprecated (declared at /usr/local/php/include/php/main/php.h:295)
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_queue_class___construct’:
/work/setup/php-rabbit/rabbit.c:363: warning: ‘php_std_error_handling’ is deprecated (declared at /usr/local/php/include/php/main/php.h:295)
/work/setup/php-rabbit/rabbit.c:368: warning: ‘php_std_error_handling’ is deprecated (declared at /usr/local/php/include/php/main/php.h:295)
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_queue_class_delete’:
/work/setup/php-rabbit/rabbit.c:504: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c:512: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_queue_class_purge’:
/work/setup/php-rabbit/rabbit.c:574: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c:580: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_queue_class_bind’:
/work/setup/php-rabbit/rabbit.c:646: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_queue_class_unbind’:
/work/setup/php-rabbit/rabbit.c:713: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_queue_class_consume’:
/work/setup/php-rabbit/rabbit.c:781: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_queue_class_get’:
/work/setup/php-rabbit/rabbit.c:917: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_exchange_class___construct’:
/work/setup/php-rabbit/rabbit.c:1169: warning: ‘php_std_error_handling’ is deprecated (declared at /usr/local/php/include/php/main/php.h:295)
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_exchange_class_delete’:
/work/setup/php-rabbit/rabbit.c:1318: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c:1326: error: unknown field ‘ticket’ specified in initializer
/work/setup/php-rabbit/rabbit.c: In function ‘zim_rabbit_exchange_class_bind’:
/work/setup/php-rabbit/rabbit.c:1534: error: unknown field ‘ticket’ specified in initializer
make: *** [rabbit.lo] Error 1
看了下是头文件 /usr/local/include/amqp_framing.h 中的结构体定义与源代码中冲突,因此怀疑是 librabbitmq 版本的问题。
使用 svn 上的代码也不行,很明显当前版本不适用。
看到官方下载页面上的声明:
Specification
The current RabbitMQ server and Java client library releases implement AMQP protocol version 0-8. The .NET/C# client implements AMQP protocol versions 0-8 and 0-9.
估计得用 0.8 版,我使用以下方式安装 librabbitmq 时可以编译通过:
wget http://hg.rabbitmq.com/rabbitmq-c/archive/ce1eaceaee94.tar.gz -O rabbitmq-c.tar.gz
tar zxf rabbitmq-c.tar.gz
wget http://hg.rabbitmq.com/rabbitmq-codegen/archive/c7c5876a05bb.tar.gz -O rabbitmq-codegen.tar.gz
tar zxf rabbitmq-codegen.tar.gz
mv rabbitmq-codegen-c7c5876a05bb/ rabbitmq-c-ce1eaceaee94/codegen
cd rabbitmq-c-ce1eaceaee94/
yum -y install automake libtool
autoreconf -i && ./configure && make && make install
如果提示:
checking finding a python with simplejson installed… configure: error: could not find a python that can ‘import simplejson’
请安装 python 的 simplejson 库:
# wget http://pypi.python.org/packages/source/s/simplejson/simplejson-2.1.1.tar.gz
# tar zxf simplejson-2.1.1.tar.gz
# cd simplejson-2.1.1
# python setup.py install
提示问题:macro `AM_PROG_LIBTOOL’ not found in library
解决方案:一种是没有安装libtool,一种是安装了libtool但是aclocal和libtool未安装在同一文件夹下
下载并安装 php-rabbit:
# wget http://php-rabbit.googlecode.com/files/php-rabbit.r91.tar.gz
# tar zxf php-rabbit.r91.tar.gz
# cd php-rabbit
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-rabbit
# make && make install
修改 php.ini,在尾部加上:
extension = rabbit.so
查看扩展是否安装成功:
# php -m | grep rabbit
rabbit
PHP连接rabbitMQ测试 test_rabbitmq_conn.php
<?php
/**
* producer demo
*
* @author wei
* @version $Id$
**/
$params = array('host' =>'localhost',
'port' => 5672,
'login' => 'guest',
'password' => 'guest',
'vhost' => '/');
$cnn = new AMQPConnect($params);
// declare Exchange
$exchange = new AMQPExchange($cnn);
$exchange->declare('ex1', 'topic', AMQP_DURABLE );
// declare Queue
$queue = new AMQPQueue($cnn);
$queue->declare('queue1', AMQP_DURABLE);
// bind Queue
$queue->bind('ex1','wei.#');
// publishing
$msg = "msg";
for ($i=0; $i < 100; $i++) {
$res = $exchange->publish($i . 'msg', 'wei.' . $i);
if ($res) {
echo $i . 'msg' . " Yes\n";
}
else {
echo $i . 'msg' . " No\n";
}
}
?>