RabbitMQ消息队列:安装与配置

by LauCyun Sep 22,2017 16:38:44 25,905 views

RabbitMQ是一个在 AMQP 基础上完整的,可复用的企业消息系统。他遵循 Mozilla Public License 开源协议。RabbitMQ相关资料:

本文主要介绍RabbitMQ的安装和基础配置,先介绍一下环境:

  • OS:CentOS 6.5
  • RabbitMQ:3.6.12
  • Erlang:20.0

1 安装Erlang

Erlang是一种通用的面向并发的编程语言,具体介绍:Erlang (programming language) - Wikipedia

方法1(使用Erlang Solutions安装):

Erlang Solutions:https://packages.erlang-solutions.com/erlang/

将Erlang Solutions仓库添加到系统中:

wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
# Erlang Solutions key
rpm --import https://packages.erlang-solutions.com/rpm/erlang_solutions.asc

/etc/yum.repos.d/erlang_solutions.repo的内容如下(则Erlang Solutions安装成功):

[erlang-solutions]
name=Centos $releasever - $basearch - Erlang Solutions
baseurl=https://packages.erlang-solutions.com/rpm/centos/$releasever/$basearch
gpgcheck=1
gpgkey=https://packages.erlang-solutions.com/rpm/erlang_solutions.asc
enabled=1

安装erlange:

sudo yum install erlang

方法2:

RabbitMQ官网提供Erlang安装包,下载地址:http://www.rabbitmq.com/releases/erlang/

下载好之后,安装下面两个文件:

yum localinstall -y erlang-19.0.4-1.el6.x86_64.rpm
yum localinstall -y esl-erlang-compat-18.1-1.noarch.rpm

方法3(源码安装):

当然,也可以通过源码来安装Erlang,先到www.erlang.org/download.html找到适合自己机器运行的版本,将Erlang下载到本地:

wget http://erlang.org/download/otp_src_20.0.tar.gz

解压并安装:

tar -zxvf otp_src_20.0.tar.gz
cd otp_src_20.0
./configure
make && make install

注意,这里是使用默认的路径进行安装,如有需要可以自行更改。

安装完后输入erl以下提示即为安装成功:

[root@localhost ~]# erl
Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.0  (abort with ^G)
1>

2 安装RabbitMQ

首先为了避免各种签名错误,我们把公钥加入可信任的列表:

# centos
rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
# ubuntu
wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
apt-key add rabbitmq-signing-key-public.asc

然后我们开始更新和安装RabbitMQ:

# centos
wget https://dl.bintray.com/rabbitmq/rabbitmq-server-rpm/rabbitmq-server-3.6.12-1.el6.noarch.rpm
yum install -y rabbitmq-server-3.6.12-1.el6.noarch.rpm
# ubuntu
apt-get update
apt-get install rabbitmq-server

如果安装出现如下错误:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Setting up Install Process
Examining rabbitmq-server-3.6.12-1.el6.noarch.rpm: rabbitmq-server-3.6.12-1.el6.noarch
Marking rabbitmq-server-3.6.12-1.el6.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package rabbitmq-server.noarch 0:3.6.12-1.el6 will be installed
--> Processing Dependency: socat for package: rabbitmq-server-3.6.12-1.el6.noarch
--> Finished Dependency Resolution
Error: Package: rabbitmq-server-3.6.12-1.el6.noarch (/rabbitmq-server-3.6.12-1.el6.noarch)
           Requires: socat
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

解决方法:

yum -y install socat

此时会报错没有socat包或是找不到socat包,解决方法安装centos的epel的扩展源:

yum -y install epel-release

之后重新安装socat

3 启动RabbitMQ

先看下自己的主机名:hostname,我的主机名是:laucyun

先修改一下 hosts 文件:vim /etc/hosts,添加一行:

127.0.0.1 laucyun

启动:

service rabbitmq-server start

启动一般都比较慢,所以别急

停止:

service rabbitmq-server stop

重启:

service rabbitmq-server restart

设置开机启动:

chkconfig rabbitmq-server on

4 配置RabbitMQ

4.1 修改配置文件rabbitmq.config

查找默认配置位置:find / -name "rabbitmq.config.example",搜索结果是:

$ find / -name "rabbitmq.config.example"
/usr/share/doc/rabbitmq-server-3.6.12/rabbitmq.config.example

复制默认配置:

$ cp /usr/share/doc/rabbitmq-server-3.6.12/rabbitmq.config.example /etc/rabbitmq/

修改配置文件名:

$ cd /etc/rabbitmq
$ mv rabbitmq.config.example rabbitmq.config

编辑配置文件,开启用户远程访问:vim rabbitmq.config

在 64 行:%% {loopback_users, []},(注意:该语句最后有一个逗号,等下是要去掉的),将其改为:{loopback_users, []}

开启Web界面管理:

$ rabbitmq-plugins enable rabbitmq_management

重启RabbitMQ服务:

$ service rabbitmq-server restart

开放防火墙端口:

$ iptables -I INPUT -p tcp -m tcp --dport 15672 -j ACCEPT
$ iptables -I INPUT -p tcp -m tcp --dport 5672 -j ACCEPT
$ service iptables save
$ service iptables restart

浏览器访问:http://192.168.0.149:15672 默认管理员账号:guest默认管理员密码:guest,如图1:


图1 RabbitMQ登录界面

4.2 添加新授权用户

Admin > User > Add a user中添加新用户,如图2:


图2 添加新授权用户

注意:用户的Tags是可以通过下面那行快捷输入的。

同样也可以通过命令行添加新授权用户,如下:

$ rabbitmqctl add_user admin admin
Creating user "admin"
$ rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator]
$ rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/"
$ rabbitmqctl list_users
Listing users
admin   [administrator]

4.3 添加Host

Admin > Virtual Hosts > Add a new virtual host 中添加新Host,如图3:


图3 添加Host

给添加的Host设置权限:


图4 添加的 Host 设置权限

5 参考

Tags