RabbitMQ 入门指南——安装

时间:2022-11-09 05:58:43

RabbitMQ好文

安装RabbitMQ

Ubuntu

添加RabbitMQ源:

sudo echo "deb http://www.rabbitmq.com/debian testing main" >> /etc/apt/sources.list

将RabbitMQ公钥加入我们的信任密钥列表,避免一些关于unsigned packages的警告。

wget https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
sudo apt-key add rabbitmq-signing-key-public.asc

更新软件列表,安装rabbitmq-server

apt-get update
apt-get install rabbitmq-server

rabbitmq的相关目录:

$ find / -name .erlang.cookie
/var/lib/rabbitmq/.erlang.cookie

执行apt-get update之后报错:

W: GPG error: http://www.rabbitmq.com testing InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 6B73A36E6026DFCA

使用apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6B73A36E6026DFCA解决

启动MQ:

service rabbitmq-server start

发现通过ip:15672还是无法访问,那么,还需要进一步设置,启用RabbitMQWeb管理插件:

rabbitmq-plugins enable rabbitmq_management

然后就可以通过浏览器访问了,当使用guest/guest登陆时,报错,User can only log in via localhost

解决办法:

rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"

添加用户参考:

CentOS

yum -y install epel-release
yum -y update
wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.10/rabbitmq-server-3.6.10-1.el7.noarch.rpm
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
rpm -Uvh rabbitmq-server-3.6.10-1.el7.noarch.rpm
systemctl start rabbitmq-server
systemctl enable rabbitmq-server
rabbitmq-plugins enable rabbitmq_management

where -U to update if package is already installed, -v for verbose output, -h to print hash output

坑1:

在执行systemctl start rabbitmq-server.service之后,报错:Failed to start RabbitMQ broker:

原因,在运行systemctl start rabbitmq-server.service之前,我先执行了rabbitmq-plugins enable rabbitmq_management,导致/etc/rabbitmq/下生成了enabled_plugins。解决启动错误,先删除该文件。

此外,启动出错时,还有可能因为防护墙的原因,可以参考此篇文章关闭。

防火墙关闭解决办法:

firewall-cmd --permanent --add-port=5672/tcp
firewall-cmd --reload
setsebool -P nis_enabled 1

或者粗暴点的办法(不推荐)

systemctl stop firewalld
systemctl disable firewalld
setenforce 0 (记得/etc/selinux/config也改成 disable,否则重启就不生效了)
``` 坑2: rabbitmq服务总算start成功了,但是执行`rabbitmq-plugins enable rabbitmq_management`还是报错: ```
[root@SHA1000154085 rabbitmq]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
amqp_client
cowlib
cowboy
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@SHA1000154085... failed.
Error: {cannot_read_enabled_plugins_file,"/etc/rabbitmq/enabled_plugins",
eacces}
``` 网上看到一篇文章,抱着绝望的心情执行了: ```
umask 0022; rabbitmq-plugins enable rabbitmq_management
``` 竟然OK了!!! 坑3: 报错:`"Cookie file /var/lib/rabbitmq/.erlang.cookie must be accessible by owner only"` 解决办法-[unable to start rabbitmq server on ubuntu 10.04](https://*.com/questions/24610367/unable-to-start-rabbitmq-server-on-ubuntu-10-04): ```
chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie
chmod 600 /var/lib/rabbitmq/.erlang.cookie
``` 看来`.erlang.cookie`的属组也很关键,修改好之后是这样的:
```
-rw------- 1 rabbitmq rabbitmq 20 Jun 14 14:31 .erlang.cookie
``` ### 安装参考 - [*-How to install Install rabbitmq server on centos7](https://*.com/questions/32327873/how-to-install-install-rabbitmq-server-on-centos7)
- [How to Install RabbitMQ Server on CentOS 7](https://www.howtoforge.com/tutorial/how-to-install-rabbitmq-server-on-centos-7/)
- [RabbitMQ 基础](http://www.cnblogs.com/vincenshen/articles/6659589.html)
- [how-to-install-rabbitmq-on-centos-7](https://www.vultr.com/docs/how-to-install-rabbitmq-on-centos-7)
- [How to Install RabbitMQ on CentOS/RHEL 7/6 and Fedora](https://tecadmin.net/install-rabbitmq-on-centos/) ## RabbitMQ集群 - [rabbitmq 集群搭建](http://www.pandan.xyz/2017/02/11/rabbitmq%20%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/)
- [Using RabbitMQ in Cluster](https://dzone.com/articles/rabbitmq-in-cluster)
- [CentOs7.3 搭建 RabbitMQ 3.6 Cluster 集群服务与使用](https://segmentfault.com/a/1190000010702020)
- [Rabbitmq集群高可用测试](https://www.cnblogs.com/flat_peach/archive/2013/04/07/3004008.html)
- [RabbitMQ集群和高可用配置的技巧](https://www.linuxprobe.com/rabbitmq-configuration.html) ### 修改hostname 临时修改hostname:`hostname <new hostname>` 推荐永久修改的方式: CentOS系
```
$vi /etc/sysconfig/network
HOSTNAME=yourhostname
``` Debian发行版的hostname的配置文件是`/etc/hostname`。 Ubuntu ```
vi /etc/hostname
``` 还需要修改`hosts`: ```
$vi /etc/hosts
127.0.0.1 node2
127.0.0.1 localhost node2 localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
```
最后`reboot`重启,输入`hostname`查看是否生效。 参考: - [CentOs6.5 修改主机名](http://www.ymq.io/2017/07/31/linux-localdomain/#3%E9%87%8D%E5%90%AF%E6%9C%8D%E5%8A%A1%E5%99%A8)
- [修改centos等linux的hostname-永久生效](https://blog.csdn.net/yangshangwei/article/details/52878530) ## 文件路径 ```
[root@SHA1000154085 rabbitmq]# find / -name rabbitmq-server /usr/lib/ocf/resource.d/rabbitmq/rabbitmq-server
/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.10/sbin/rabbitmq-server
/usr/lib/rabbitmq/bin/rabbitmq-server
/usr/sbin/rabbitmq-server
/etc/logrotate.d/rabbitmq-server
``` ## 重要方法 ### factory.setAutomaticRecoveryEnabled() ```
//默认rabbitmq client是打开了自动recover连接的能力的(5.0版本的rabbitmq 默认是打开的)
factory.setAutomaticRecoveryEnabled(true); ExecutorService es = Executors.newFixedThreadPool(20);
return factory.newConnection(es);
``` ### channel.basicQos()
channel.basicQos(1); // accept only one unack-ed message at a time (see below) ### 参考-重要方法 - [rabbitmq channel参数详解](https://www.cnblogs.com/piaolingzxh/p/5448927.html)
- [RabbitMQ指南(上)](http://www.importnew.com/24319.html)