消息隊列-RabbitMQ 二 安裝和使用

環境和版本

ubuntu18, 最新的MQ版本

how

  1. 使用apt軟件倉庫, 可能會有版本的延遲
  2. 手動安裝,使用dpkg -i, 需要手動安裝所有依賴

Erlang安裝

Rabbit Mq是使用Erlang編寫,需要安裝合適的版本,最簡單的是使用apt安裝。
標準的debian和ubuntu倉庫可能會提供過期的版本,但是RMQ團隊維持了一個Erlang/OTP版本供apt安裝。

安裝步驟如下

  • 導入倉庫簽名key
    為了可以使用這個倉庫,我們需要添加apt-key Rmq簽名的key,使得apt信任該軟件包。這部分工作可以使用一個key server(可能容易過載)或者直接使用下載的key(推薦)。
  1. 下載key
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc | sudo apt-key add -
原站勘誤
  1. 使用key server
sudo apt-key adv --keyserver "hkps.pool.sks-keyservers.net" --recv-keys "0x6B73A36E6026DFCA"

開啟apt https 傳輸

sudo apt-get install apt-transport-https
  • 添加倉庫資源列表
    所有的第三方apt倉庫,都需要有一個文件來描述,并且存放在/etc/apt/source.list.d/目錄下,建議存放在/etc/apt/source.list.d/bintray.erlang.list
    資源倉庫的定義文件使用下面的模式:
# This repository provides RabbitMQ packages
# See below for supported distribution and component values
deb https://dl.bintray.com/rabbitmq/debian $distribution $component

不得不說國外網站講解的真是細致,國內快猛操的模式,導致大家干啥都很浮躁,沒有這種好的態度,不得不說人家只憑這種態度還是值得我們學習的。(什么狗屁的崇洋媚外,在這點我不得不一吐為快,rabbitMQ你值得擁有,當然也不一概而論,我準備在Linux上放棄Vmare了,ubuntu18搭建centos7的nat(wifi網卡)搞得我圖血,網站上一點都搜不到相關的知識,有了解的同學可以留言,網上搜出來的不對題的麻煩不要留言,我玩了一整天,看到一萬遍c-p。不然今天應該是centos7+rabbitMq了)。下面的我就不翻譯了,大家膜拜一下原汁原味的態度吧。

The next couple of sections discuss what distribution and component values are supported.

Distribution

In order to set up an apt repository that provides the correct package, a few decisions have to be made. One is determining the distribution name. It comes from the Debian or Ubuntu release used:

  • bionic for Ubuntu 18.04
  • xenial for Ubuntu 16.04
  • buster for Debian Buster
  • stretch for Debian Stretch

Erlang/OTP Version

Another is what Erlang/OTP release version should be provisioned. It is possible to track a specific series (e.g. 21.x) or install the most recent version available. The choice determines what Debian repository component will be configured.

Consider the following repository file at /etc/apt/sources.list.d/bintray.erlang.list:

# Installs the latest 21.x version available in the repository.
deb http://dl.bintray.com/rabbitmq-erlang/debian bionic erlang-21.x

It configures apt to install the most recent Erlang 21.x version available in the repository and use packages for Ubuntu 18.04 (Bionic).

For Debian Stretch the file would look like this:

# Installs the latest 21.x version available in the repository.
deb http://dl.bintray.com/rabbitmq-erlang/debian stretch erlang-21.x

To use the most recent 20.x patch release available, switch the component to erlang-20.x:

# Installs the latest 20.x version available in the repository.
deb http://dl.bintray.com/rabbitmq-erlang/debian bionic erlang-20.x

erlang-21.x, erlang-19.x, and erlang-16.x are the components for Erlang 21.x, 19.x and R16B03, respectively.

The erlang component installs the most recent version available:

# Installs the latest version available in the repository.
# Consider using version pinning.
deb http://dl.bintray.com/rabbitmq-erlang/debian bionic erlang

That version may or may not be supported by RabbitMQ, so version pinning is highly recommended.

  • 更新包metadata
sudo apt-get update -y
  • 安裝合適的Erlang包
# This is recommended. Metapackages such as erlang and erlang-nox must only be used
# with apt version pinning. They do not pin their dependency versions.
sudo apt-get install -y erlang-base \
                        erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                        erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                        erlang-runtime-tools erlang-snmp erlang-ssl \
                        erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl

包版本和倉庫鎖定(apt pinning)感興趣的同學可以自行google
至此我們已經安裝完成了Erlang,what?那MQ呢... heihei,同學你已經一只腳邁向了地球(welcome earth, Allies),歡迎來到MQ的地球

使用apt安裝rabbit mq

磨刀不誤砍柴工,既然你看了上面的安裝過程,下面一樣

#!/bin/sh

## Install RabbitMQ signing key
curl -fsSL https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc | sudo apt-key add -

## Install apt HTTPS transport
sudo apt-get install apt-transport-https

## Add Bintray repositories that provision latest RabbitMQ and Erlang 21.x releases
sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list <<EOF
deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang-21.x
deb https://dl.bintray.com/rabbitmq/debian bionic main
EOF

## Update package indices
sudo apt-get update -y

## Install rabbitmq-server and its dependencies
sudo apt-get install rabbitmq-server -y --fix-missing

再次五體投地原汁原味膜拜網站小哥的態度,顫抖吧 E.T

The next couple of sections discusses what distribution and component values are supported.

Distribution

In order to set up an apt repository that provides the correct package, a few decisions have to be made. One is determining the distribution name. It comes from the Debian or Ubuntu release used:

  • bionic (Ubuntu 18.04)
  • artful
  • trusty
  • sid
  • buster
  • stretch
  • xenial (Ubuntu 16.04)
  • yakkety
  • zesty

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

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

where {distribution} is the name of the Debian or Ubuntu distribution used (see above).

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

It is possible to list multiple repositories, for example, one that provides RabbitMQ and one that provides Erlang/OTP packages. On Ubuntu 18.04 that can be done by modifying the command in the above example like so:

>sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list <<EOF
deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang-21.x
deb https://dl.bintray.com/rabbitmq/debian bionic main
EOF

and on Ubuntu 16.04 it would be

>sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list <<EOF
deb https://dl.bintray.com/rabbitmq-erlang/debian xenial erlang-21.x
deb https://dl.bintray.com/rabbitmq/debian xenial main
EOF

Install RabbitMQ Package

After updating the list of apt sources it is necessary to run apt-get update:

>sudo apt-get update -y

Then install the package with

>sudo apt-get install -y rabbitmq-server

(手動安裝我已不翻譯了)

Manual Installation with Dpkg

In some cases it may easier to download the package directly from GitHub and install it manually using sudo dpkg -i. Below is a download link.

Description Download Signature
.deb for Debian-based Linux (from GitHub) rabbitmq-server_3.7.17-1_all.deb Signature

When installing manually with dpkg, it is necessary to install package dependencies first. dpkg, unlike apt, does not resolve or manage dependencies.

Here's an example that does that, installs wget, downloads the RabbitMQ package and installs it:

# sync package metadata
sudo apt-get update
# install dependencies manually
sudo apt-get -y install socat logrotate init-system-helpers adduser

# download the package
sudo apt-get -y install wget
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.17/rabbitmq-server_3.7.17-1_all.deb

# install the package with dpkg
sudo dpkg -i rabbitmq-server_3.7.17-1_all.deb

rm rabbitmq-server_3.7.17-1_all.deb

Installation via apt repositories on Bintray and Package Cloud is recommended over downloading the package directly and installing via dpkg -i. When the RabbitMQ package is installed manually with dpkg -i the operator is responsible for making sure that all package dependencies are met.

User Privilege Requirements

RabbitMQ Debian package will require sudo privileges to install and manage. In environments where sudo isn't available, consider using the generic binary build instead.

讓MQ run 起來

安裝時,rabbit mq默認以守護進程啟動,并且使用沒有權限的用戶 rabbitmq
可以使用super user的service命令啟動。關閉、和查看rabbitmq-server狀態

sudo service rabbitmq-server status[start| stop | restart]

端口獲取

rabbitMq節點綁定有個開放的tcp端口,用來接受client端和CLI工具的鏈接。其他的工具或者進程可能占用或者阻止端口,竟會出現錯誤。要保證一下的端口可用性,因為Firewalls和SeLinux會屏蔽端口連接。

  • 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). Unless external connections on these ports are really necessary (e.g. the cluster uses federation or CLI tools are used on machines outside the subnet), these ports should not be publicly exposed. 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 server distribution port + 10000 through server distribution port + 10010). See networking guide for details.
  • 15672: HTTP API clients, management UI 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)

It is possible to configure RabbitMQ to use different ports and specific network interfac

默認user access

broker會創建一個user為guest,密碼為guest。通常沒有配置的clients使用這個憑證。但是默認的這些憑證只能從localhost連接broker,所以你需要配置user用于網絡上的連接。

See the documentation on access control for information on how to create more users and delete the guest user.

系統限制

生產級別的rabbitMq可能需要系統limit和內核參數調優到允許處理高并發連接和緩沖隊列,主要是ulimit -n參數,生產環境可以調到最大的65535,開發環境4096,下面knee down & suck

With systemd (Recent Linux Distributions)

On distributions that use systemd, the OS limits are controlled via a configuration file at /etc/systemd/system/rabbitmq-server.service.d/limits.conf. For example, to set the max open file handle limit (nofile) to 64000:

[Service]
LimitNOFILE=64000

See systemd documentation to learn about the supported limits and other directives.

With Docker

To configure kernel limits for Docker contains, use the "default-ulimits" key in Docker daemon configuration file. The file has to be installed on Docker hosts at /etc/docker/daemon.json:

{
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 64000,
      "Soft": 64000
    }
  }
}

Without systemd (Older Linux Distributions)

The most straightforward way to adjust the per-user limit for RabbitMQ on distributions that do not use systemd is to edit the /etc/default/rabbitmq-server (provided by the RabbitMQ Debian package) or rabbitmq-env.conf to invoke ulimit before the service is started.

>ulimit -S -n 4096

This soft limit cannot go higher than the hard limit (which defaults to 4096 in many distributions). The hard limit can be increased via /etc/security/limits.conf. This also requires enabling the pam_limits.so module and re-login or reboot. Note that limits cannot be changed for running OS processes.

For more information about controlling fs.file-max with sysctl, please refer to the excellent Riak guide on open file limit tuning.

驗證系統limit(應該只能驗證配置參數,系統參數需要系統里面配置)

rabbitnqctl satus
or 
cat /proc/$rabbitmq_bean_process_pid/limits

See the CLI tools guide to learn more.

Log Files and Management

Server logs can be found under the configurable directory, which usually defaults to /var/log/rabbitmq when RabbitMQ is installed via a Linux package manager.

RABBITMQ_LOG_BASE can be used to override log directory location.

Assuming a systemd-based distribution, system service logs can be inspected using

journalctl --system

which requires superuser privileges. Its output can be filtered to narrow it down to RabbitMQ-specific entries:

sudo journalctl --system | grep rabbitmq

The output will look similar to this:

Dec 26 11:03:04 localhost rabbitmq-server[968]: ##  ##
Dec 26 11:03:04 localhost rabbitmq-server[968]: ##  ##      RabbitMQ 3.7.16\. Copyright (c) 2007-2019 Pivotal Software, Inc.
Dec 26 11:03:04 localhost rabbitmq-server[968]: ##########  Licensed under the MPL.  See http://www.rabbitmq.com/
Dec 26 11:03:04 localhost rabbitmq-server[968]: ######  ##
Dec 26 11:03:04 localhost rabbitmq-server[968]: ##########  Logs: /var/log/rabbitmq/rabbit@localhost.log
Dec 26 11:03:04 localhost rabbitmq-server[968]: /var/log/rabbitmq/rabbit@localhost_upgrade.log
Dec 26 11:03:04 localhost rabbitmq-server[968]: Starting broker...
Dec 26 11:03:05 localhost rabbitmq-server[968]: systemd unit for activation check: "rabbitmq-server.service"
Dec 26 11:03:06 localhost rabbitmq-server[968]: completed with 6 plugins.

Log Rotation

The broker always appends to the log files, so a complete log history is retained.

logrotate is the recommended way of log file rotation and compression. By default, the package will set up logrotate to run weekly on files located in default /var/log/rabbitmq directory. Rotation configuration can be found in /etc/logrotate.d/rabbitmq-server.

rabbitmq 有很多命令行工具

  • rabbitmqctl 服務管理和通常的操作任務
  • rabbitmq-diagnostics 診斷額健康檢查
  • rabbitmq-plugins 插件管理
  • rabbitmqadmin http api的操作任務

感興趣的可以自行查閱doc文檔

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容