MQ全稱爲Message Queue, 消息隊列(MQ)是一種應用程序對應用程序的通訊方法。應用程序經過讀寫出入隊列的消息(針對應用程序的數據)來通訊,而無需專用鏈接來連接它們。消息傳遞指的是程序之間經過在消息中發送數據進行通訊,而不是經過直接調用彼此來通訊,直接調用一般是用於諸如遠程過程調用的技術。排隊指的是應用程序經過 隊列來通訊。隊列的使用除去了接收和發送應用程序同時執行的要求。其中較爲成熟的MQ產品有IBM WEBSPHERE MQ等等。html
rabbitmq是採用Erlang(一種通用的面向併發的編程語言)編寫的符合AMQP(Advanced Message Queuing Protocol)規範的消息中間件。誕生於金融行業,如今普遍應用於企業級的分佈式應用中。node
從模型抽象上來講其工做過程:生產者(producer)建立消息,而後發佈到隊列(queue)中,最後將消息發送到監聽的消費者(consumer)。git
Exchange分發消息的類型有四種:github
routing key
(exchange和queue綁定時可指定)直接投遞到隊列 routing key
由.
號分隔的字符串指定,如quick.orange.rabbit
,每一個分隔的字符串最好能對應必定的業務含義。支持模糊匹配,如*.orange.rabbit
或如quick.orange.#
。*
精確匹配一個字符串,#
匹配0個或多個字符串。 routing key
。關於本節內容可參考:https://www.jianshu.com/p/79ca08116d57web
系統環境:centos 7.3算法
內網ip:192.168.228.130express
$ yum install -y erlang
此處直接yum安裝的版本爲erlang-R16B-03.18.el7.x86_64
在後面安裝Rabbitmq時會報錯:編程
錯誤:軟件包:rabbitmq-server-3.7.6-1.el7.noarch (/rabbitmq-server-3.7.6-1.el7.noarch) 須要:erlang >= 19.3 已安裝: erlang-R16B-03.18.el7.x86_64 (@epel) erlang = R16B-03.18.el7 您能夠嘗試添加 --skip-broken 選項來解決該問題 您能夠嘗試執行:rpm -Va --nofiles --nodigest
緣由是:rabbitmq-server-3.7.6依賴erlang版本要19.3=<erlang<=20.3.x(第一次根據官方提供的方法安裝的21.0版本——成功入坑,詳情繼續向下看)。vim
解決辦法(此處有坑):centos
# 下載erlang官方鏡像源 $ wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm # 配置鏡像源 $ rpm -Uvh erlang-solutions-1.0-1.noarch.rpm # 安裝erlang $ yum install -y erlang # 查看版本 $ rpm -q erlang erlang-21.0-1.el7.centos.x86_64 # 即安裝的爲21.0版本
再次入坑(使用上述辦法安裝erlang,在手動添加rabbitmq配置文件後),報錯信息:
Could not start application logger: Logger.App.start(:normal, []) returned an error: shutdown: failed to start child: Logger.ErrorHandler ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
按官方提供的方法安裝好erlang後,當手動添加rabbitmq配置文件後再次啓動出現上面的報錯信息,通過分析發現(太坑了):
再次爬坑: 根據上圖中的提示,只能再找辦法安裝19.3~20.3.x之間版本的erlang了,可是官方提供的方法只能安裝最新版的erlang,通過一番搜索找到以下辦法能夠安裝19.3~20.3.x之間版本的erlang:
# 手動配置erlang的repo源 # In /etc/yum.repos.d/rabbitmq-erlang.repo [rabbitmq-erlang] name=rabbitmq-erlang baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/20/el/7 gpgcheck=1 gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc repo_gpgcheck=0 enabled=1 # 清理原有rpm包緩存 $ yum clean all # 從新安裝20.3版本的erlang $ yum install -y erlang # 查看包狀態 $ rpm -q erlang erlang-20.3-1.el7.centos.x86_64 # 完成!
注意1: 由於以前安裝過舊版本的erlang會致使衝突,因此在安裝新版本erlang時須要將舊版本erlang有關的包所有卸載掉yum remove -y erlang
(使用rpm -e erlang --nodeps只能卸載erlang包,沒法卸載其相關的依賴包)。
注意2: 若是你以前像我同樣根據官方提示安裝過erlang-21.0,那麼確定也入坑了,爬坑過程當中卸載erlang的時候注意觀察會發現rabbitmq-server會同時被卸載,因此安裝好erlang-20.3後須要再次安裝rabbitmq-server,方法以下。
參考erlang官方文檔:https://www.erlang-solutions.com/resources/download.html
若是你安裝的是低版本的rabbitmq會比較省事,官方提供了erlang的rpm包:http://www.rabbitmq.com/releases/erlang/
# download rpm.repo $ wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.6/rabbitmq-server-3.7.6-1.el7.noarch.rpm $ rpm --import https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc # this example assumes the CentOS 7 version of the package $ yum install -y rabbitmq-server-3.7.6-1.el7.noarch.rpm # Done! $ rpm -q rabbitmq-server rabbitmq-server-3.7.6-1.el7.noarch
若是報錯請返回 「erlang 安裝」 。
centos7能夠直接使用系統工具管理服務
$ systemctl start/status/restart/stop rabbitmq-server # 查看rabbimq啓動的端口 $ netstat -lntp tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 96803/epmd tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 96660/beam.smp tcp6 0 0 :::4369 :::* LISTEN 96803/epmd tcp6 0 0 :::5672 :::* LISTEN 96660/beam.smp
參考rabbitmq官方文檔:http://www.rabbitmq.com/install-rpm.html
文件結構:
/var/lib/rabbitmq/ ├── config # 存放配置文件 ├── mnesia # 存放節點的數據、信息、狀態、日誌等文件 └── schema # 存放rabbitmq的綱要文件
怎麼尋找RabbitMQ配置文件位置:
每一個節點的配置信息均可以在log_file(首行)找到,內容以下
node : rabbit@host3 home dir : /var/lib/rabbitmq config file(s) : (none) # 在此發現rabbit@host1這個節點並無配置文件(後面補充) cookie hash : FFGcfZmAr6Q+pjUEHEiUEw== log(s) : /var/log/rabbitmq/rabbit@host3.log : /var/log/rabbitmq/rabbit@host3_upgrade.log database dir : /var/lib/rabbitmq/mnesia/rabbit@host3
能夠再Management_UI界面找到,前提是開啓UI插件,開啓方法以下:
$ rabbitmq-plugins enable rabbitmq_management
關於Management_UI用戶類型及權限管理:
官方關於如何分配服務器資源的 建議 :http://www.rabbitmq.com/production-checklist.html
{vm_memory_high_watermark, 0.4}
;disk_free_limit
;{disk_free_limit, {mem_relative, 1.0}}
,即當剩餘的磁盤空間和分配的內存空間相同時告警,這是保證節點正常運行的最低內存、磁盤空間比例;{disk_free_limit, {mem_relative, 1.5}}
,當剩餘磁盤空間爲分配的內存空間的1.5倍時告警,這樣配置比較安全;{disk_free_limit, {mem_relative, 2.0}}
,當剩餘磁盤空間爲分配的內存空間的2倍時告警,這樣配置最謹慎。自3.7.0版本開始,rabbitmq的配置文件命名爲rabbitmq.config,是一種標準的erlang配置文件,同時也兼容舊版本的配置文件(advanced.config)格式,與舊格式的配置文件相比,新格式配置文件更簡介、更清晰、更易於管理,且可使用Chef、Puppet等自動化部署工具生成。新舊版本配置文件對比e.g.:
新版配置文件格式的結構能夠總結爲如下三點:
key = value
模式;listeners.tcp.default = 5673
或,
[ {rabbit, [{tcp_listeners, [5673]}]} ].
配置完成後啓動rabbitmq:
# 重啓 $ systemctl restart rabbitmq-server # 查看端口 $ netstat -lntp tcp6 0 0 :::5673 :::* LISTEN 44435/beam.smp # 查看日誌 $ less /var/log/rabbitmq/rabbit@host3.log 2018-07-09 21:15:37.692 [info] <0.215.0> node : rabbit@host3 home dir : /var/lib/rabbitmq config file(s) : /etc/rabbitmq/rabbitmq.conf # 此時新配置文件生效 cookie hash : p40SfKMD4r8HemoR0EOJWw== log(s) : /var/log/rabbitmq/rabbit@host3.log : /var/log/rabbitmq/rabbit@host3_upgrade.log database dir : /var/lib/rabbitmq/mnesia/rabbit@host3
自定義rabbitmq的配置文件路徑:
$ vim /usr/lib/systemd/system/rabbitmq-server.service [Unit] Description=RabbitMQ broker After=syslog.target network.target [Service] Type=notify User=rabbitmq Group=rabbitmq UMask=0027 NotifyAccess=all TimeoutStartSec=3600 # RABBITMQ_CONFIG_FILE=/etc/rabbitmq/rabbitmq.conf # 指定配置文件 # Un-comment this setting if you need to increase RabbitMQ's # open files limit # LimitNOFILE=16384 # # Note: systemd on CentOS 7 complains about in-line comments, # so only append them here # # Restart: # The following setting will automatically restart RabbitMQ # in the event of a failure. systemd service restarts are not a # replacement for service monitoring. Please see # http://www.rabbitmq.com/monitoring.html Restart=on-failure RestartSec=10 WorkingDirectory=/var/lib/rabbitmq ExecStart=/usr/sbin/rabbitmq-server ExecStop=/usr/sbin/rabbitmqctl shutdown # See rabbitmq/rabbitmq-server-release#51 SuccessExitStatus=69 [Install] WantedBy=multi-user.target
配置文件詳解:
說明:
開啓管理插件:
$ rabbitmq-plugins enable rabbitmq_management <輸出內容> The following plugins have been configured: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch Applying plugin configuration to rabbit@host3... The following plugins have been enabled: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch started 3 plugins. <開啓成功>
訪問web界面:(默認端口爲15672)http://server_ip:15672
爲guest用戶受權:loopback_users = none
(默認只能是本機訪問,localhost或者127.0.0.1,從外部訪問須要添加該配置)
重啓後訪問,登陸管理頁面:
管理頁面能作的操做:
list and delete exchanges, queues, bindings, users, virtual hosts and permissions. Monitor queue length, message rates globally and per channel, data rates per connection, etc. Monitor resource usage, such as file descriptors, memory use, available disk space. Manage users (provided administrative permissions of the current user). Export and import object definitions (vhosts, users, permissions, queues, exchanges, bindings, parameters, policies) to JSON. Force close connections, purge queues. Send and receive messages (useful in development environments and for troubleshooting).
由於guest爲默認用戶,對外開放並不安全,可是經過localhost來訪問又不方便(經過配置代理也能夠實現外網訪問),因此在此還可使用命令行進行管理,在此準備了兩種管理工具:rabbitmqctl、rabbitmqadmin(二者不衝突)。
rabbitmqctl爲rabbitmq內置管理工具,功能不少,詳細的使用方法能夠參考下面的連接:http://www.rabbitmq.com/man/rabbitmqctl.8.html。
用法示例:
添加用戶:
# rabbitmqctl add_user <username> <newpassword> $ rabbitmqctl add_user adai 123456 Adding user "adai" ...
查看用戶信息:
$ rabbitmqctl list_users Listing users ... adai [] guest [administrator]
修改用戶密碼:
# rabbitmqctl change_password <username> <newpassword> $ rabbitmqctl change_password adai 111111 Changing password for user "adai" ...
爲用戶受權:
# 格式: rabbitmqctl set_permissions [-pvhostpath] {user} {conf} {write} {read} ## vhost The name of the virtual host to which to grant the user access, defaulting to "/". ## user The name of the user to grant access to the specified virtual host. ## conf A regular expression matching resource names for which the user is granted configure permissions. ## write A regular expression matching resource names for which the user is granted write permissions. ## read A regular expression matching resource names for which the user is granted read permissions. # 示例:受權給用戶adai默認vhost("/")的全部配置的讀、寫權限 $ rabbitmqctl set_permissions -p "/" adai ".*" ".*" ".*" Setting permissions for user "adai" in vhost "/" ...
查看用戶權限:
# rabbitmqctl list_permissions [-p vhost] $ rabbitmqctl list_permissions -p / Listing permissions for vhost "/" ... guest .* .* .* adai .* .* .*
清理用戶權限:
# rabbitmqctl clear_permissions [-p vhost] username $ rabbitmqctl clear_permissions -p / adai Clearing permissions for user "adai" in vhost "/" ... $ rabbitmqctl list_permissions -p / Listing permissions for vhost "/" ... guest .* .* .*
查看某用戶的權限:
$ rabbitmqctl list_user_permissions adai Listing permissions for user "adai" ... $ rabbitmqctl set_permissions -p "/" adai ".*" ".*" ".*" Setting permissions for user "adai" in vhost "/" ... $ rabbitmqctl list_user_permissions adai Listing permissions for user "adai" ... / .* .* .*
設置用戶角色(上面有講,共五種角色類型):
$ rabbitmqctl set_user_tags adai administartor # 將adai設置爲管理員 Setting tags for user "adai" to [administartor] ... $ rabbitmqctl list_users Listing users ... adai [administartor] guest [administrator]
刪除用戶:
# rabbitmqctl delete_user <username> $ rabbitmqctl add_user test monitoring Adding user "test" ... $ rabbitmqctl list_users Listing users ... adai [administartor] guest [administrator] test [] $ rabbitmqctl delete_user test Deleting user "test" ... $ rabbitmqctl list_users Listing users ... adai [administartor] guest [administrator]
該工具來自第三方,獲取方法:
若是你安裝了UI_management插件,能夠直接經過下面方法獲取
wget http://server_ip:15672/cli/rabbitmqadmin # 注意: ## The tool supports: ## Python 3.x ## Python 2.6 or later for HTTP connections ## Python 2.7.9 or later for HTTPS connections
還能夠經過github倉庫獲取,地址https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.7.6/bin/rabbitmqadmin
獲取rabbitmqadmin後,將其加入系統環境:/usr/local/bin
mv rabbitmqadmin /usr/local/bin/ # 賦予其執行權限 chmod 755 /usr/local/bin/rabbitmqadmin # 激活rabbitmqadmin命令 rabbitmqadmin --help
注意: rabbitmqadmin的版本要好rabbitmq的版本對應,不然沒法使用。
rabbitmqadmin工具支持在bash中以列表的輸出信息:
# 開啓rabbitmqadmin的列表支持功能 rabbitmqadmin --bash-completion > /etc/bash_completion.d/rabbitmqadmin