RabbitMQ消息队列服务部署

环境介绍


OS:CentOS7.4

RabbitMQ:3.7.7

依赖环境:erlang、socat


端口介绍:

4369 ##erlang发现口

5672 ##client端通信口

15672  ##管理界面ui端口

25672  ##server间内部通信


依赖环境安装

[root@localhost yum.repos.d]# cat a.repo 
[bbitmq-erlang] 
name=rabbitmq-erlang 
baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/20/el/7
gpgcheck=1 
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc 
enabled=1

导入gpgcheck

[root@localhost yum.repos.d]# rpm --import https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey

安装socat

[root@localhost yum.repos.d]# yum -y install socat

下载RabbitMQ安装包


[root@localhost ~]# wget https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.7.7-1.el7.noarch.rpm
[root@localhost ~]# yum -y localinstall rabbitmq-server-3.7.7-1.el7.noarch.rpm

启用RabbitMQ的web插件

[root@localhost ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch

started 3 plugins.
[root@localhost ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      38383/epmd          
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1253/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1015/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1012/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1182/master         
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      38454/beam.smp      
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::4369                 :::*                    LISTEN      38383/epmd          
tcp6       0      0 :::22                   :::*                    LISTEN      1015/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1012/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1182/master         
tcp6       0      0 :::5672                 :::*                    LISTEN      38454/beam.smp

开机启动rabbit

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

启动rabbitmq

[root@localhost ~]# systemctl restart rabbitmq-server
Job for rabbitmq-server.service failed because the control process exited with error code. See "systemctl status rabbitmq-server.service" and "journalctl -xe" for details.

查看状态

[root@localhost ~]# systemctl status rabbitmq-server
● rabbitmq-server.service - RabbitMQ broker
   Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-01-15 01:24:21 CST; 1min 19s ago
  Process: 41443 ExecStop=/usr/sbin/rabbitmqctl shutdown (code=exited, status=69)
 Main PID: 41607 (beam.smp)
   Status: "Initialized"

一般这个状态是因为文件".erlang.cookie"用户和用户组权限的问题
将该文件通过chown授权给rabbitmq

启动rabbitmq,查看端口

[root@localhost ~]# netstat -antlp | grep 5672
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      3641/beam.smp       
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      3641/beam.smp       
tcp        0      0 127.0.0.1:25672         127.0.0.1:56251         TIME_WAIT   -                   
tcp6       0      0 :::5672                 :::*                    LISTEN      3641/beam.smp

浏览器访问测试image.png

用户名密码都是guest

image.png

rabbit搭建完成!

RabbitMQ使用方法

  • 查看当前的连接

[root@localhost ~]# rabbitmqctl list__connectios
  • 列出多有队列及队列的消息数

[root@localhost ~]# rabbitmqctl list__queues
  • 查看当前队列消息


[root@localhost ~]# rabbitmqctl status
Status of node rabbit@localhost ...
[{pid,3641},
...
  • 设置用户权限为超级管理员权限

[root@localhost ~]# rabbitmqctl set_user_tags root administartor

image.png


  • 创建虚拟机管理消息队列

[root@localhost ~]# rabbitmqctl add_vhost read
Adding vhost "read" ...
  • 删除虚拟机

[root@localhost ~]# rabbitmqctl delete_vhost read
  • 查看当前虚拟机

[root@localhost ~]# rabbitmqctl list_vhosts
Listing vhosts ...
/  ##默认虚拟机
read

RabbitMQ用户管理、角色和权限管理

  • 创建用户

[root@localhost ~]# rabbitmqctl add_user {user}root {password}123456
  • 删除用户

[root@localhost ~]# rabbitmqctl add_user {username}
  • 修改用户密码

[root@localhost ~]# rabbitmqctl change_password {username} {newpassword}
  • 查看用户列表

[root@localhost ~]# rabbitmqctl list_users

使用python调用rabbitmq测试

  • 安装py

[root@localhost ~]# yum -y install python-pip
  • 安装epel扩展源

[root@localhost ~]# yum -y install epel-release
  • 更新pip

[root@localhost ~]# pip install --upgrade pip
  • 安装pika模块

[root@localhost ~]# pip install pika

创建生产者代码


[root@localhost ~]# cat send.py 
#!/bin/env python
import pika
#指定连接的主机参数为localhost
connection=pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
#连接引导
channel=connection.channel()
#声明rabbitmq的队列BindingKey
channel.queue_declare(queue='hello')
#指定Exchange匹配模式,指定RoutingKey
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print("[x] Sent 'Hello World'")
#关闭连接
connection.close()

创建消费者代码


[root@localhost ~]# cat receive.py 
#!/usr/bin/env python 
import pika 
#指定连接的主机参数为localhost
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) 
#连接引导
channel = connection.channel() 
#声明rabbitmq的队列BindingKey 
channel.queue_declare(queue='hello') 
#指定调用的消息 
def callback(ch, method, properties, body):     
print(" [x] Received %r" % body) 
 
channel.basic_consume(callback,                       
              queue='hello',                       
              no_ack=True) 
 
print(' [*] Waiting for messages. To exit press CTRL+C') 
channel.start_consuming()

测试脚本

[root@localhost ~]# python send.py 
[x] Sent 'Hello World'

查看浏览器,消息队列产生了两个(脚本执行两次)且已处理完成

image.png








猜你喜欢

转载自blog.51cto.com/13944252/2342714