IT星球论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 347|回复: 0

消息队列服务rabbitmq安装配置

[复制链接]

2002

主题

1

好友

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

优秀会员 助人为乐 辛勤工作 技术精英 多才多艺 优秀班竹 灌水天才 星球管理 宣传大使 灌水之王 财富勋章 版主勋章 动漫勋章 勤奋会员 论坛精英 PS高手 心 8 闪游皮肤 双鱼座 8★8➹ 志愿者 乖

发表于 2016-1-1 14:01:36 |显示全部楼层
消息队列服务rabbitmq安装配置
一. 安装erlang
Rabbitmq基于erlang语言开发,因此需要安装erlang虚拟机
1.通过yum安装
# wget -O /etc/yum.repos.d/epel-erlang.repo http://repos.fedorapeople.org/re ... g/epel-erlang.repo# yum install erlang
1
2

# 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
1
2
3
4
5
6

# 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-e ... ase-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
1
2
3

# rpm -Uvh http://mirrors.sohu.com/fedora-e ... ease-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 ... c-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_cleanSASL_BOOT_FILE=start_saslCONFIG_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq //设置rabbitmq运行参数LOG_BASE=${SYS_PREFIX}/var/log/rabbitmqMNESIA_BASE=${SYS_PREFIX}/var/lib/rabbitmq/mnesiaENABLED_PLUGINS_FILE=${SYS_PREFIX}/etc/rabbitmq/enabled_plugins //允许插件列表配置文件PLUGINS_DIR="${RABBITMQ_HOME}/plugins"CONF_ENV_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq-env.conf
1
2
3
4
5
6
7
8
9
10

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].
1
2
3
4
5

[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"}]}]}].
1
2
3
4
5
6
7
8
9

[
{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 监听IPRABBITMQ_NODE_PORT=5672 监听端口RABBITMQ_LOG_BASE=/data/rabbitmq/log 日志目录RABBITMQ_PLUGINS_DIR=/data/rabbitmq/plugins 插件目录RABBITMQ_MNESIA_BASE=/data/rabbitmq/mnesia 后端存储目录
1
2
3
4
5
6

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
1

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



四. rabbitmq插件管理
启用rabbitmq web管理插件
# ./rabbitmq-plugins enable rabbitmq_management
1

# ./rabbitmq-plugins enable rabbitmq_management



列出所有插件
# ./rabbitmq-plugins list
1

# ./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
1
2
3
4
5
6
7
8
9
10
11

# 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();?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

<?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();?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

<?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 argumentsListing 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.
1
2
3
4
5
6
7
8
9
10
11
12

# /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.
1
2
3
4

# /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_bindingsListing bindings ...exchange ttlsa_queue queue ttlsa_queue []ttlsa_exchange exchange ttlsa_queue queue ttlsa_routingkey []...done.
1
2
3
4
5

# /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
1

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



转载请注明出处: http://www.ttlsa.com/html/1656.html
http://www.ttlsa.com/linux/install-rabbitmq-on-linux/
消息队列服务rabbitmq安装配置

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

回顶部