【RabbitMQ实战】06 RabbitMQ配置

一、概述

一般情况下,可以使用默认的内建配置来有效地运行RabbitMQ,并且大多数情况下也并不需要修改任何 RabbitMQ的配置。当然,为了更加有效地操控 RabbitMQ,也可以利用调节系统范围内的参数来达到定制化的需求。

RabbitMQ提供了三种方式来定制化服务:

  • (1)环境变量(Enviroment Variables )。RabbitMQ服务端参数可以通过环境变量进行配置,
    例如,节点名称、RabbitMQ配置文件的地址、节点内部通信端口等。

  • (2)配置文件(Configuration File )。可以定义 RabbitMQ服务和插件设置,例如,TCP监听端口,以及其他网络相关的设置、内存限制、磁盘限制等。

  • (3)运行时参数和策略(Runtime Parameters and Policies)。可以在运行时定义集群层面的服务设置。

对于不同的操作系统和不同的RabbitMQ安装包来说,相应的配置会有所变化,包括相应的配置文件的地址等,在使用时要尤为注意。

二、环境变量

2.1 手动设置的环境变量

还记得我们在第一节,【 3分钟在Linux上安装RabbitMQ】,其中就手动设置了两个环境变量RABBITMQ_USERNAMERABBITMQ_PASSWORD
定义了超级管理员的账号和密码,这样我们就能通过这个账号登录管理后台

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

进入docker容器看一下关于RABBITMQ现在有哪些环境变量

I have no name!@0d5cb60e3a06:/$ env | grep RABBIT
RABBITMQ_PASSWORD=admin
RABBITMQ_USERNAME=admin

三、变量配置文件rabbitmq-env.conf

rabbitmq-env.conf配置文件所在目录为:/etc/rabbitmq

I have no name!@0d5cb60e3a06:/etc/rabbitmq$ ls -al
total 12
drwxrwxr-x. 1 root root  75 Sep 25 05:23 .
drwxr-xr-x. 1 root root  22 Dec  2  2021 ..
-rw-r--r--. 1 1001 root  41 Sep 25 05:23 enabled_plugins
-rw-r--r--. 1 1001 root 560 Sep 20 12:33 rabbitmq.conf
-rw-r--r--. 1 1001 root  78 Sep 20 12:33 rabbitmq-env.conf

查看一下该配置文件配置了些啥

I have no name!@0d5cb60e3a06:/etc/rabbitmq$ cat rabbitmq-env.conf 
HOME=/opt/bitnami/rabbitmq/.rabbitmq
NODE_PORT=5672
NODENAME=rabbit@localhost

默认的配置,就上面的HOME目录,端口,以及节点名称
如何配置这个文件呢?如果想改端口为5673,改成NODE_PORT=5673即可。如果想添加一个变量值,同样的,在最下面就可以直接添加。可以看到,这里的变量名,是不带RABBITMQ前缀的。

四、默认的取值规则文件rabbitmq-defaults

该文件一般是在:$RABBITMQ_HOME/sbin下面
本示例所在的目录是:/opt/bitnami/rabbitmq/sbin下面

I have no name!@0d5cb60e3a06:/opt/bitnami/rabbitmq/sbin$ ls -al
total 48
drwxrwxr-x. 1 root root  238 Dec  2  2021 .
drwxr-xr-x. 1 root root   45 Jan  4  2022 ..
-rwxrwxr-x. 1 root root  855 Dec  2  2021 rabbitmqctl
-rwxrwxr-x. 1 root root  609 Dec  2  2021 rabbitmq-defaults
-rwxrwxr-x. 1 root root  864 Dec  2  2021 rabbitmq-diagnostics
-rwxrwxr-x. 1 root root 6887 Dec  2  2021 rabbitmq-env
-rwxrwxr-x. 1 root root  860 Dec  2  2021 rabbitmq-plugins
-rwxrwxr-x. 1 root root  859 Dec  2  2021 rabbitmq-queues
-rwxrwxr-x. 1 root root 6002 Dec  2  2021 rabbitmq-server
-rwxrwxr-x. 1 root root  857 Dec  2  2021 rabbitmq-streams
-rwxrwxr-x. 1 root root  858 Dec  2  2021 rabbitmq-tanzu
-rwxrwxr-x. 1 root root  860 Dec  2  2021 rabbitmq-upgrade

该文件内容如下:

I have no name!@0d5cb60e3a06:/opt/bitnami/rabbitmq/sbin$ cat rabbitmq-defaults 
#!/bin/sh -e
##  This Source Code Form is subject to the terms of the Mozilla Public
##  License, v. 2.0. If a copy of the MPL was not distributed with this
##  file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
##  Copyright (c) 2012-2020 VMware, Inc. or its affiliates.  All rights reserved.
##

### next line potentially updated in package install steps
SYS_PREFIX=${RABBITMQ_HOME}

CLEAN_BOOT_FILE=start_clean
SASL_BOOT_FILE=start_sasl
BOOT_MODULE="rabbit"

if test -z "$CONF_ENV_FILE" && test -z "$RABBITMQ_CONF_ENV_FILE"; then
    CONF_ENV_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq-env.conf
fi

可以看到这个文件有一些变量配置,如CONF_ENV_FILE,可以大概猜测到是指定rabbitmq的变量配置文件

五、配置文件rabbitmq.conf

配置文件在哪些目录,总是让人困扰的一件事,最简单的办法就是查看rabbitmq的服务启动日志
本示例通过docker logs 容器名称,查看日志

[root@localhost ~]# docker logs rabbitmq
......
2023-09-25 04:33:10.216618+00:00 [info] <0.222.0>  node           : rabbit@localhost
2023-09-25 04:33:10.216618+00:00 [info] <0.222.0>  home dir       : /opt/bitnami/rabbitmq/.rabbitmq
2023-09-25 04:33:10.216618+00:00 [info] <0.222.0>  config file(s) : /opt/bitnami/rabbitmq/etc/rabbitmq/rabbitmq.conf
2023-09-25 04:33:10.216618+00:00 [info] <0.222.0>  cookie hash    : XvNpT2Nlg8SZCslJcYXN8g==
2023-09-25 04:33:10.216618+00:00 [info] <0.222.0>  log(s)         : /opt/bitnami/rabbitmq/var/log/rabbitmq/rabbit@localhost_upgrade.log
2023-09-25 04:33:10.216618+00:00 [info] <0.222.0>                 : <stdout>
2023-09-25 04:33:10.216618+00:00 [info] <0.222.0>  database dir   : /bitnami/rabbitmq/mnesia/rabbit@localhost

该文件内容如下:
可以看到日志中配置文件在下面的目录:
config file(s) : /opt/bitnami/rabbitmq/etc/rabbitmq/rabbitmq.conf

I have no name!@0d5cb60e3a06:/opt/bitnami/rabbitmq/etc/rabbitmq$ cat rabbitmq.conf 
## Clustering
cluster_partition_handling = ignore

## Defaults
# During the first start, RabbitMQ will create a vhost and a user
# These config items control what gets created
default_permissions.configure = .*
default_permissions.read = .*
default_permissions.write = .*
default_vhost = /
default_user = admin
default_pass = admin

## Networking
listeners.tcp.default = 5672

## Management
management.tcp.ip = 0.0.0.0
management.tcp.port = 15672

## Resource limits
# Set a free disk space limit relative to total available RAM
disk_free_limit.relative = 1.0

可以看到rabbitmq监听的端口是5672。一般情况下,如果能在rabbitmq-env.conf里面改的数据,建议直接在rabbitmq-env.conf里面改。

六、变量生效顺序

环境变量>rabbitmq-env.conf>rabbitmq.conf
以RABBITMQ_NODENAME 这个变量为例,

  • RabbitMQ在启动服务的时候首先判断当前Shell环境中有无 RABBITMQ_NODENANE的定义,如果有则启用此值;
  • 如果没有,则查看rabbitmq-env.conf 中是否定义了 NODENAME 这个变量,如果有则启用此值,如果没有则采用默认的取值规则,即 rabbit@$HOSTNAME。

附、关于环境变量的说明

(来源于官网)

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实战,rabbitmq)