主流消息队列
- 主流的消息队列:RabbitMQ、ActiveMQ、RocketMQ、Kafka、ZeroMQ
消息队列中的两种工作模式
- point-to-point(点到点)
- 一方发送消息,另外一方接收
- pub/sub
- 即发布/订阅模式,消费者可以订阅一个或多个主题并使用该主题中的所有消息
消息队列的缺点
- 系统可用性降低
- 系统复杂度提高
- 数据一致性无法保证
RabbitMQ相关术语
- 生产者:产生消息的进程或服务
- 消费者:接收消息的进程或服务
- 队列:真正存储消息数据
- 交换器:类似于网络设备交换机,根据不同的关键字,将消息发送到不同的队列中
- 虚拟主机:提供了资源的逻辑分组和 分隔,每一个虚拟主机本质上是mini版的RabbitMQ服务器
单节点实验
101消息队列 102生产者 102消费者
1.关闭防火墙和内核
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
2.使用阿里的本地光盘做镜像
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
3.安装开发环境
[root@localhost ~]# yum -y install erlang
4.安装rabbit环境
[root@localhost ~]# yum -y install rabbitmq-server
5.启动
[root@localhost ~]# systemctl start rabbitmq-server
6.查看rabbitmq当前工作状态
[root@localhost ~]# rabbitmq-plugins list
7.开启插件
[root@localhost ~]# rabbitmq-plugins enable rabbitmq_management
8.使用命令查看是否开启插件成功
[root@localhost ~]# rabbitmq-plugins list
9.启动插件后需要重启才可生效
[root@localhost ~]# systemctl restart rabbitmq-server
10.使用浏览器访问192.168.10.101:15672,用密码账号都是guest登录进去查看,可以看到下面的图形界面
11.添加代理
第一步先安装nginx
[root@localhost ~]# yum -y install nginx
12。在conf.d下创建并打开文件
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# ls
[root@localhost nginx]# cd conf.d/
[root@localhost conf.d]# vim rabbitmq.conf
server {
listen 80;
location /
{
proxy_pass http://127.0.0.1:15672;
}
}
13.使用命令测试语句是否正确
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
14.启动
[root@localhost conf.d]# systemctl start nginx
创建程序测试环境
打开102和103
15.安装环境
分别在102.103上安装
[root@localhost ~]# yum -y install python3
16.拉入安装包
pika-1.3.1-py3-none-any.whl
17.安装
[root@localhost ~]# pip3 install pika-1.3.1-py3-none-any.whl
18.拉入应用程序
send_message.sh拉入102
receive_message.sh拉入103
19.启动102应用程序
[root@localhost ~]# python3 send_message.sh
可以查看到生产者已经生产消息
20.启动103应用程序
[root@localhost ~]# python3 receive_message.sh
可以看到消息已经被消费
集群配置
1.将101.102.103修改设置名称
hostnamectl set-hostname mq01
hostnamectl set-hostname mq02
hostnamectl set-hostname mq01
(此时开启发送信息同步)全部进行bash
2.在host中写入三个主机IP和名称
[root@mq01 ~]# vim /etc/hosts
192.168.10.101 mq01
192.168.10.102 mq02
192.168.10.103 mq03
3.关闭并永久关闭防火墙和se
[root@mq01 ~]# systemctl stop firewalld
[root@mq01 ~]# setenforce 0
[root@mq01 ~]# systemctl disable firewalld
4.使用阿里的本地光盘
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
5.安装开发环境
[root@mq01 ~]# yum -y install erlang
6.安装rabbit环境
[root@localhost ~]# yum -y install rabbitmq-server
7.启动rabbit并设置为开机自启
[root@mq01 ~]# systemctl start rabbitmq-server
^[[A[root@mq01 ~]# systemctl enable rabbitmq-server
8.设置插件并重启生效
[root@mq01 ~]# rabbitmq-plugins enable rabbitmq_management
[root@mq01 ~]# systemctl restart rabbitmq-server
(取消同步)
9.拷贝
[root@mq01 ~]# scp /var/lib/rabbitmq/./erlang.cookie mq02:/var/lib/rabbitmq/
[root@mq01 ~]# scp /var/lib/rabbitmq/./erlang.cookie mq02:/var/lib/rabbitmq/
10.同步后重启
root@mq01 ~]# reboot
11.查看是否成功
[root@mq01 ~]# ps aux | grep rabbit
(关闭同步)
12.在102和103上关闭rabbit
[root@mq02/3 ~]# rabbitmqctl stop_app
Stopping node rabbit@mq02 ...
...done.
13.在102和103上加入101集群
[root@mq02/3 ~]#rabbitmqctl join_cluster --ram rabbit@mq01
14.再启动rabbit
[root@mq02/3 ~]# rabbitmqctl start_app
15.随便一台机器测试
[root@mq02 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@mq02 ...
[{nodes,[{disc,[rabbit@mq02]}]},
{running_nodes,[rabbit@mq02]},
{cluster_name,>},
{partitions,[]}]
...done.
出现以上结果为成功