RabbitMQ搭建

Posted 还行少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ搭建相关的知识,希望对你有一定的参考价值。

一、环境

主机名IP地址操作系统rabbitmq版本
mq1192.168.10.180Centos 7rabbitmq3.9.7
mq2192.168.10.181Centos 7rabbitmq3.9.7
mq3192.168.10.183Centos 7rabbitmq3.9.7

二、单机部署

1.安装gcc gcc-c++等模块

[root@mq1 ~]#  yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel ncurses-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
Package 1:make-3.82-24.el7.x86_64 already installed and latest version
Package gcc-4.8.5-44.el7.x86_64 already installed and latest version
Package gcc-c++-4.8.5-44.el7.x86_64 already installed and latest version
Package kernel-devel-3.10.0-1160.59.1.el7.x86_64 already installed and latest version
Package m4-1.4.16-10.el7.x86_64 already installed and latest version
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-24.el7_9.x86_64 already installed and latest version
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Nothing to do
[root@mq1 ~]# 

2.安装erlang环境

2.1 下载erlang解压并重名名

[root@mq1 ~]# wget http://erlang.org/download/otp_src_24.0.tar.gz
--2022-02-28 14:41:08--  http://erlang.org/download/otp_src_24.0.tar.gz
Resolving erlang.org (erlang.org)... 192.121.151.106
Connecting to erlang.org (erlang.org)|192.121.151.106|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 101193664 (97M) [application/gzip]
Saving to: ‘otp_src_24.0.tar.gz’

100%[=============================================================================>] 101,193,664  250KB/s   in 6m 45s 

2022-02-28 14:47:54 (244 KB/s) - ‘otp_src_24.0.tar.gz’ saved [101193664/101193664]
[root@mq1 ~]# tar xf otp_src_24.0.tar.gz 
[root@mq1 ~]# mv otp_src_24.0 /usr/local/erlang

2.2 检查编译环境,配置安装路径

[root@mq1 ~]# cd /usr/local/erlang/
[root@mq1 erlang]# ./configure --prefix=/usr/local/erlang --without-javac

2.3 编译安装

[root@mq1 erlang]# make && make install

2.4 加入环境变量,如下即为成功

[root@mq1 erlang]# source /etc/profile
[root@mq1 erlang]# tail -n 1 /etc/profile
export PATH=$PATH:/user/local/erlang/bin

[root@mq1 erlang]# ln -s /usr/local/erlang/bin/erl /usr/local/bin/erl
[root@mq1 erlang]# erl
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V12.0  (abort with ^G)
1> 

3.安装rabbitmq

3.1 下载rabbitmq

[root@mq1 ~]# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.9.7/rabbitmq-server-generic-unix-3.9.7.tar.xz

3.2 配置hosts

[root@mq1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.180 mq1

3.3 安装rabbitmq

[root@mq1 ~]# tar xf rabbitmq-server-generic-unix-3.9.7.tar 
[root@mq1 ~]# ls
otp_src_24.0.tar.gz  rabbitmq_server-3.9.7  rabbitmq-server-generic-unix-3.9.7.tar
[root@mq1 ~]# mv rabbitmq_server-3.9.7/ /data/rabbitmq

3.4 添加环境变量

[root@mq1 ~]# cat >> /etc/profile << EOF
> export PATH=/data/rabbitmq/sbin:$PATH
> EOF
[root@mq1 ~]# source /etc/profile

3.5 启动mq后台管理方式

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

set 3 plugins.
Offline change; changes will take effect at broker restart.
[root@mq1 ~]# 

3.6 运行rabbitmq

[root@mq1 ~]# rabbitmq-server 
2022-02-28 15:35:46.446039+08:00 [info] <0.222.0> Feature flags: list of feature flags found:
2022-02-28 15:35:46.471099+08:00 [info] <0.222.0> Feature flags:   [ ] implicit_default_bindings
2022-02-28 15:35:46.471247+08:00 [info] <0.222.0> Feature flags:   [ ] maintenance_mode_status
2022-02-28 15:35:46.471353+08:00 [info] <0.222.0> Feature flags:   [ ] quorum_queue
2022-02-28 15:35:46.471446+08:00 [info] <0.222.0> Feature flags:   [ ] stream_queue
2022-02-28 15:35:46.471745+08:00 [info] <0.222.0> Feature flags:   [ ] user_limits
2022-02-28 15:35:46.471863+08:00 [info] <0.222.0> Feature flags:   [ ] virtual_host_metadata
2022-02-28 15:35:46.471958+08:00 [info] <0.222.0> Feature flags: feature flag states written to disk: yes
2022-02-28 15:35:47.253160+08:00 [noti] <0.44.0> Application syslog exited with reason: stopped
2022-02-28 15:35:47.253395+08:00 [noti] <0.222.0> Logging: switching to configured handler(s); following messages may not be visible in this log output

  ##  ##      RabbitMQ 3.9.7
  ##  ##
  ##########  Copyright (c) 2007-2021 VMware, Inc. or its affiliates.
  ######  ##
  ##########  Licensed under the MPL 2.0. Website: https://rabbitmq.com

  Erlang:      24 [emu]
  TLS Library: OpenSSL - OpenSSL 1.0.2k-fips  26 Jan 2017

  Doc guides:  https://rabbitmq.com/documentation.html
  Support:     https://rabbitmq.com/contact.html
  Tutorials:   https://rabbitmq.com/getstarted.html
  Monitoring:  https://rabbitmq.com/monitoring.html

  Logs: /data/rabbitmq/var/log/rabbitmq/rabbit@mq1.log
        /data/rabbitmq/var/log/rabbitmq/rabbit@mq1_upgrade.log
        <stdout>

  Config file(s): (none)

  Starting broker...
 completed with 3 plugins.

三、集群部署

1.安装mq(同上)

2.修改配置文件(其他两台对应修改mq2、mq3)

[root@mq1 ~]# cd /data/rabbitmq/etc/rabbitmq
[root@mq1 rabbitmq]# touch  rabbitmq-env.conf
[root@mq1 rabbitmq]# cat  rabbitmq-env.conf
NODENAME=rabbit@mq1
MNESIA_BASE=/data/rabbitmq/mnesia
LOG_BASE=/data/rabbitmq/log

3.重启进程,重载配置文件

[root@mq1 rabbitmq]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      6906/beam.smp       
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      5581/epmd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      7437/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6855/sshd: /usr/sbi 
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      6653/cupsd          
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      6906/beam.smp       
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      6910/master         
tcp        0      0 127.0.0.1:65432         127.0.0.1:4369          TIME_WAIT   -                   
tcp        0      0 192.168.10.180:4369     192.168.10.180:61106    TIME_WAIT   -                   
tcp        0      0 192.168.10.180:4369     192.168.10.180:61110    TIME_WAIT   -                   
tcp        0      0 127.0.0.1:4369          127.0.0.1:65438         ESTABLISHED 5581/epmd           
tcp        0    336 192.168.10.180:22       20.0.0.3:51939          ESTABLISHED 20039/sshd: root@pt 
tcp        0      0 192.168.10.180:4369     192.168.10.180:61112    TIME_WAIT   -                   
tcp        0      0 127.0.0.1:65434         127.0.0.1:4369          TIME_WAIT   -                   
tcp        0      0 127.0.0.1:65438         127.0.0.1:4369          ESTABLISHED 6906/beam.smp       
tcp6       0      0 :::5672                 :::*                    LISTEN      6906/beam.smp       
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::4369                 :::*                    LISTEN      5581/epmd           
tcp6       0      0 :::22                   :::*                    LISTEN      6855/sshd: /usr/sbi 
tcp6       0      0 ::1:631                 :::*                    LISTEN      6653/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      6910/master         
[root@mq1 rabbitmq]# kill -9 6906

[root@mq1 rabbitmq]# rabbitmq-plugins enable rabbitmq_management   
Enabling plugins on node rabbit@mq1:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@mq1...
Plugin configuration unchanged.

[root@mq1 rabbitmq]# rabbitmq-server -detached

4.添加用户(三台都要)

[root@mq1 rabbitmq]# rabbitmqctl add_user admin 123456
Adding user "admin" ...
Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.

[root@mq1 rabbitmq]# rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...

[root@mq1 rabbitmq]# rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...

[root@mq1 rabbitmq]# rabbitmqctl list_users
Listing users ...
user    tags
admin   [administrator]
guest   [administrator]

5.修改erlang.cookie权限,传送到其他服务器

[root@mq1 ~]# chmod 400 .erlang.cookie 
[root@mq1 ~]# scp .erlang.cookie 192.168.10.181:/root
The authenticity of host '192.168.10.181 (192.168.10.181)' can't be established.
ED25519 key fingerprint is SHA256:Q7berYeCACYwLcFtyUEZxThcf6TSoKTMMERfs0NUx3M.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.10.181' (ED25519) to the list of known hosts.
(root@192.168.10.181) Password: 
.erlang.cookie                                                                       100%   20    23.8KB/s   00:00    
[root@mq1 ~]# scp .erlang.cookie 192.168.10.183:/root
(root@192.168.10.183) Password: 
.erlang.cookie                                                                       100%   20    31.3KB/s   00:00    
[root@mq1 ~]# 

6.停止mq2和mq3的进程,加入集群

6.1 mq2作为磁盘节点

[root@mq2 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@mq2 ...

[root@mq2 ~]# rabbitmqctl join_cluster rabbit@mq1
Clustering node rabbit@mq2 with rabbit@mq1

[root@mq2 ~]# rabbitmqctl start_app
Starting node rabbit@mq2 ...

6.2 mq3作为内存节点

[root@mq3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@mq3 ...

[root@mq3 ~]# rabbitmqctl join_cluster --ram rabbit@mq1
Clustering node rabbit@mq3 with rabbit@mq1

[root@mq3 ~]# rabbitmqctl start_app
Starting node rabbit@mq3 ...

7.查看集群状态

[root@mq1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@mq1 ...
Basics

Cluster name: rabbit@mq1

Disk Nodes

rabbit@mq1
rabbit@mq2

RAM Nodes

rabbit@mq3

Running Nodes

rabbit@mq1
rabbit@mq2
rabbit@mq3

四、使用

1.修改rabbitmq密码

[root@mq1 ~]# rabbitmqctl  change_password  admin  '123123'
Changing password for user "admin" ...

未完待续

以上是关于RabbitMQ搭建的主要内容,如果未能解决你的问题,请参考以下文章

RabbitMQ环境搭建

RabbitMQ环境搭建

RabbitMQ:Docker环境下搭建rabbitmq集群

RabbitMQ集群环境搭建-4

Rabbitmq集群环境搭建

搭建RabbitMQ环境(windows)