1. 程式人生 > >訊息佇列:Ubuntu16.04安裝和Web頁面管理RabbitMQ(樓主親測、真實有效)

訊息佇列:Ubuntu16.04安裝和Web頁面管理RabbitMQ(樓主親測、真實有效)

RabbitMQ

總來來說,RabbitMQ的安裝還是有一些難度的。不同的方式,安裝的方法也是完全不一樣,還要解決蠻多依賴。加上現在有些網站,極其不負責,很多博文都沒有經過測試檢驗就直接發出來的。樓主來親測一下,希望能對大家有好的幫助。

一、安裝前的準備

要確保,你有Erlang的環境。
樓主親測安裝Erlang

這裡寫圖片描述

二、安裝RabbitMQ

1、新增 apt repository倉庫

推薦通過apt repository的方式安裝。不然你需要自己去解決蠻多依賴的問題

To add the apt repository to the source list directory (/etc/apt/sources.list.d), use:

So, on Ubuntu 18.04 the above command becomes:

echo "deb https://dl.bintray.com/rabbitmq/debian bionic main" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list

and on Ubuntu 16.04 it would be:

echo "deb https://dl.bintray.com/rabbitmq/debian xenial main" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq
.list

2、新增Signing Key

In order to use the repository, add a key used to sign RabbitMQ releases to apt-key:

wget -O - 'https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc' | sudo apt-key add -

3、更新下系統庫

Run the following command to update the package list:

sudo apt-get update

4、安裝rabbitmq-server

sudo apt-get install rabbitmq-server

過程會讓你輸入y同意就好。

不報錯就OK了

三、啟動RabbitMQ

1、啟動命令:

service rabbitmq-server start

2、檢視RabbitMQ的狀態

service rabbitmq-server status

這裡寫圖片描述

四、Web頁面管理RabbitMQ

1、開啟頁面管理

rabbitmq既可以命令列操作,也可以用rabbitmq自帶的web管理介面,只需要啟動外掛便可以使用。

rabbitmq-plugins enable rabbitmq_management

結果如下

[email protected]:/home/rabbitmq# rabbitmq-plugins enable rabbitmq_management
warning: the VM is running with native name encoding of latin1 which may cause Elixir to malfunction as it expects utf8. Please ensure your locale is set to UTF-8 (which can be verified by running "locale" in your shell)
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to [email protected]
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch

started 3 plugins.
[email protected]:/home/rabbitmq# 

2、登陸頁面

然後通過瀏覽器訪問,如果是本機則可以輸入http://127.0.0.1:15672開啟登入介面,輸入使用者名稱和密碼訪問web管理介面了。預設使用者名稱guest密碼guest。
這裡寫圖片描述

我這是遠端的,就登陸不了。會報:User can only log in via localhost

3、解決User can only log in via localhost

  • 你需要先停止rabbitmq
service rabbitmq-server stop
  • 這個時候,需要編輯/etc/rabbitmq/rabbitmq.config檔案

本來是沒有這個檔案的。
你在執行下面的vi編輯命令的時候

vi /etc/rabbitmq/rabbitmq.config

會自動建立,你將下面的這個配置複製進去。

[
{rabbit, [{tcp_listeners, [5672]}, {loopback_users, ["admin"]}]}
].
  • 啟動RabbitMQ,開啟web訪問
service rabbitmq-server start
rabbitmq-plugins enable rabbitmq_management
  • 你再登陸,就可以遠端登陸啦。

這裡寫圖片描述

五、關於RabbitMQ的配置和埠問題

1、配置

一般使用預設配置就好了,具體配置可參考官方配置說明。
Rabbit官方配置說明

2、埠

預設的一些重要埠:

4369 – erlang發現口
5672 –client端通訊口
15672 – 管理介面ui埠
25672 – server間內部通訊口

其他埠請參考官方文件:埠說明

以下為rabbitmq的預設埠,如果這些埠被其他程式佔用,rabbitmq就會出錯。

4369: epmd, a peer discovery service used by RabbitMQ nodes and CLI tools
5672, 5671: used by AMQP 0-9-1 and 1.0 clients without and with TLS
25672: used for inter-node and CLI tools communication (Erlang distribution server port) and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). See networking guide for details.
35672-35682: used by CLI tools (Erlang distribution client ports) for communication with nodes and is allocated from a dynamic range (computed as Erlang dist port + 10000 through dist port + 10010). See networking guide for details.
15672: HTTP API clients and rabbitmqadmin (only if the management plugin is enabled)
61613, 61614: STOMP clients without and with TLS (only if the STOMP plugin is enabled)
1883, 8883: (MQTT clients without and with TLS, if the MQTT plugin is enabled
15674: STOMP-over-WebSockets clients (only if the Web STOMP plugin is enabled)
15675: MQTT-over-WebSockets clients (only if the Web MQTT plugin is enabled)

執行sudo netstat -tlnpe命令可以看到rabbitmq預設的監聽埠。

root@ubuntu:~# netstat -tlnpe
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          16278       1007/sshd       
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      112        184136      27132/beam.smp  
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      112        184091      27132/beam.smp  
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      112        184041      27223/epmd      
tcp6       0      0 :::22                   :::*                    LISTEN      0          16280       1007/sshd       
tcp6       0      0 :::5672                 :::*                    LISTEN      112        184123      27132/beam.smp  
tcp6       0      0 :::4369                 :::*                    LISTEN      112        184042      27223/epmd      
root@ubuntu:~# 

因為上面運行了sudo rabbitmq-plugins enable rabbitmq_management,所以這裡可以看到15672這個埠。

如果相沖突的話,可以參考官網文件埠修改文件修改或者殺掉佔用埠配置的檔案。

六、RabbitMQ常用的一些命令

sudo chkconfig rabbitmq-server on #新增開機啟動(chkconfig一般只有redhat系統有)RabbitMQ服務
sudo service rabbitmq-server start # 啟動服務
sudo service rabbitmq-server status # 檢視服務狀態
sudo service rabbitmq-server stop # 停止服務
sudo rabbitmqctl stop # 停止服務
sudo rabbitmqctl status # 檢視服務狀態
sudo rabbitmqctl list_users # 檢視當前所有使用者
sudo rabbitmqctl list_user_permissions guest # 檢視預設guest使用者的許可權
sudo rabbitmqctl delete_user guest# 刪掉預設使用者(由於RabbitMQ預設的賬號使用者名稱和密碼都是guest。為了安全起見, 可以刪掉預設使用者)
sudo rabbitmqctl add_user username password # 新增新使用者
sudo rabbitmqctl set_user_tags username administrator# 設定使用者tag
sudo rabbitmqctl set_permissions -p / username “.” “.” “.*” # 賦予使用者預設vhost的全部操作許可權
sudo rabbitmqctl list_user_permissions username # 檢視使用者的許可權

好啦,接下來就祝大家都能夠工作和學習順利啦。