RabbitMQ消息队列

什么是消息队列

▪ 消息(Message)是指在应用间传送的数据

▪ 消息队列(Message Queue)是一种应用间的通信方式解决方法,确保消息的可靠传递

▪ 主流消息队列

▪ 目前主流的几大消息队列有:RabitMQ、ActiveMQ、RocketMQ、Kafka、ZeroMQ等,也有一些小众的比如Beanstalk,Redis也可以实现消息队列的功能

▪ 消息队列名词

▪ Broker:消息服务器,作为server提供消息核心服务

▪ Topic:每条发布到Kafka集群的消息都有一个分类,这个类别被称为Topic (主题)

▪ Producer:指消息的生产者,负责发布消息到kafka broker

▪ Consumer:指消息的消费者,从kafka broker拉取数据,并消费这些已发布的消息

▪ Partition:Partition是物理上的概念,每个Topic包含一个或多个Partition,每个Partition都是一个有序的队列。Partition中的每条消息都会被分配一个有序的id(offset)

▪ Consumer Group:消费者组,可以给每个Consumer指定消费组,若不指定消费者组,则属于默认的Group

▪ Message:消息,通信的基本单位,每个producer可以向一个topic发布一些消息

消息队列中两种工作模式

▪ Point-to-Point

▪ 一方发送消息,另外一方接收

▪ Pub/Sub

▪ 即发布/订阅模式,消费者可以订阅一个或多个主题并使用该主题中的所有消息

消息队列的缺点

▪ 系统可用性降低

▪ 系统复杂性提高

▪ 数据一致性无法保证

RabbitMQ相关术语

(1)生产者:产生消息的进程或服务

(2)消费者:接收消息的进程或服务

(3)队列:RabbitMQ是消息队列中间件,而真正储存消息数据的就是队列,队列可以有很多

(4)交换器:类似于网络设备交换机,它可以根据不同的关键字,将消息发送到不同的队列

(5)虚拟主机:虚拟主机提供了资源的逻辑分组和分隔,每一个虚拟主机本质上是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.列出当前虚拟主机

[root@localhost ~]# rabbitmqctl list

2.添加zhangsan和fll

[root@localhost ~]# rabbitmqctl add_vhost zhangsan

[root@localhost ~]# rabbitmqctl add_vhost fll

可以查看到:

3.创建账号并设置密码

[root@localhost ~]# rabbitmqctl add_user user1

4.使用列出可以查看到

[root@localhost ~]# rabbitmqctl list_users

Listing users ...

guest [administrator]

user1 []

...done.

5..给user1设置角色management

[root@localhost ~]# rabbitmqctl set_user_tags user1 management

6.列出可以查看

[root@localhost ~]# rabbitmqctl list_users

Listing users ...

guest [administrator]

user1 [management]

...done.

7. 设置用户张三的配置权,读写权,交换器的管理权

[root@localhost ~]# rabbitmqctl set_permissions -p zhangsan user1 '.*' '.*' '.*'

集群配置

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

0

(关闭同步)

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.

出现以上结果为成功

  • 20
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值