1. 程式人生 > 實用技巧 >RabbitMQ 安裝及服務化

RabbitMQ 安裝及服務化

一、RabbitMQ執行需要的基礎環境,Erlang安裝

1.從官網(http://erlang.org/download/)下載安裝包,示例版本:otp_src_21.3.tar.gz

2.解壓otp_src_21.3.tar.gz

tar -zvxf opt_src_21.3.tar.gz

3.配置

./configure
或者

./configure --prefix=/usr/local/erlang

4.編譯

make install
或
make && make install

5.環境變數配置

vi /etc/profile

在檔案末尾加一行

PATH=$PATH:/usr/local/erlang/bin

儲存檔案並退出:

Esc
:wq

執行指令,使檔案生效

source /etc/profile

6.驗證安裝是否成功

[root@localhost ~]# erl
Erlang/OTP 21 [erts-10.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Eshell V10.3  (abort with ^G)
1> exit
1> quit
1> halt()
1> halt().
* 2: syntax error before: quit
1> halt()。
* 1: illegal character
1> halt().
[root@localhost ~]# erl
Erlang/OTP 21 [erts-10.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Eshell V10.3  (abort with ^G)
1> ssl:versions().
[{ssl_app,"9.2"},
 {supported,['tlsv1.2','tlsv1.1',tlsv1]},
 {supported_dtls,['dtlsv1.2',dtlsv1]},
 {available,['tlsv1.2','tlsv1.1',tlsv1,sslv3]},
 {available_dtls,['dtlsv1.2',dtlsv1]}]
2> halt().
[root@localhost ~]#

至此,Erlang安裝成功。

注意:Erlang執行還需要以下基礎環境

yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel

二、RabbitMQ安裝

1.從官網(https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/)下載安裝包,示例版本:rabbitmq-server-generic-unix-3.7.10.tar.xz

2.解壓rabbitmq-server-generic-unix-3.7.10.tar.xz

xz -d rabbitmq-server-generic-unix-3.7.10.tar.xz
tar -xvf rabbitmq-server-generic-unix-3.7.10.tar
或者
tar xvjf rabbitmq-server-generic-unix-3.7.10..tar.xz

3.配置環境變數

vi /etc/profile
在檔案末尾新增一行:
#set rabbitmq enviroment
PATH=$PATH:/usr/local/rabbitmq_server-3.6.15/sbin

Esc
:wq

4.啟動RabbitMQ

[root@localhost rabbitmq_server-3.7.10]# ./sbin/rabbitmq-server

  ##  ##  
  ##  ##      RabbitMQ 3.7.10. Copyright (C) 2007-2018 Pivotal Software, Inc.
  ##########  Licensed under the MPL.  See http://www.rabbitmq.com/
  ######  ##
  ##########  Logs: /home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/[email protected]
                    /home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/rabbit@localhost_upgrade.log

              Starting broker...
 completed with 5 plugins.

5.啟動RabbitMQ管理後臺

[root@localhost rabbitmq_server-3.7.10]# ./sbin/rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_delayed_message_exchange
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_mqtt
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
Plugin configuration unchanged.
[root@localhost rabbitmq_server-3.7.10]#

6.後臺啟動rabbitmq

[root@localhost rabbitmq_server-3.7.10]# ./sbin/rabbitmq-server -detach

  ##  ##
  ##  ##      RabbitMQ 3.7.10. Copyright (C) 2007-2018 Pivotal Software, Inc.
  ##########  Licensed under the MPL.  See http://www.rabbitmq.com/
  ######  ##
  ##########  Logs: /home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/[email protected]
                    /home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/rabbit@localhost_upgrade.log

              Starting broker...
 completed with 5 plugins.

7.訪問管理後臺

http://127.0.0.1:15672/

至此,RabbitMQ安裝完成。但是需要注意的是RabbitMQ預設賬號密碼為guest/guest,只能本機訪問。所以需要建立一個遠端訪問的使用者,並設定管理員許可權。具體如下:

新增使用者:
rabbitmqctl add_user test test
新增許可權:
rabbitmqctl set_permissions -p "/" test ".*" ".*" ".*"
修改使用者角色:
rabbitmqctl set_user_tags test administrator

三、RabbitMQ常用命令列

1.啟動方式
rabbitmq-server -detached 後臺啟動
rabbitmq-server          直接啟動
2.服務管理
rabbitmq-service install 安裝服務
rabbitmq-service start  開始服務
rabbitmq-service stop  停止服務
rabbitmq-service enable 使服務有效
rabbitmq-service disable 使服務無效
rabbitmq-service help   幫助
3.關閉
rabbitmqctl stop
4.rabbitmq管理外掛啟用
rabbitmq-plugins enable rabbitmq_management 啟用後臺管理外掛視覺化介面
rabbitmq-plugins disable rabbitmq_management 關閉後臺管理外掛視覺化介面
5.檢視服務狀態
rabbitmqctl status
[root@localhost rabbitmq_server-3.7.10]# ./sbin/rabbitmqctl status
Status of node rabbit@localhost ...
[{pid,3800},
 {running_applications,
     [{rabbitmq_management,"RabbitMQ Management Console","3.7.10"},
      {rabbitmq_management_agent,"RabbitMQ Management Agent","3.7.10"},
      {rabbitmq_delayed_message_exchange,"RabbitMQ Delayed Message Exchange",
          "3.8.0"},
      {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.7.10"},
      {rabbitmq_mqtt,"RabbitMQ MQTT Adapter","3.7.10"},
      {rabbit,"RabbitMQ","3.7.10"},
      {os_mon,"CPO  CXC 138 46","2.4.7"},
      {amqp_client,"RabbitMQ AMQP Client","3.7.10"},
      {rabbit_common,
          "Modules shared by rabbitmq-server and rabbitmq-erlang-client",
          "3.7.10"},
      {xmerl,"XML parser","1.3.19"},
      {cowboy,"Small, fast, modern HTTP server.","2.6.1"},
      {ranch,"Socket acceptor pool for TCP protocols.","1.7.1"},
      {ssl,"Erlang/OTP SSL application","9.2"},
      {public_key,"Public key infrastructure","1.6.5"},
      {asn1,"The Erlang ASN1 compiler version 5.0.8","5.0.8"},
      {mnesia,"MNESIA  CXC 138 12","4.15.6"},
      {jsx,"a streaming, evented json parsing toolkit","2.9.0"},
      {cowlib,"Support library for manipulating Web protocols.","2.7.0"},
      {crypto,"CRYPTO","4.4.1"},
      {inets,"INETS  CXC 138 49","7.0.6"},
      {recon,"Diagnostic tools for production use","2.3.6"},
      {lager,"Erlang logging framework","3.6.5"},
      {goldrush,"Erlang event stream processor","0.1.9"},
      {compiler,"ERTS  CXC 138 10","7.3.2"},
      {syntax_tools,"Syntax tools","2.1.7"},
      {sasl,"SASL  CXC 138 11","3.3"},
      {stdlib,"ERTS  CXC 138 10","3.8"},
      {kernel,"ERTS  CXC 138 10","6.3"}]},
 {os,{unix,linux}},
 {erlang_version,
     "Erlang/OTP 21 [erts-10.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:64] [hipe]\n"},
 {memory,
     [{connection_readers,42644},
      {connection_writers,1452},
      {connection_channels,5076},
      {connection_other,74756},
      {queue_procs,881148},
      {queue_slave_procs,0},
      {plugins,1720784},
      {other_proc,22698808},
      {metrics,237748},
      {mgmt_db,259672},
      {mnesia,130264},
      {other_ets,2980584},
      {binary,153832},
      {msg_index,62944},
      {code,23731442},
      {atom,1172689},
      {other_system,11885549},
      {allocated_unused,12399008},
      {reserved_unallocated,446464},
      {strategy,rss},
      {total,[{erlang,66039392},{rss,78884864},{allocated,78438400}]}]},
 {alarms,[]},
 {listeners,
     [{clustering,25672,"::"},
      {amqp,5672,"::"},
      {mqtt,1883,"::"},
      {http,15672,"::"}]},
 {vm_memory_calculation_strategy,rss},
 {vm_memory_high_watermark,0.4},
 {vm_memory_limit,11712584089},
 {disk_free_limit,50000000},
 {disk_free,927859998720},
 {file_descriptors,
     [{total_limit,924},{total_used,12},{sockets_limit,829},{sockets_used,0}]},
 {processes,[{limit,1048576},{used,449}]},
 {run_queue,1},
 {uptime,1540},
 {kernel,{net_ticktime,60}}]
[root@localhost rabbitmq_server-3.7.10]#

四、常見問題總結

問題一:啟動rabbitMQ報錯,具體如下

./rabbitmq-server: line 80: erl: command not found

原因:ErlangRabbitMQ環境變數沒有配置

問題二:啟動rabbitMQ報錯

Error description : noproc

原因:rabbitmqerlang版本不一致,前往rabbitMQ官網查詢應下載版本

問題三:本地訪問成功,其他主機訪問失敗

原因:rabbitmq的管理埠15672,防火牆埠未開放

解決過程:

1.檢查防火牆是否啟用

systemctl status firewalld

2.檢查埠15672是否開放

firewall-cmd --query-port=15672/tcp

3.開放15672

firewall-cmd --permanent --add-port=80/tcp

4. 重啟防火牆

firewall-cmd --reload

5.檢視埠15672是否開放

firewall-cmd --query-port=8080/tcp

問題四:

原因:系統預設的HOME/root,可能造成erlang語言環境獲取不到HOME引數。

檢視一個erl程序:ps aux | grep beam

可以看出-home引數實在啟動的時候載入進去的,啟動一個erl例項。

解決:

問題五:

原因:erlang環境載入不到

解決:在rabbitmq-server檔案中加入

ERLANG_HOME=/usr/local/lib/erlang/erts-10.3/bin

PATH=$PATH:$ERLANG_HOME

問題六:

原因:erlang環境載入不到

解決:在rabbitmqctl檔案中加入

ERLANG_HOME=/usr/local/lib/erlang/erts-10.3/bin

PATH=$PATH:$ERLANG_HOME

五、系統服務

方式一、service服務

1.建立自動啟動指令碼

vi /etc/init.d/rabbitmq
#
# chkconfig: 2345 90 10
# description: start the RabbitMQ
#
 
#指令碼名稱
prog=rabbitmq
export HOME=/home/h4kit/tools/rabbitmq_server-3.7.10
PATH=$PATH:$HOME/sbin
#RabbitMQ安裝目錄
RABBITMQ_HOME=/home/h4kit/tools/rabbitmq_server-3.7.10/sbin
export RABBITMQ_HOME
case "$1" in
    start)
    echo "Starting RabbitMQ ..."
    $RABBITMQ_HOME/rabbitmq-server start
    ;;
 
 
stop)
    echo "Stopping RabbitMQ ..."
    $RABBITMQ_HOME/rabbitmqctl stop
    ;;
 
 
restart)
    echo "no restart command"
    ;;
 
 
*)
    echo "Usage: $prog {start|stop|restart}"
    ;;
esac
exit 0

2.修改指令碼為可執行檔案

chmod a+x rabbitmq

3.檢視rabbitmq開機啟動情況

chkconfig --list

4.沒有rabbitmq,則新增到系統啟動佇列中

chkconfig --add rabbitmq

5.檢視rabbitmq開機啟動情況

chkconfig --list

方式二、systemctl服務

1.進入系統服務目錄

cd /usr/lib/systemd/system

2.建立檔案rabbitmq.service

3.編輯rabbitmq.service

vi rabbitmq.service

[Unit]

Description=rabbitmq

 

[Service]

#Type=forking

User=root

Group=root

KillMode=/home/h4kit/tools/rabbitmq_server-3.7.10/sbin/rabbitmqctl stop

PIDFile=/home/h4kit/tools/rabbitmq_server-3.7.10/rabbitmq.pid

Restart=no

ExecStart=/home/h4kit/tools/rabbitmq_server-3.7.10/sbin/rabbitmq-server start

PrivateTmp=true

 

[Install]   

WantedBy=multi-user.target

4.重新載入systemd程式的配置檔案儲存檔案Esc :wq

systemctl start rabbitmq.service

5.檢視rabbitmq是否已啟動

方式一:

ps -ef|grep rabbitmq

[root@localhost ~]# ps -ef|grep rabbitmq

root       724     1  0 10:07 ?        00:00:00 /bin/sh /home/h4kit/tools/rabbitmq_server-3.7.10/sbin/rabbitmq-server start

root      1423   724  0 10:08 ?        00:00:05 /usr/local/lib/erlang/erts-10.3/bin/beam.smp -W w -A 64 -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -K true -B i -- -root /usr/local/lib/erlang -progname erl -- -home /root -- -pa /home/h4kit/tools/rabbitmq_server-3.7.10/ebin  -noshell -noinput -s rabbit boot -sname rabbit@localhost -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit lager_log_root "/home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq" -rabbit lager_default_file "/home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/[email protected]" -rabbit lager_upgrade_file "/home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/rabbit@localhost_upgrade.log" -rabbit enabled_plugins_file "/home/h4kit/tools/rabbitmq_server-3.7.10/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/home/h4kit/tools/rabbitmq_server-3.7.10/plugins" -rabbit plugins_expand_dir "/home/h4kit/tools/rabbitmq_server-3.7.10/var/lib/rabbitmq/mnesia/rabbit@localhost-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/home/h4kit/tools/rabbitmq_server-3.7.10/var/lib/rabbitmq/mnesia/rabbit@localhost" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672 start

root      4171  3929  0 10:19 pts/0    00:00:00 grep --color=auto rabbitmq

[root@localhost ~]#

方式二:

systemctl status rabbitmq.service

[root@localhost ~]# systemctl status rabbitmq.service

● rabbitmq.service - rabbitmq

   Loaded: loaded (/usr/lib/systemd/system/rabbitmq.service; enabled; vendor preset: disabled)

   Active: active (running) since 一 2020-12-21 10:07:52 CST; 13min ago

 Main PID: 724 (rabbitmq-server)

   CGroup: /system.slice/rabbitmq.service

           ├─ 724 /bin/sh /home/h4kit/tools/rabbitmq_server-3.7.10/sbin/rabbitmq-server start

           ├─1288 /usr/local/lib/erlang/erts-10.3/bin/epmd -daemon

           ├─1423 /usr/local/lib/erlang/erts-10.3/bin/beam.smp -W w -A 64 -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -K true -B i -- -r...

           ├─1658 erl_child_setup 1024

           ├─3216 inet_gethost 4

           └─3217 inet_gethost 4

 

12月 21 10:07:52 localhost.localdomain systemd[1]: Started rabbitmq.

12月 21 10:08:07 localhost.localdomain rabbitmq-server[724]: ##  ##

12月 21 10:08:07 localhost.localdomain rabbitmq-server[724]: ##  ##      RabbitMQ 3.7.10. Copyright (C) 2007-2018 Pivotal Software, Inc.

12月 21 10:08:07 localhost.localdomain rabbitmq-server[724]: ##########  Licensed under the MPL.  See http://www.rabbitmq.com/

12月 21 10:08:07 localhost.localdomain rabbitmq-server[724]: ######  ##

12月 21 10:08:07 localhost.localdomain rabbitmq-server[724]: ##########  Logs: /home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/[email protected]

12月 21 10:08:07 localhost.localdomain rabbitmq-server[724]: /home/h4kit/tools/rabbitmq_server-3.7.10/var/log/rabbitmq/rabbit@localhost_upgrade.log

12月 21 10:08:07 localhost.localdomain rabbitmq-server[724]: Starting broker...

12月 21 10:08:09 localhost.localdomain rabbitmq-server[724]: completed with 5 plugins.

[root@localhost ~]#

6.設定開機自啟

systemctl enable rabbitmq

7.伺服器重啟

systemctl reboot

8.檢視rabbitmq是否已啟動,同5

1.建立自動啟動指令碼

vi /etc/init.d/rabbitmq

2.跳轉目錄

cd /etc/init.d

3.修改指令碼為可執行檔案

chmod a+x rabbitmq

4.檢視rabbitmq開機啟動情況

chkconfig --list

5.沒有rabbitmq,則新增到系統啟動佇列中

chkconfig --add rabbitmq

6.檢視rabbitmq開機啟動情況

chkconfig --list