RabbitMQ消息队列

1:安装erlang

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# setenforce 0

RabbitMQ服务器是用Erlang语言编写的

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

yum install -y erlang

2:安装RabbitMQ

[root@localhost ~]# yum install -y rabbitmq-server

3:启动RabbitMQ

[root@localhost ~]# systemctl start rabbitmq-server 

[root@localhost ~]# ps aux |grep rabbit ## 查看rabbit进程

[root@localhost ~]# netstat -lntp ## 查看监听端口

4:开启web管理控制台

[root@localhost ~]# rabbitmq-plugins enable rabbitmq_management

##激活插件

[root@localhost ~]# rabbitmq-plugins list

## 查看所有插件

此时可以通过 http://ip:15672 来访问rabbitmq的web管理控制台,用户名密码都是guest,但是有个限制,只允许127.0.0.1访问,所以还需在本机配置一个nginx代理

5:Nginx代理

[root@localhost ~]# yum install -y nginx   ##安装nginx

vim /etc/nginx/conf.d/rabbitmq.conf

server {

listen 80;

server_name www.fllrabbit.com;

        location /

        {

            proxy_pass http://127.0.0.1:15672;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     }

}

[root@localhost ~]# systemctl start nginx  

[root@localhost ~]# systemctl start rabbitmq-server 

现在可以通过浏览器访问rabbit,账号密码都是:guest

6:发送消息到rabbitmq

(1)打开另一台主机安装python3

[root@localhost ~]# yum -y install python3

[root@localhost ~]# pip3 install pika

(2)编辑测试程序

[root@localhost ~]# vim send_message.sh

import pika

 

# 连接到RabbitMQ服务器

connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.10.101'))

channel = connection.channel()

 

# 声明队列;如果队列不存在会被创建

channel.queue_declare(queue='test_queue', durable=True)

 

# 发送消息到队列中

channel.basic_publish(

    exchange='',

    routing_key='test_queue',

    body='Hello, RabbitMQ!',

    properties=pika.BasicProperties(

        delivery_mode=2,  # 使消息持久化

    )

)

 

print("消息发送完毕")

 

# 关闭连接

connection.close()

(3)测试发送消息

[root@localhost ~]# python3 send_message.sh

(4)查看RabbitMQ

7:从RabbitMQ接收消息

(1)安装python3

打开第三台主机

[root@localhost ~]# yum -y install python3

[root@localhost ~]# pip3 install pika

2编写测试程序

[root@localhost ~]# vim receice_message.sh

import pika

 

# 连接到RabbitMQ服务器

connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.10.101'))

channel = connection.channel()

 

# 声明队列,确保RabbitMQ中有一个名为'test_queue'的队列

channel.queue_declare(queue='test_queue', durable=True)

 

# 定义回调函数来处理消息

def callback(ch, method, properties, body):

    print(f"Received {body.decode()}")

 

# 消费队列中的消息,回调函数为callback

channel.basic_consume(queue='test_queue', on_message_callback=callback, auto_ack=True)

 

print("Waiting for messages. To exit press CTRL+C")

# 开始监听消息

channel.start_consuming()

(3)执行程序接收消息

[root@localhost ~]# python3 receice_message.sh

Waiting for messages. To exit press CTRL+C

Received Hello, RabbitMQ!

Received Hello, RabbitMQ!

Received Hello, RabbitMQ!

注意:

可以一边发送消息,一边观察接收到的消息。

rabbitmq常用命令

1:虚拟机管理

[root@localhost ~]# rabbitmqctl list_vhosts  

##列出所有的虚拟主机

[root@localhost ~]# rabbitmqctl add_vhost fll 

##创建名字叫fll的虚拟主机

[root@localhost ~]# rabbitmqctl delete_vhost fll

##删除名字叫fll的虚拟主机

2:用户管理

[root@localhost ~]# rabbitmqctl add_user user1 user1_passwd

##创建user1用户,密码为user1_passwd

[root@localhost ~]# rabbitmqctl list_users

##列出所有用户

[root@localhost ~]# rabbitmqctl change_password user1 new_passwd

##更改user1的密码为new_passwd

[root@localhost ~]# rabbitmqctl delete_user user1

#删除user1用户

备注:

语法汇总

rabbitmqctl list_users #列出用户

rabbitmqctl add_user <username> <password> #创建用户

rabbitmqctl change_password <username> <password> ##更改用户密码

rabbitmqctl delete_user <username> #删除用户

rabbitmqctl clear_password <username> #清除用户密码

3:tags角色介绍

(1) 超级管理员(administrator):guest

可登陆管理控制台(启用management plugin的情况下),可查看所有的信息,并且可以对用户,策略(policy)进行操作。

(2) 监控者(monitoring)

可登陆管理控制台(启用management plugin的情况下),同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)

(3) 策略制定者(policymaker)

可登陆管理控制台(启用management plugin的情况下), 同时可以对policy进行管理。但无法查看节点的相关信息。

(4) 普通管理者(management)

仅可登陆管理控制台(启用management plugin的情况下),无法看到节点信息,也无法对策略进行管理。

(5) 其他

无法登陆管理控制台,通常就是普通的生产者和消费者。

[root@localhost ~]# rabbitmqctl add_user user1 user1_passwd

[root@localhost ~]# rabbitmqctl set_user_tags user1 managemnet  

##赋予user1用户management角色

[root@localhost ~]# rabbitmqctl add_vhost fll

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

##针对fll虚拟主机给user1用户设置所有的配置、读写queue和exchange的权限。默认是没有任何权限的

说明:用户权限指的是用户对exchange(交换器),queue(队列)的操作权限,包括配置权限,读写权限。配置权限会影响到exchange,queue的声明和删除。读写权限影响到从queue里取消息,向exchange发送消息以及queue和exchange的绑定(bind)操作。例如: 将queue绑定到某exchange上,需要具有queue的可写权限,以及exchange的可读权限;向exchange发送消息需要具有exchange的可写权限;从queue里取数据需要具有queue的可读权限。

[root@localhost ~]# rabbitmqctl list_user_permissions user1

##列出user1的权限

[root@localhost ~]# rabbitmqctl list_permissions -p fll

##列出fll下的所有用户权限

备注:

如果需要清除user2在fll上的权限

[root@localhost ~]# rabbitmqctl clear_permissions -p fll user2

备注:

其他操作语法

rabbitmqctl list_user_permissions <username>

#列出某用户的权限,即该用户对哪个虚拟主机有权限

rabbitmqctl list_permissions -p <vhostname>

#列出指定虚拟主机下所有用户的权限,即哪些用户对该虚拟主机有权限

rabbitmqctl clear_permissions -p <vhostname> <user>

#清除某用户在指定虚拟机上的授权

4:插件管理

rabbitmq-plugins list   ##获取RabbitMQ插件列表

rabbitmq-plugins enable <插件名字> ##安装RabbitMQ插件

rabbitmq-plugins disable <插件名字> ##卸载某个插件

5:限制

rabbitmqctl set_vhost_limits -p vhost_name '{"max-connections": 256}'

#设置虚拟主机的最大连接数

rabbitmqctl set_vhost_limits -p vhost_name '{"max-connections": 0}'

#不允许客户端连接虚拟主机

rabbitmqctl set_vhost_limits -p vhost_name '{"max-connections": -1}'

#不限制连接数

rabbitmqctl set_vhost_limits -p vhost_name '{"max-queues": 1024}'

#限制虚拟主机里最大的队列数

rabbitmqctl set_vhost_limits -p vhost_name '{"max-queues": -1}'

#不限制队列数

6:其他

rabbitmqctl list_exchanges

#列出所有的交换器

rabbitmqctl list_bindings

#列出所有的绑定,即把exchange和queue按照路由规则绑定起来

rabbitmqctl list_queues

 #分别查看当前系统种存在的Exchange和Exchange上绑定的Queue信息。

rabbitmqctl status  

#查看运行信息

RabbitMQ集群

RabbitMQ本身是基于Erlang编写的,Erlang天生支持分布式(通过同步Erlang集群各节点的cookie来实现),因此不需要像Kafka那样通过ZooKeeper来实现分布式集群。

1:元数据

RabbitMQ内部有各种基础构件,包括队列、交换器、绑定、虚拟主机等,他们组成了AMQP协议消息通信的基础,而这些构件以元数据的形式存在

2:内存节点与磁盘节点

在集群中的每个节点,要么是内存节点,要么是磁盘节点,如果是内存节点,会将所有的元数据信息仅存储到内存中,而磁盘节点则不仅会将所有元数据存储到内存上, 还会将其持久化到磁盘。所以在搭建集群的时候,为了保证数据的安全性和性能,最好是两种节点都要有

3:规划

主机名

Ip

节点类型

centos01

192.168.10.101

磁盘节点

Centos02

192.168.10.102

内存节点

Centos03

192.168.10.103

内存节点

4:部署集群

(1)配置hosts以及hostname

三台机器设置hostname

hostnamectl set-hostname mq01

hostnamectl set-hostname mq02

hostnamectl set-hostname mq03

三台机器上都需要编辑如下hosts

192.168.10.101 mq01

192.168.10.102 mq02

192.168.10.103 mq03

(2)关闭selinux以及firewalld

三台机器都要执行

setenforce 0

systemctl stop firewalld

systemctl disable firewalld

(3)安装rabbitmq

RabbitMQ服务器是用Erlang语言编写的

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

yum install -y erlang

[root@localhost ~]# yum install -y rabbitmq-server

(4)启动服务

三台机器都启动起来

[root@localhost ~]# systemctl start rabbitmq-server 

[root@localhost ~]# systemctl enable rabbitmq-server 

[root@localhost ~]# ps aux |grep rabbit ## 查看rabbit进程

[root@localhost ~]# netstat -lntp ## 查看监听端口

(5)安装management插件

三台机器都要开启

rabbitmq-plugins enable rabbitmq_management

(6)同步cookie

在mq01上将mq01的cookie文件同步到mq02和mq03

scp /var/lib/rabbitmq/.erlang.cookie root@mq02:/var/lib/rabbitmq/

scp /var/lib/rabbitmq/.erlang.cookie root@mq03:/var/lib/rabbitmq/

(7)重启三台主机并检查三台主机的rabbitmq服务

[root@localhost ~]# ps aux |grep rabbit ## 查看rabbit进程

[root@localhost ~]# netstat -lntp ## 查看监听端口

(8)分配节点

centos01为磁盘节点,centos02和centos03为内存节点

centos02和centos03上都执行:

停止rabbitmq

[root@localhost ~]# rabbitmqctl stop_app  

备注:

表示终止RabbitMQ的应用,但是Erlang节点还在运行。在管理RabbitMQ应用时需要停止rabbitmq,但是又要使用erlang对rabbitmq进行管理。

将centos02作为内存节点连接到centos01

[root@localhost ~]# rabbitmqctl join_cluster --ram rabbit@mq01

开启rabbitmq

[root@localhost ~]# rabbitmqctl start_app

查看集群状态

[root@localhost ~]# rabbitmqctl cluster_status

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值