實踐環境
CentOS-7-x86_64-DVD-2009.iso
rabbitmq-server-3.9.16-1.el7.noarch.rpm
https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.9.16
https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.9.16/rabbitmq-server-3.9.16-1.el7.noarch.rpm
備註:rabbitmq-server最後一個支援CentOS7的版本
erlang-23.3.4.11-1.el7.x86_64.rpm
https://github.com/rabbitmq/erlang-rpm/releases
https://github.com/rabbitmq/erlang-rpm/releases/download/v23.3.4.11/erlang-23.3.4.11-1.el7.x86_64.rpm
備註:Erlang最後一個支援Centos7的版本
實踐操作
下載RabbitMQ及Erlang軟體包
RabbitMQ是採用 Erlang語言開發的,所以系統環境必須提供 Erlang環境,需要是安裝 Erlang。Erlang版本要和需要安裝的RabbitMQ版本匹配,可透過網址https://www.rabbitmq.com/docs/which-erlang 不同Erlang支援的RabbitMQ版本。
安裝軟體包
# rpm -ivh erlang-23.3.4.11-1.el7.x86_64.rpm
# rpm -ivh rabbitmq-server-3.9.16-1.el7.noarch.rpm
說明:參照官方文件,https://www.rabbitmq.com/docs/install-rpm#package-dependencies 還需要安裝依賴包:socat
,logrotate
,但是筆者實踐時未安裝這兩個依賴包。
執行RabbitMQ
RabbitMQ伺服器包安裝完成後,預設情況下伺服器不會作為守護程序啟動。執行以下命令,設定當系統啟動時,預設以管理員身份,以守護程序的方式啟動RabbitMQ
# systemctl enable rabbitmq-server
Created symlink from /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service to /usr/lib/systemd/system/rabbitmq-server.service.
停、啟伺服器
# 啟動服務
# systemctl start rabbitmq-server
# 檢視服務狀態
# 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 Sat 2024-07-13 04:02:54 EDT; 12s ago
Main PID: 1237 (beam.smp)
CGroup: /system.slice/rabbitmq-server.service
├─1237 /usr/lib64/erlang/erts-11.2.2.10/bin/beam.smp -W w -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -sbwt none -sbwtdcpu none -sbwtdio none -...
├─1252 erl_child_setup 32768
├─1275 /usr/lib64/erlang/erts-11.2.2.10/bin/epmd -daemon
├─1296 inet_gethost 4
└─1297 inet_gethost 4
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: Doc guides: https://rabbitmq.com/documentation.html
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: Support: https://rabbitmq.com/contact.html
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: Tutorials: https://rabbitmq.com/getstarted.html
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: Monitoring: https://rabbitmq.com/monitoring.html
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: Logs: /var/log/rabbitmq/rabbit@localhost.log
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: /var/log/rabbitmq/rabbit@localhost_upgrade.log
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: <stdout>
Jul 13 04:02:49 localhost.localdomain rabbitmq-server[1237]: Config file(s): (none)
Jul 13 04:02:54 localhost.localdomain rabbitmq-server[1237]: Starting broker... completed with 0 plugins.
Jul 13 04:02:54 localhost.localdomain systemd[1]: Started RabbitMQ broker.
# 停止服務
# systemctl stop rabbitmq-server
參考連結:https://www.rabbitmq.com/docs/install-rpm#running-rpm
啟用管理外掛
# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
Plugin configuration unchanged.
備註:開啟外掛後不需要重啟伺服器
開啟後,瀏覽器訪問管理介面(預設地址:http://node-hostname:15672/
.),如下,其中 192.167.206.101為RabbitMQ伺服器地址
參考連線:https://www.rabbitmq.com/docs/management#getting-started
配置檔案管理
查詢配置檔案
# rabbitmq-diagnostics status
......
Config files
Log file(s)
* /var/log/rabbitmq/rabbit@localhost.log
* /var/log/rabbitmq/rabbit@localhost_upgrade.log
* <stdout>
......
如上,配置檔案位於 Log file(s)
上方,這裡筆者實踐時命令輸出中沒看到配置檔案路徑資訊,按官方文件描述,應該Config files
下應該會包含配置檔案路徑資訊,類似如下
Config files
* /etc/rabbitmq/advanced.config
* /etc/rabbitmq/rabbitmq.conf
參考連線:
https://www.rabbitmq.com/docs/configure#configuration-files
https://www.rabbitmq.com/docs/configure#verify-configuration-config-file-location
使用者管理
預設使用者訪問
broker預設建立了一個使用者名稱和密碼都為guest
的使用者。預設情況下,該使用者只能在以訪問localhost
的方式連線到broker,即不能遠端訪問。嘗試遠端訪問伺服器時會提示User can only log in via localhost
。如果希望guest
也可以遠端登入,需要修改rabbitmq.conf
配置檔案,檔案末尾新增loopback_users = none
# vi /etc/rabbitmq/rabbitmq.conf
loopback_users = none
# systemctl restart rabbitmq-server
參考連線:
https://www.rabbitmq.com/docs/install-rpm#default-user-access
https://www.rabbitmq.com/docs/access-control#user-management
新增使用者
# rabbitmqctl add_user "testacc"
Adding user "testacc" ...
Password:
test1234
Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.
獲取當前使用者列表
# rabbitmqctl list_users
Listing users ...
user tags
testacc []
guest [administrator]
# rabbitmqctl list_users --formatter=json
[
{"user":"testacc","tags":[]}
,{"user":"guest","tags":["administrator"]}
]
刪除使用者
# rabbitmqctl delete_user 'username'
使用者授權
使用rabbitmqctl set_permissions
要向虛擬主機中的使用者授予許可權:
# rabbitmqctl set_permissions -p "custom-vhost" "username" ".*" ".*" ".*"
說明:
第一個 ".*" 表示配置每個實體上的許可權(First ".*" for configure permission on every entity)
第二個 ".*" 表示每個實體的寫許可權(Second ".*" for write permission on every entity)
第二個 ".*" 表示每個實體的讀許可權(Third ".*" for read permission on every entity)
# rabbitmqctl set_permissions -p "/" "testacc" ".*" ".*" ".*"
Setting permissions for user "testacc" in vhost "/" ...
移除授權
使用rabbitmqctl clear_permissions
移除授予給虛擬主機中的使用者許可權:
# Revokes permissions in a virtual host
rabbitmqctl.bat clear_permissions -p 'custom-vhost' 'username'
參考連線:https://www.rabbitmq.com/docs/access-control#user-management
https://www.rabbitmq.com/docs/access-control#override-default-user-credentials
設定使用者標籤
# rabbitmqctl set_user_tags testacc administrator
Setting tags for user "testacc" to [administrator] ...
說明:如果不設定標籤,以 testacc 使用者登入管理介面時,會提示Not management user
參考連線:
https://www.rabbitmq.com/docs/man/rabbitmqctl.8#set_user_tags
https://www.rabbitmq.com/docs/management#permissions
登入管理介面
開啟mqtt相關外掛
# rabbitmq-plugins enable rabbitmq_mqtt
Enabling plugins on node rabbit@localhost:
rabbitmq_mqtt
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_mqtt
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
rabbitmq_mqtt
參考連結:
https://www.rabbitmq.com/docs/mqtt#enabling-plugin
# rabbitmq-plugins enable rabbitmq_web_mqtt
Enabling plugins on node rabbit@localhost:
rabbitmq_web_mqtt
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_mqtt
rabbitmq_web_dispatch
rabbitmq_web_mqtt
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
rabbitmq_web_mqtt
started 1 plugins.
參考連結:https://www.rabbitmq.com/docs/web-mqtt#enabling
# rabbitmq-plugins enable rabbitmq_web_mqtt_examples
Enabling plugins on node rabbit@localhost:
rabbitmq_web_mqtt_examples
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_mqtt
rabbitmq_web_dispatch
rabbitmq_web_mqtt
rabbitmq_web_mqtt_examples
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
rabbitmq_web_mqtt_examples
參考連結:https://www.rabbitmq.com/docs/web-mqtt#examples
安裝後示例外掛後,訪問 http://主機IP地址:15670
,出現類似如下頁面