【RabbitMQ实战】01 3分钟在Linux上安装RabbitMQ

本节采用docker安装RabbitMQ。采用的是bitnami的镜像。Bitnami是一个提供各种流行应用的Docker镜像和软件包的公司。采用docker的方式3分钟就可以把我们想安装的程序运行起来,不得不说真的很方便啊,好了,开搞。使用前提:Linux虚拟机,安装好了docker环境。如果没有安装docker的话,请参考这里,先把docker先安装上。

一、搜索镜像

[root@localhost ~]# docker search rabbitmq
NAME                                      DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
rabbitmq                                  RabbitMQ is an open source multi-protocol me…   4887      [OK]       
bitnami/rabbitmq                          Bitnami Docker Image for RabbitMQ                103                  [OK]

这里我们选择bitname/rabbitmq

二、下载镜像

[root@localhost ~]# docker pull bitnami/rabbitmq
Using default tag: latest
latest: Pulling from bitnami/rabbitmq

三、把RabbitMQ镜像跑起来

docker run -d --name rabbitmq 
-e RABBITMQ_USERNAME=admin 
-e RABBITMQ_PASSWORD=admin 
-p 15672:15672 -p 5672:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 bitnami/rabbitmq

其中RABBITMQ_USERNAME是用户名
RABBITMQ_PASSWORD是密码
关于更多环境变量的说明,见文末

四、查看运行效果

看一下docker容器是否成功启动了,下面可以看到正在运行

[root@localhost ~]# docker ps | grep rabbit
0d5cb60e3a06   bitnami/rabbitmq       "/opt/bitnami/script…"   22 seconds ago   Up 20 seconds   4369/tcp, 5551-5552/tcp, 0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp, 0.0.0.0:25672->25672/tcp, :::25672->25672/tcp, 0.0.0.0:61613->61613/tcp, :::61613->61613/tcp, 15671/tcp   rabbitmq

输入地址访问一下控制台
http://192.168.56.201:15672/#/
用户名:admin,密码admin
登录成功
在这里插入图片描述
到此为止,我们就可以愉快地和RabbitMQ玩耍了

五、关于环境变量的说明

来源于官网

RABBITMQ_VHOST: RabbitMQ application vhost. Default: /
RABBITMQ_VHOSTS: List of additional virtual host (vhost), separated by space. E.g.: /shared /prioritized /tasks
RABBITMQ_USERNAME: RabbitMQ application username. Default: user
RABBITMQ_PASSWORD: RabbitMQ application password. Default: bitnami
RABBITMQ_SECURE_PASSWORD: Whether to set the RabbitMQ password securely. This is incompatible with loading external RabbitMQ definitions. Default: no
RABBITMQ_LOAD_DEFINITIONS: Whether to load external RabbitMQ definitions. This is incompatible with setting the RabbitMQ password securely. Default: no.
RABBITMQ_ERL_COOKIE: Erlang cookie to determine whether different nodes are allowed to communicate with each other.
RABBITMQ_NODE_TYPE: Node Type. Valid values: stats, queue-ram or queue-disc. Default: stats
RABBITMQ_NODE_NAME: Node name and host. E.g.: node@hostname or node (localhost won't work in cluster topology). Default rabbit@localhost. If using this variable, ensure that you specify a valid host name as the container wil fail to start otherwise. If using a fully qualified domain name, RABBITMQ_USE_LONGNAME needs to be set to true as well.
RABBITMQ_USE_LONGNAME: When set to true this will cause RabbitMQ to use fully qualified names to identify nodes. Default: false
RABBITMQ_FORCE_BOOT: Force a node to start even if it was not the last to shut down. Default: no
RABBITMQ_CLUSTER_NODE_NAME: Node name to cluster with. E.g.: clusternode@hostname
RABBITMQ_CLUSTER_PARTITION_HANDLING: Cluster partition recovery mechanism. Default: ignore
RABBITMQ_NODE_PORT_NUMBER: Node port. Default: 5672
RABBITMQ_NODE_SSL_PORT_NUMBER: RabbitMQ node port number for SSL connections. Default: 5671
RABBITMQ_SSL_CACERTFILE: Path to the RabbitMQ server SSL CA certificate file. No defaults.
RABBITMQ_SSL_CERTFILE: Path to the RabbitMQ server SSL certificate file. No defaults.
RABBITMQ_SSL_KEYFILE: Path to the RabbitMQ server SSL certificate key file. No defaults.
RABBITMQ_COMBINED_CERT_PATH: Cert- and keyfile are combined automatically into one combined file at this file path. If you are using a combined certificate anyways, mount it to the container and set this path to the mounted file. Default: /tmp/rabbitmq_combined_keys.pem
RABBITMQ_SSL_DEPTH: Maximum number of non-self-issued intermediate certificates that may follow the peer certificate in a valid certification path. No defaults.
RABBITMQ_SSL_FAIL_IF_NO_PEER_CERT: Whether to reject TLS connections if client fails to provide a certificate. Default: verify_none
RABBITMQ_SSL_VERIFY: Whether to enable peer SSL certificate verification. Default: no
RABBITMQ_PLUGINS: Comma, semi-colon or space separated list of plugins to enable during the initialization. No defaults.
RABBITMQ_COMMUNITY_PLUGINS: Comma, semi-colon or space separated list of URLs where to download custom plugins during the initialization. No defaults.
Management server configuration
RABBITMQ_MANAGEMENT_BIND_IP: RabbitMQ management server bind IP address. Default: 0.0.0.0
RABBITMQ_MANAGEMENT_PORT_NUMBER: RabbitMQ management server port number. Default: 15672
RABBITMQ_MANAGEMENT_SSL_PORT_NUMBER: RabbitMQ management server port number for SSL/TLS connections. No defaults.
RABBITMQ_MANAGEMENT_SSL_CACERTFILE: Path to the RabbitMQ management server SSL CA certificate file. No defaults.
RABBITMQ_MANAGEMENT_SSL_CERTFILE: Path to the RabbitMQ management server SSL certificate file. No defaults.
RABBITMQ_MANAGEMENT_SSL_KEYFILE: Path to the RabbitMQ management server SSL certificate key file. No defaults.
RABBITMQ_MANAGEMENT_SSL_DEPTH: Maximum number of non-self-issued intermediate certificates that may follow the peer certificate in a valid certification path, for the RabbitMQ management server. No defaults.
RABBITMQ_MANAGEMENT_SSL_FAIL_IF_NO_PEER_CERT: Whether to reject TLS connections if client fails to provide a certificate for the RabbitMQ management server. Default: yes
RABBITMQ_MANAGEMENT_SSL_VERIFY: Whether to enable peer SSL certificate verification for the RabbitMQ management server. Default: verify_peer
LDAP configuration
RABBITMQ_ENABLE_LDAP: Enable the LDAP configuration. Defaults: no
RABBITMQ_LDAP_TLS: Enable secure LDAP configuration. Defaults: no
RABBITMQ_LDAP_SERVERS: Comma, semi-colon or space separated list of LDAP server hostnames. No defaults.
RABBITMQ_LDAP_SERVERS_PORT: LDAP servers port. Defaults: 389
RABBITMQ_LDAP_USER_DN_PATTERN: DN used to bind to LDAP in the form cn=$${username},dc=example,dc=org. No defaults.
Memory and disk configuration
RABBITMQ_VM_MEMORY_HIGH_WATERMARK: High memory watermark for RabbitMQ to block publishers and prevent new messages from being enqueued. Can be specified as an absolute or relative value (as percentage or value between 0 and 1). No defaults.
RABBITMQ_DISK_FREE_RELATIVE_LIMIT: Disk relative free space limit of the partition on which RabbitMQ is storing data. Default: 1.0
RABBITMQ_DISK_FREE_ABSOLUTE_LIMIT: Disk absolute free space limit of the partition on which RabbitMQ is storing data (takes precedence over the relative limit). No defaults.
RABBITMQ_ULIMIT_NOFILES: Resources limits: maximum number of open file descriptors. Default: 65536

六、进入RabbitMQ容器一探究竟

首先进入容器

[root@localhost ~]# docker exec -it rabbitmq bin/bash

rabbitmq是依赖erlang环境的,所以查看一下erlang是否安装成功
输入 erl,可以看到如下输出


```bash
I have no name!@0d5cb60e3a06:/$ erl
Erlang/OTP 24 [erts-12.2] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit]

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

bitnami的镜像,软件一般安装在opt/bitnami目录下

I have no name!@0d5cb60e3a06:/$ cd opt/bitnami/
I have no name!@0d5cb60e3a06:/opt/bitnami$ ls -al
total 4
drwxrwxr-x. 1 root root  22 Jan  4  2022 .
drwxrwxr-x. 1 root root  21 Jan  4  2022 ..
-rw-r--r--. 1 root root 659 Jan  4  2022 .bitnami_components.json
drwxr-xr-x. 4 root root  33 Aug 17  2021 common
drwxr-xr-x. 5 root root  44 Dec 15  2021 erlang
drwxrwxr-x. 2 root root  26 Jan  4  2022 licenses
drwxr-xr-x. 1 root root  45 Jan  4  2022 rabbitmq
drwxrwxr-x. 1 root root  82 Jan  4  2022 scripts

查看rabbitmq是否正常启动
rabbitmqctl status可以输出很多rabbitmq的信息
包括版本信息,安装的插件,数据安装目录,还有一些指标信息。

I have no name!@0d5cb60e3a06:/opt/bitnami$ rabbitmqctl status
Status of node rabbit@localhost ...
Runtime

OS PID: 213
OS: Linux
Uptime (seconds): 816
Is under maintenance?: false
RabbitMQ version: 3.9.11
Node name: rabbit@localhost
Erlang configuration: Erlang/OTP 24 [erts-12.2] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit]
Erlang processes: 378 used, 1048576 limit
Scheduler run queue: 1
Cluster heartbeat timeout (net_ticktime): 60

Plugins

Enabled plugin file: /opt/bitnami/rabbitmq/etc/rabbitmq/enabled_plugins
Enabled plugins:

 * rabbitmq_management
 * amqp_client
 * rabbitmq_web_dispatch
 * cowboy
 * cowlib
 * rabbitmq_management_agent

Data directory

Node data directory: /bitnami/rabbitmq/mnesia/rabbit@localhost
Raft data directory: /bitnami/rabbitmq/mnesia/rabbit@localhost/quorum/rabbit@localhost

Config files

 * /opt/bitnami/rabbitmq/etc/rabbitmq/rabbitmq.conf

Log file(s)

 * /opt/bitnami/rabbitmq/var/log/rabbitmq/rabbit@localhost_upgrade.log
 * <stdout>

Alarms

(none)

Memory

Total memory used: 0.1509 gb
Calculation strategy: rss
Memory high watermark setting: 0.4 of available memory, computed to: 1.1548 gb

reserved_unallocated: 0.0833 gb (55.2 %)
code: 0.0347 gb (22.97 %)
other_system: 0.0238 gb (15.78 %)
other_proc: 0.0192 gb (12.7 %)
other_ets: 0.0032 gb (2.09 %)
atom: 0.0014 gb (0.94 %)
plugins: 0.0014 gb (0.9 %)
binary: 5.0e-4 gb (0.31 %)
mgmt_db: 2.0e-4 gb (0.15 %)
connection_other: 1.0e-4 gb (0.09 %)
mnesia: 1.0e-4 gb (0.07 %)
metrics: 1.0e-4 gb (0.05 %)
msg_index: 1.0e-4 gb (0.04 %)
connection_readers: 0.0 gb (0.03 %)
queue_procs: 0.0 gb (0.02 %)
quorum_ets: 0.0 gb (0.0 %)
connection_channels: 0.0 gb (0.0 %)
stream_queue_procs: 0.0 gb (0.0 %)
stream_queue_replica_reader_procs: 0.0 gb (0.0 %)
connection_writers: 0.0 gb (0.0 %)
allocated_unused: 0.0 gb (0.0 %)
queue_slave_procs: 0.0 gb (0.0 %)
quorum_queue_procs: 0.0 gb (0.0 %)
stream_queue_coordinator_procs: 0.0 gb (0.0 %)

File Descriptors

Total: 6, limit: 1048479
Sockets: 1, limit: 943629

Free Disk Space

Low free disk space watermark: 2.8869 gb
Free disk space: 10.2966 gb

Totals

Connection count: 1
Queue count: 1
Virtual host count: 2

Listeners

Interface: [::], port: 15672, protocol: http, purpose: HTTP API
Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0

更多rabbitmq的操作,可以通过rabbitmqctl --help来查看

I have no name!@0d5cb60e3a06:/opt/bitnami$ rabbitmqctl --help

七、扩展阅读

在玩容器的时候,一般搜索镜像的时候,会发现bitnami仓库的镜像,star都比较高。发现确实好用!
这里附上
官网链接:https://bitnami.com/stacks
在bitnami的官网,可以看到很多打包好的应用,链接进去是github的链接,是相关的镜像的说明
在这里插入图片描述

其中RabbitMQ镜像的git链接:https://github.com/bitnami/containers/tree/main/bitnami/rabbitmq