RabbitMQ学习笔记
# 安装Erlang和RabbitMQ
- RabbitMQ官网 (opens new window)
- RabbitMQ下载1 (opens new window)
- RabbitMQ下载2 (opens new window)
- Erlang版本要求 (opens new window)
- https://www.erlang.org/downloads (opens new window)
- https://erlang.org/download/otp_versions_tree.html (opens new window)
- Linux中安装Erlang (opens new window)

【Erlang】准备环境
# 查询是否安装某软件包:rpm -q 软件包
# yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel
yum -y install make kernel-devel ncurses-devel
2
3
【Erlang】创建目录
mkdir erlang
【Erlang】解压文件
tar -zxf otp_src_22.3.tar.gz -C /opt/erlang
【Erlang】进入解压目录
cd otp_src_22.3/
【Erlang】设定安装规则
# –prefix 指定安装目录
# –enable-smp-support启用对称多处理支持(Symmetric Multi-Processing对称多处理结构的简称)
# –enable-threads启用异步线程支持
# –enable-sctp启用流控制协议支持(Stream Control Transmission Protocol,流控制传输协议)
# –enable-kernel-poll启用Linux内核poll
# –enable-hipe启用高性能Erlang
# –with-ssl 启用ssl包
# –without-javac不用java编译
./configure --prefix=/usr/local/erlang --enable-kernel-poll --enable-hipe --enable-threads --enable-smp-support --with-ssl --without-javac
2
3
4
5
6
7
8
9
【Erlang】编译并且安装
make && make install
【Erlang】配置环境变量
vim /etc/profile
# Erlang
ERL_HOME=/usr/local/erlang
export PATH=$PATH:$ERL_HOME/bin
2
3
# 使环境变量生效
source /etc/profile
2
【Erlang】使用 erl
测试是否安装成功,使用 halt().
退出
[root@localhost otp_src_22.3]# erl
Erlang/OTP 22 [erts-10.7] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [hipe]
Eshell V10.7 (abort with ^G)
1> halt(). #退出
2
3
4
5
【RabbitMQ】安装
#[root@localhost opt]# rpm -ivh rabbitmq-server-3.8.14-1.el7.noarch.rpm
#警告:rabbitmq-server-3.8.14-1.el7.noarch.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 6026dfca: NOKEY
#错误:依赖检测失败:
# erlang >= 21.3 被 rabbitmq-server-3.8.14-1.el7.noarch 需要
# socat 被 rabbitmq-server-3.8.14-1.el7.noarch 需要
rpm -ivh --nodeps rabbitmq-server-3.8.14-1.el7.noarch.rpm
2
3
4
5
6
7
【RabbitMQ】查看位置
[root@localhost opt]# whereis rabbitmq
rabbitmq: /usr/lib/rabbitmq /etc/rabbitmq
2
【RabbitMQ】主配置文件
在 /etc/rabbitmq 新建主配置文件 rabbitmq.conf,rabbitmq.conf允许配置RabbitMQ服务器和插件。从RabbitMQ 3.7.0开始,格式为sysctl格式。
以下为最小配置:
loopback_users.guest = false
更多配置请查看:
https://www.rabbitmq.com/configure.html#config-file (opens new window)
rabbitmq.conf.example (opens new window)
【RabbitMQ】启用Web管理控制台
/usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_management
【RabbitMQ】查看、启动、重启、停止服务
# 查看状态
systemctl status rabbitmq-server
# 启动服务
systemctl start rabbitmq-server
# 重启服务
systemctl restart rabbitmq-server
# 停止服务
systemctl stop rabbitmq-server
2
3
4
5
6
7
8
9
10
11
【RabbitMQ】开放端口号
[root@localhost bin]# firewall-cmd --add-port=5672/tcp --permanent
success
[root@localhost bin]# firewall-cmd --add-port=15672/tcp --permanent
success
[root@localhost bin]# firewall-cmd --reload
2
3
4
5
在浏览器输入地址:http://192.168.66.133:15672/,账号密码均为:guest


【RabbitMQ】疑难杂症
Q:3月 22 18:07:05 localhost.localdomain rabbitmq-server[73958]: /usr/lib/rabbitmq/bin/rabbitmq-server: 第 82 行:exec: erl: 未找到
A:在 exec
前面加入
ERL_HOME=/usr/local/erlang
export PATH=$PATH:$ERL_HOME/bin
2
# 工作原理
基本概念:
A producer is a user application that sends messages.
A queue is a buffer that stores messages.
A consumer is a user application that receives messages.
An exchange is a very simple thing. On one side it receives messages from producers and the other side it pushes them to queues. There are a few exchange types available: direct
, topic
, headers
and fanout
.
A binding is a relationship between an exchange and a queue.
RabbitMQ核心概念以及工作原理 (opens new window)
RabbitMQ消息队列工作原理及集成使用 (opens new window)
# 消息模型
https://www.rabbitmq.com/getstarted.html (opens new window)
# Hello World(简单模式)
一个生产者,一个消费者,生产者和消费者直接跟队列打交道。(点对点,直连)
# Work Queues(工作队列)
工作队列,任务队列。有多个消费者,消息可以平均分发到消费者,也可以多劳多得。
# Publish/Subscribe(发布订阅)
发布/订阅。在该模型中,交换机会将接收的消息广播到它所知道的所有队列中。exchange=fanout
# Routing(静态路由)
静态路由。生产者发送消息给交换机,交换机根据路由键转发到对应的队列,然后由监听对应队列的消费者消费。exchange=direct

# Topics(动态路由)
动态路由。与静态路由不同,路由键是可以由多个单词组成,每个单词以.
分隔。可以使用*
及#
。exchange=topic
*
(star) can substitute for exactly one word.一个单词。
#
(hash) can substitute for zero or more words.零个或多个单词。

# 应用场景
- 异步处理
- 应用解耦
- 流量削峰