1. 首页 > 服务器运维

消息队列服务rabbitmq安装配置

一. 安装erlang
Rabbitmq基于erlang语言开发,因此需要安装erlang虚拟机
1.通过yum安装

# wget -O /etc/yum.repos.d/epel-erlang.repo http://repos.fedorapeople.org/repos/peter/erlang/epel-erlang.repo
# yum install erlang

2.通过源码包安装

# yum install ncurses-devel.x86_64
# wget http://www.erlang.org/download/otp_src_R16B01.tar.gz
# tar zxvf otp_src_R16B01.tar.gz 
# ./configure --prefix=/usr/local/erlang_R16B01 --enable-halfword-emulator --enable-m64-build
# make
# make instal

二. 安装rabbitmq
1. 通过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-3.1.1-1.noarch.rpm

2. 通过源码包安装
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.1.3/rabbitmq-server-generic-unix-3.1.3.tar.gz //免安装包,解压就可以用了

三. 配置
/usr/local/rabbitmq/sbin目录下
rabbitmq-env 环境配置
rabbitmq-defaults 默认参数设置
rabbitmqctl 管理工具
rabbitmq-plugins 插件管理工具
rabbitmq-server rabbitmq服务

# vim rabbitmq-defaults

SYS_PREFIX=${RABBITMQ_HOME}
ERL_DIR=/usr/local/erlang/bin/ //设置erl命令路径
CLEAN_BOOT_FILE=start_clean
SASL_BOOT_FILE=start_sasl
CONFIG_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq //设置rabbitmq运行参数
LOG_BASE=${SYS_PREFIX}/var/log/rabbitmq
MNESIA_BASE=${SYS_PREFIX}/var/lib/rabbitmq/mnesia
ENABLED_PLUGINS_FILE=${SYS_PREFIX}/etc/rabbitmq/enabled_plugins //允许插件列表配置文件
PLUGINS_DIR="${RABBITMQ_HOME}/plugins"
CONF_ENV_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq-env.conf

主要配置文件
1. enabled_plugins:设置允许的插件列表,格式如下:

[rabbitmq_jsonrpc_channel,
rabbitmq_jsonrpc_channel_examples,
rabbitmq_management,
rabbitmq_management_visualiser,
rabbitmq_tracing].

2. rabbitmq.config:设置rabbitmq运行参数。结构为hash数组格式。如

[
{mnesia, [{dump_log_write_threshold, 1000}]},
{rabbit, [{vm_memory_high_watermark, 0.4}]}
{rabbitmq_management,
[{listener, [{port, 55673},
{ip, "0.0.0.0"}
]}
]}
].

其中几个关键参数为:
tcp_listerners 设置rabbimq的监听端口,默认为[5672]。
disk_free_limit 磁盘低水位线,若磁盘容量低于指定值则停止接收数据,默认值为{mem_relative, 1.0},即与内存相关联1:1,也可定制为多少byte.
vm_memory_high_watermark,设置内存低水位线,若低于该水位线,则开启流控机制,默认值是0.4,即内存总量的40%。
hipe_compile 将部分rabbimq代码用High Performance Erlang compiler编译,可提升性能,该参数是实验性,若出现erlang vm segfaults,应关掉。
force_fine_statistics, 该参数属于rabbimq_management,若为true则进行精细化的统计,但会影响性能。
/usr/local/etc/rabbitmq/rabbitmq.config
[{auth_backends,[rabbit_auth_backend_internal]},
{auth_mechanisms,['PLAIN','AMQPLAIN']},
{backing_queue_module,rabbit_variable_queue},
{cluster_nodes,[]},
{collect_statistics,fine},
{collect_statistics_interval,5000},
{default_permissions,[<<".*">>,<<".*">>,<<".*">>]},
{default_user,<<"guest">>},
{default_user_tags,[administrator]},
{default_vhost,<<"/">>},
{delegate_count,16},
{error_logger,{file,"/usr/local/var/log/rabbitmq/rabbit@Technophiliac.log"}},
{frame_max,131072},
{hipe_compile,false},
{included_applications,[]},
{msg_store_file_size_limit,16777216},
{msg_store_index_module,rabbit_msg_store_ets_index},
{queue_index_max_journal_entries,262144},
{sasl_error_logger,{file,"/usr/local/var/log/rabbitmq/rabbit@Technophiliac-sasl.log"}},
{server_properties,[]},
{ssl_listeners,[]},
{ssl_options,[]},
{tcp_listen_options,
[binary,
{packet,raw},
{reuseaddr,true},
{backlog,128},
{nodelay,true},
{exit_on_close,false}]},
{tcp_listeners,[5672]},
{trace_vhosts,[<<"/">>]},
{vm_memory_high_watermark,0.4}]
3. rabbitmq-env.conf rabbitmq环境参数配置

RABBITMQ_NODENAME=FZTEC-240088 节点名称
RABBITMQ_NODE_IP_ADDRESS=127.0.0.1 监听IP
RABBITMQ_NODE_PORT=5672 监听端口
RABBITMQ_LOG_BASE=/data/rabbitmq/log 日志目录
RABBITMQ_PLUGINS_DIR=/data/rabbitmq/plugins 插件目录
RABBITMQ_MNESIA_BASE=/data/rabbitmq/mnesia 后端存储目录

更详细的配置参见: http://www.rabbitmq.com/configure.html#configuration-file

4. 启动

# /usr/local/rabbitmq/sbin/rabbitmq-server -detached

四. rabbitmq插件管理
启用rabbitmq web管理插件

# ./rabbitmq-plugins enable rabbitmq_management

列出所有插件

# ./rabbitmq-plugins list

五. 安装php的rabbitmq扩展

# yum install librabbitmq-devel.x86_64 
# wget http://pecl.php.net/get/amqp-1.2.0.tgz
# tar zxvf amqp-1.2.0.tgz 
# cd amqp-1.2.0
# /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
# /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf

六. php操作实例
1. 创建队列

<?php
//连接RabbitMQ
$conn_args = array( 'host'=>'10.31.247.202' , 'port'=> '5672', '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'=>'10.31.247.202' , 'port'=> '5672', '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();
?>

执行后输出信息如下:
array(2) {
[0]=>
string(12) "Hello World!"
[1]=>
string(6) "DIRECT"
}

七. 查看队列信息
1. 查看exchange信息

# /usr/local/rabbitmq/sbin/rabbitmqctl list_exchanges name type durable auto_delete arguments
Listing exchanges ...
direct true false []
amq.direct direct true false []
amq.fanout fanout true false []
amq.headers headers true false []
amq.match headers true false []
amq.rabbitmq.log topic true false []
amq.rabbitmq.trace topic true false []
amq.topic topic true false []
ttlsa_exchange direct true false []
...done.

2. 查看队列信息

# /usr/local/rabbitmq/sbin/rabbitmqctl list_queues name durable auto_delete messages consumers memory 
Listing queues ...
ttlsa_queue true true 1 0 7244
...done.

3. 查看绑定信息

# /usr/local/rabbitmq/sbin/rabbitmqctl list_bindings
Listing bindings ...
exchange ttlsa_queue queue ttlsa_queue []
ttlsa_exchange exchange ttlsa_queue queue ttlsa_routingkey []
...done.

八. 日志轮转

# /usr/local/rabbitmq/sbin/rabbitmqctl rotate_logs .1

本文由主机测评网发布,不代表主机测评网立场,转载联系作者并注明出处:https://zhuji.jb51.net/yunwei/8293.html

联系我们

在线咨询:点击这里给我发消息

微信号:

工作日:8:30-17:30,节假日休息