Rabbit MQ
1.linux安装RabbitMQ
1. 首先下载erlang包和RabbitMQ包
- 下载erlang
wget https://packages.erlang-solutions.com/erlang-solutions-2.0-1.noarch.rpm
- 下载RabbitMQ(也可以直接去下载:https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/8/rabbitmq-server-3.9.9-1.el8.noarch.rpm
wget --content-disposition https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/8/rabbitmq-server-3.9.9-1.el8.noarch.rpm/download.rpm
2. 安装
- 先切换到你下载的包目录
- 先安装erlang
rpm -Uvh erlang-solutions-2.0-1.noarch.rpm
yum install -y erlang
- 安装完之后可以使用:erl -v查看是否安装成功
[root@hecs-x-medium-2-linux-20211102093112 ~]# erl -vErlang/OTP 24 [erts-12.0.4] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1]Eshell V12.0.4 (abort with ^G)1>
- 再安装rebbitmq之前先安装
yum install -y socat
- 安装rabbitmq以及运行和查看状态
[root@hecs-x-medium-2-linux-20211102093112 rabbitmq]# rpm -ivh rabbitmq-server-3.9.9-1.el8.noarch.rpm warning: rabbitmq-server-3.9.9-1.el8.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 6026dfca: NOKEYPreparing... ################################# [100%]Updating / installing... 1:rabbitmq-server-3.9.9-1.el8 ################################# [100%][root@hecs-x-medium-2-linux-20211102093112 rabbitmq]# systemctl start rabbitmq-server // 运行rabbitmq服务[root@hecs-x-medium-2-linux-20211102093112 rabbitmq]# systemctl status rabbitmq-server.service // 查看服务状态● rabbitmq-server.service - RabbitMQ broker Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2021-11-12 10:28:29 CST; 18s ago Main PID: 4551 (beam.smp) Tasks: 21 Memory: 63.8M CGroup: /system.slice/rabbitmq-server.service ├─4551 /usr/lib64/erlang/erts-12.0.4/bin/beam.smp -W w -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 104857... ├─4566 erl_child_setup 32768 ├─4610 inet_gethost 4 └─4611 inet_gethost 4Nov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: Doc guides: https://rabbitmq.com/documentation.htmlNov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: Support: https://rabbitmq.com/contact.htmlNov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: Tutorials: https://rabbitmq.com/getstarted.htmlNov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: Monitoring: https://rabbitmq.com/monitoring.htmlNov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: Logs: /var/log/rabbitmq/rabbit@hecs-x-medium-2-linux-20211102....logNov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: /var/log/rabbitmq/rabbit@hecs-x-medium-2-linux-20211102093112....logNov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: <stdout>Nov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: Config file(s): (none)Nov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 rabbitmq-server[4551]: Starting broker... completed with 0 plugins.Nov 12 10:28:29 hecs-x-medium-2-linux-20211102093112 systemd[1]: Started RabbitMQ broker.Hint: Some lines were ellipsized, use -l to show in full.[root@hecs-x-medium-2-linux-20211102093112 rabbitmq]#
- 开启开机自启动服务
[root@hecs-x-medium-2-linux-20211102093112 rabbitmq]# systemctl enable rabbitmq-serverCreated symlink from /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service to /usr/lib/systemd/system/rabbitmq-server.service.[root@hecs-x-medium-2-linux-20211102093112 rabbitmq]#
……
- 停止服务
systemctl stop rabbitmq-server
3. RabbitMQ的管理界面和授权操作
- 开启管理页面
rabbitmq-plugins enable rabbitmq_management
- 然后就可以使用linux的ip地址:15672去访问了,不过有一点需要注意,应该去云服务器开启这个端口规则。最好是linux也开启一下端口,记得重启防火墙。
- 默认密码我就不说了,我们来新建用户以及配置权限等问题。
rabbitmqctl add_user admin admin
// 新增用户
rabbitmqctl change_password admin shisan.666
// 更改密码
rabbitmqctl set_user_tags admin administrator
// 设置用户操作权限
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
// 给用户分配资源权限1. administrator:可以登录控制台查看所有信息,可以对rabbitmq进行管理2. monitoring:监控者,登录控制台,查看所有信息3. policymaker:策略制定者,登录控制台,指定策略4. management:普通管理员,登录控制台
2.使用docker安装RabbitMQ
1. 首先在linux安装好docker(怎么安装可以参考我的另一篇文章:https://www.kuangstudy.com/bbs/1456094658911289345 )
2. 安装以及配置账户密码和图像管理界面
docker run -di --name myrabbit -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p 5672:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 rabbitmq:management
docker run -di --name myrabbit -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15673:15672 -p 5673:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 rabbitmq:management
3. 安装完之后运行镜像然后访问即可
[root@hecs-x-medium-2-linux-20211102093112 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6eee7855267c rabbitmq:management "docker-entrypoint.s…" 11 seconds ago Up 10 seconds 4369/tcp, 0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 15671/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp, 0.0.0.0:25672->25672/tcp, :::25672->25672/tcp, 0.0.0.0:61613->61613/tcp, :::61613->61613/tcp, 15691-15692/tcp myrabbit[root@hecs-x-medium-2-linux-20211102093112 ~]# docker start 6eee7855267c6eee7855267c[root@hecs-x-medium-2-linux-20211102093112 ~]#
3. Send && Receive
//Receive
var factory = new ConnectionFactory() { HostName = "106.12.137.32", UserName = "admin", Password = "admin", Port = 5673 };
using (var connection = factory.CreateConnection("消费者"))
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
};
channel.BasicConsume(queue: "hello",
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
//配置连接
var factory = new ConnectionFactory() { HostName = "106.12.137.32", UserName = "admin", Password = "admin", Port = 5673 };
//创建连接
using (var connection = factory.CreateConnection("生成者"))
//获取channel
using (var channel = connection.CreateModel())
{
int index = 0;
while (true)
{
//queue 队列名称
//durable 是否持久化
//exclusive 排他性
//autoDelete 是否自动删除(最后一个消费者消费完毕)
//arguments 参数
channel.QueueDeclare(queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!"+index;
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
index++;
}
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
4. MQ的工作模式【简单模式,工作队列模式,发布订阅模式,路由模式,主题模式,RPC,发布与确认机制】
1:执行发送,这个时候可以在web控制台查看到这个队列queue的信息
2:我们可以进行对队列的消息进行预览和测试如下:
3:进行预览和获取消息进行测试
5.MQ核心组成
核心概念:
Server:又称Broker ,接受客户端的连接,实现AMQP实体服务。 安装rabbitmq-server
Connection:连接,应用程序与Broker的网络连接 TCP/IP/ 三次握手和四次挥手
Channel:网络信道,几乎所有的操作都在Channel中进行,Channel是进行消息读写的通道,客户端可以建立对各Channel,每个Channel代表一个会话任务。
Message :消息:服务与应用程序之间传送的数据,由Properties和body组成,Properties可是对消息进行修饰,比如消息的优先级,延迟等高级特性,Body则就是消息体的内容。
Virtual Host 虚拟地址,用于进行逻辑隔离,最上层的消息路由,一个虚拟主机理由可以有若干个Exhange和Queueu,同一个虚拟主机里面不能有相同名字的Exchange
Exchange:交换机,接受消息,根据路由键发送消息到绑定的队列。(不具备消息存储的能力)
Bindings:Exchange和Queue之间的虚拟连接,binding中可以保护多个routing key.
Routing key:是一个路由规则,虚拟机可以用它来确定如何路由一个特定消息。
Queue:队列:也成为Message Queue,消息队列,保存消息并将它们转发给消费者。
MQ架构组成
MQ运行流程
路由模式 新建交换机 fanout
topic 模式
新建交换机 topic -->绑定路由 对比路由模式 相当于根据多个路由的名称进行匹配 将消息放到队列中
#: 0级1级或者多级 * :一级
header模式 参数交换机