RabbitMQ学习一 环境搭建
1.1、确定安装的操作系统的版本
在使用RabbitMQ之前必须预先安装配置,参考RabbitMQ官网说明,RabbitMQ支持多平台安装,例如Linux、Windows、MacOS、Docker等。不同架构的芯片对应的RabbitMQ安装包是有区别的。目前市场上主流的芯片架构有:X86架构、ARM架构、RISC-V架构和MIPS架构这四种。
例如,在Linux系统中,如果想查看CentOS版本信息,可以通过以下命令来获取。
可以看到el7 ,后续下载RabbitMQ和erlang时也需要下载el7版本的软件
[root@user ~]# cat /proc/version
Linux version 5.11.12-300.el7.aarch64 (root@centos7.9) (gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3), GNU ld version 2.30-55.el7.2) #1 SMP Thu Aug 19 09:02:08 UTC 2021
[root@user ~]# uname -a
Linux node2 5.11.12-300.el7.aarch64 #1 SMP Thu Aug 19 09:02:08 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux
[root@user ~]# uname -r
5.11.12-300.el7.aarch64
这里,我们以CentOS 7.X系统(ARM架构和X86架构)和Windows系统环境为例,介绍RabbitMQ的安装配置过程。
1.2、erlang语言介绍
Erlang(['ə:læŋ])是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信所辖的CS-Lab开发,目的是创造一种可以应对大规模并发活动的编程语言和运行环境。
最初是由爱立信专门为通信应用设计的,比如控制交换机或者变换协议等,因此非常适合构建分布式、实时软并行计算系统。Erlang运行时环境是一个虚拟机,有点像Java的虚拟机,这样代码一经编译,同样可以随处运行。
下载Erlang时需要注意版本兼容性问题,参考官方文档:https://www.rabbitmq.com/which-erlang.html
在RabbitMQ发布的所有版本中,支持CentOS 7.X的最高一个版本是rabbitmq-server-3.10.0-1.el7.noarch。
1.3、下载erlang在x86下的rpm包
下载的地址:
https://packagecloud.io/rabbitmq/erlang
建议用微软的edge浏览器打开,搜索el7,去选择相应的erlang版本下载。
我们选择erlang的版本是22.0.7-1
rpm -ivh erlang-22.0.7-1.el7.x86_64.rpm
查看是否安装成功
erl -v
1.4、下载安装Socat
ARM架构的CentOS虚拟机中在安装Erlang时,默认已经安装了Socat,因此无需重复安装Socat。而X86架构的CentOS虚拟机中在安装Erlang时,默认没有安装Socat,因此需要手动安装Socat。
Socat是Linux下的一个多功能的网络工具,名字来由是“Socket CAT”。其功能与有瑞士军刀之称的Netcat类似,可以看做是Netcat的加强版。
Socat的主要特点就是在两个数据流之间建立通道,且支持众多协议和链接方式。如IP、TCP、UDP、IPv6、PIPE、EXEC、System、Open、Proxy、Openssl、Socket等。Socat的官方网站:http://www.dest-unreach.org/socat。
下载地址
http://repo.iotti.biz/CentOS/7/x86_64/
拷贝socat-1.7.3.2-5.el7.lux.x86_64.rpm到/opt目录。
安装socat
[root@user opt]# rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm
查看Socat的版本信息。
[root@user opt]# yum info socat
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
可安装的软件包
名称 :socat
架构 :aarch64
版本 :1.7.3.2
发布 :2.el7
大小 :279 k
源 :base/7/aarch64
简介 :Bidirectional data relay between two data channels ('netcat++')
网址 :http://www.dest-unreach.org/socat
协议 :GPLv2
描述 :Socat is a relay for bidirectional data transfer between two independent data
: channels. Each of these data channels may be a file, pipe, device (serial line
: etc. or a pseudo terminal), a socket (UNIX, IP4, IP6 - raw, UDP, TCP), an
: SSL socket, proxy CONNECT connection, a file descriptor (stdin etc.), the GNU
: line editor (readline), a program, or a combination of two of these.
1.5、下载安装RabbitMQ
https://www.rabbitmq.com/download.html
采取rpm的安装方式,
1、点击downloading进入到Direct Downloads,
2、进入github之后,点击Tags,选择要下载的版本
rpm -ivh rabbitmq-server-3.7.17-1.el7.noarch.rpm
二、RabbitMQ相关命令
2.1、启动相关命令
RabbitMQ安装好之后会创建一个服务,这个因此可以通过systemctl这个命令来启动MQ
systemctl start|restart|stop|status rabbitmq-service
因为在安装好之后有一个rabbitmq-server.service文件在
/usr/lib/systemd/system/rabbitmq-server.service
被systemctl管理的命令,在/usr/lib/systemd/system/
可以找到相应的文件
/etc/systemd/system:系统或用户自定义的配置文件
/run/systemd/system:软件运行时生成的配置文件
/usr/lib/systemd/system:系统或第三方软件安装时添加的配置文件。
2.2、管理相关的命令
安装好rabbitmq之后,会有以下两个命令
/usr/lib/rabbitmq/bin/rabbitmqctl
/usr/sbin/rabbitmqctl
因此可以采用rabbitmqctl作为管理RabbitMQ的命令。
如果想知道当前用户执行的是哪个命令
type rabbitmqctl
采用rabbitmqctl查看有哪些管理命令
Usage
rabbitmqctl [--node <node>] [--timeout <timeout>] [--longnames] [--quiet] <command> [<command options>]
Available commands:
Help:
help Displays usage information for a command
version Displays CLI tools version
Nodes:
await_startup Waits for the RabbitMQ application to start on the target node
reset Instructs a RabbitMQ node to leave the cluster and eturn to its virgin state
rotate_logs Instructs the RabbitMQ node to perform internal log rotation
shutdown Stops RabbitMQ and its runtime (Erlang VM). Monitors progress for local nodes. Does not require a PID file path.
start_app Starts the RabbitMQ application but leaves the runtime (Erlang VM) running
stop Stops RabbitMQ and its runtime (Erlang VM). Requires a local node pid file path to monitor progress.
stop_app Stops the RabbitMQ application, leaving the runtime (Erlang VM) running
wait Waits for RabbitMQ node startup by monitoring a local PID file. See also 'rabbitmqctl await_online_nodes'
Cluster:
await_online_nodes Waits for <count> nodes to join the cluster
change_cluster_node_type Changes the type of the cluster node
cluster_status Displays all the nodes in the cluster grouped by node type, together with the currently running nodes
force_boot Forces node to start even if it cannot contact or rejoin any of its previously known peers
force_reset Forcefully returns a RabbitMQ node to its virgin state
forget_cluster_node Removes a node from the cluster
join_cluster Instructs the node to become a member of the cluster that the specified node is in
rename_cluster_node Renames cluster nodes in the local database
update_cluster_nodes Instructs a cluster member node to sync the list of known cluster members from <seed_node>
Replication:
cancel_sync_queue Instructs a synchronising mirrored queue to stop synchronising itself
sync_queue Instructs a mirrored queue with unsynchronised mirrors (follower replicas) to synchronise them
Users:
add_user Creates a new user in the internal database
authenticate_user Attempts to authenticate a user. Exits with a non-zero code if authentication fails.
change_password Changes the user password
clear_password Clears (resets) password and disables password login for a user
delete_user Removes a user from the internal database. Has no effect on users provided by external backends such as LDAP
list_users List user names and tags
set_user_tags Sets user tags
Access Control:
clear_permissions Revokes user permissions for a vhost
clear_topic_permissions Clears user topic permissions for a vhost or exchange
list_permissions Lists user permissions in a virtual host
list_topic_permissions Lists topic permissions in a virtual host
list_user_permissions Lists permissions of a user across all virtual hosts
list_user_topic_permissions Lists user topic permissions
list_vhosts Lists virtual hosts
set_permissions Sets user permissions for a vhost
set_topic_permissions Sets user topic permissions for an exchange
Monitoring, observability and health checks:
environment Displays the name and value of each variable in the application environment for each running application
list_bindings Lists all bindings on a vhost
list_channels Lists all channels in the node
list_ciphers Lists cipher suites supported by encoding commands
list_connections Lists AMQP 0.9.1 connections for the node
list_consumers Lists all consumers in a vhost
list_exchanges Lists exchanges
list_hashes Lists hash functions supported by encoding commands
list_queues Lists queues and their properties
list_unresponsive_queues Tests queues to respond within timeout. Lists those which did not respond
node_health_check Performs several opinionated health checks of the target node
ping Checks that the node OS process is up, registered with EPMD and CLI tools can authenticate with it
report Generate a server status report containing a concatenation of all server status information for support purposes
schema_info Lists schema database tables and their properties
status Displays broker status information
Parameters:
clear_global_parameter Clears a global runtime parameter
clear_parameter Clears a runtime parameter.
list_global_parameters Lists global runtime parameters
list_parameters Lists runtime parameters for a virtual host
set_global_parameter Sets a runtime parameter.
set_parameter Sets a runtime parameter.
Policies:
clear_operator_policy Clears an operator policy
clear_policy Clears (removes) a policy
list_operator_policies Lists operator policy overrides for a virtual host
list_policies Lists all policies in a virtual host
set_operator_policy Sets an operator policy that overrides a subset of arguments in user policies
set_policy Sets or updates a policy
Virtual hosts:
add_vhost Creates a virtual host
clear_vhost_limits Clears virtual host limits
delete_vhost Deletes a virtual host
list_vhost_limits Displays configured virtual host limits
restart_vhost Restarts a failed vhost data stores and queues
set_vhost_limits Sets virtual host limits
trace_off
trace_on
Node configuration:
decode Decrypts an encrypted configuration value
encode Encrypts a sensitive configuration value
set_cluster_name Sets the cluster name
set_disk_free_limit Sets the disk_free_limit setting
set_log_level Sets log level in the running node
set_vm_memory_high_watermark Sets the vm_memory_high_watermark setting
Operations:
close_all_connections Instructs the broker to close all connections for the specified vhost or entire RabbitMQ node
close_connection Instructs the broker to close the connection associated with the Erlang process id
eval Evaluates a snippet of Erlang code on the target node
exec Evaluates a snippet of Elixir code on the CLI node
force_gc Makes all Erlang processes on the target node perform/schedule a full sweep garbage collection
hipe_compile Performs HiPE-compilation of [some] server modules to the given directory to be used with RABBITMQ_SERVER_CODE_PATH
Queues:
delete_queue Deletes a queue
purge_queue Purges a queue (removes all messages in it)
Other:
enable_feature_flag
list_feature_flags
Use 'rabbitmqctl help <command>' to learn more about a specific command
2.3、插件相关的命令
rabbitmq中的一些插件可以通过插件命令来管理
rabbitmq-plugins enable|list|disable
三、开启管理页面
3.1、配置文件
# 开启管理界面
rabbitmq-plugins enable rabbitmq_management
# 配置远程可使用guest登录mq
cd /usr/share/doc/rabbitmq-server-3.7.17
cp rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
# 修改配置文件
vi /etc/rabbitmq/rabbitmq.config
注意配置/etc/rabbitmq/rabbitmq.config
3.2、解决guest用户的访问
1、修改配置文件
[{rabbit, [{loopback_users, []}]}].
2、添加别的用户
[root@rabbitmq1 ebin]# rabbitmqctl add_user admin 111111
Adding user "admin" ...
[root@rabbitmq1 ebin]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...
[root@rabbitmq1 ebin]# rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...
3、注意访问时需要用http协议,如果用https协议,有可能无法访问
http://ip:15672
参考文档:
【千锋Java】一文带你学会不同环境下RabbitMQ的安装 - 知乎 (zhihu.com)
https://blog.csdn.net/qq_44861126/article/details/130283245
https://blog.csdn.net/Milogenius/article/details/125224527