在 Linux Ubuntu 18.04.x 上安装和配置 ActiveMQ - Artemis





0/5 (0投票)
如何在 Linux Ubuntu 18.04.x 操作系统上安装/配置 Artemis
假设/预期
Linux 命令和 Vim
我假设本文的读者对 Linux shell 和命令有一定的了解,对所使用的 Linux 命令进行解释已超出本文的范围。除非另有说明,否则 Linux 命令将在终端中运行。
我使用 vim 编辑器进行文本编辑,但读者可以使用任何其他文本编辑器。
'root' 用户
本文中的所有命令均由 root 用户发出。
背景
面向消息的中间件 (MoM)
面向消息的中间件 (MoM) 是支持分布式系统之间消息的发送和接收的软件或硬件基础设施。
为什么使用 MoM?
MoM 允许应用程序模块分布在异构平台上,并降低了开发跨越多个操作系统、网络协议和编程语言的应用程序的复杂性。MoM 通过提供可靠性、事务和许多其他功能,使系统中的进程能够通过一种定义明确的协议进行通信。
Apache ActiveMQ Artemis
Apache ActiveMQ Artemis (Artemis) 是一个异步消息系统,是 MoM 系统的软件实现。Artemis 能够通过运行的 broker 实例来协调分布式系统中进程之间的消息流量。
Artemis 支持的消息协议
Artemis 支持以下协议
- MPQOpenWire
- MQTT
- STOMP
- HornetQ (用于 HornetQ 客户端)
- Core (Artemis CORE 协议)
例如,在一个三层系统中,可能有使用 Java、C/C++、Python、GoLang、nodejs 等编写的进程,它们分布在前台、中台和后台运行。这些进程可以通过注册到指定的队列或主题来发送/接收消息,相互通信(例如,使用 STOMP 协议)。
系统信息
- 硬件:Odroid N2 (ARM 64)
- 操作系统:Ubuntu 18.04.4 LTS (Bionic Beaver)
JAVA (openjdk-11) 安装
Artemis 需要 JRE (至少 8 版本) 才能运行。以下是 openjdk-11 的安装/配置步骤。
安装
sudo apt install openjdk-11-jdk
验证
which java
/usr/bin/java
java --version
openjdk 11.0.7 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode)
安装后配置:设置 JAVA_HOME 环境变量
Artemis 要求设置 JAVA_HOME
环境变量。以下是在 /etc/bash.bashrc 中设置它并激活它的步骤。
确定 JDK 已安装目录的绝对路径
在 Ubuntu 中,jdk 安装在 /usr/lib/jvm 下。jdk 构建中的二进制文件是平台特定的;因此,JAVA_HOME
变量必须指向一个平台特定的已安装 JDK (在本例中,平台是 ARM64)。
这是 /usr/lib/jvm 下的样子
pwd
/usr/lib/jvm
ls
java-1.11.0-openjdk-arm64 java-11-openjdk-arm64 openjdk-11
因此,JAVA_HOME
环境变量应设置为“/usr/lib/jvm”。
在 /etc/bash.bashrc 中设置 JAVA_HOME
编辑文件 /etc/bash.bashrc
vim /etc/bash.bashrc
添加以下行 (在此文件的底部)
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64
加载脚本
source /etc/bash.bashrc
验证变量的值
echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-arm64
2. ActiveMQ-Artemis 安装/配置
从 Apache.org 下载 Artemis
打开网页浏览器,指向以下 URL
https://activemq.apache.ac.cn/components/artemis/download/
源代码和二进制包的 URL
https://activemq.apache.ac.cn/components/artemis/download/
注意:
- 本教程使用了 tar 包 (本例中的文件位于 /media/SDA1)。
- ActiveMQ 服务器及其 broker 将安装在“/opt”目录中。
- 我将使用的 broker 名称是 `
ActiveMQ-Odroid-110
`。
解压下载的包
pwd
/opt
tar -xzf /media/SDA1/apache-artemis-2.12.0-bin.tar.gz
验证
ls
(among other things…) apache-artemis-2.12.0
创建一个新的 Linux 用户组 `activemq`
addgroup --quiet --system activemq
创建一个新的 Linux 用户 `activemq`
adduser --quiet --system --ingroup activemq \
--no-create-home --disabled-password activemq
更改 `/opt/apache-artemis-2.12.0` 目录的所有权
chown -R activemq:activemq /opt/activemq
导航到 `bin` 目录
cd apache-artemis-2.12.0/bin
验证
./artemis
如果您看到以下输出,则表示您已准备好进行 broker 安装
usage: artemis <command> [<args>]
The most commonly used artemis commands are:
address Address tools group (create|delete|update|show)
(example ./artemis address create)
browser It will browse messages on an instance
consumer It will consume messages from an instance
create creates a new broker instance
data data tools group (print) (example ./artemis data print)
help Display help information
mask mask a password and print it out
migrate1x Migrates the configuration of a 1.x Artemis Broker
producer It will send messages to an instance
queue Queue tools group (create|delete|update|stat|purge)
(example ./artemis queue create)
See 'artemis help <command>' for more information on a specific command.
创建 Broker
在此示例中,broker 将具有以下属性
- 名称:ActiveMQ-Odroid-110
- 管理员 UID:admin
- 管理员密码:admin
./artemis create ActiveMQ-Odroid-110 --user=admin --password=admin \
--http-host 0.0.0.0 --relax-jolokia
... 安装将有一个额外的提示 (只需键入“Y”并按 Enter)
--allow-anonymous | --require-login: is a mandatory property!
Allow anonymous access?, valid values are Y,N,True,False
Y <--- Enter
其余输出
Auto tuning journal ...
done! Your system can make 1.61 writes per millisecond,
your journal-buffer-timeout will be 620000
You can now start the broker by executing:
"/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis" run
Or you can run the broker in the background using:
"/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service" start
"/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service"
Usage: ./artemis-service {start|stop|restart|force-stop|status}
启动 Broker 实例
现在,我们准备运行 broker 了
测试运行 #1:手动启动 Broker 进行测试运行
"/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis" run
现在,打开网页浏览器,指向以下网站
http://<IP of the broker>:8161
如果您看到以下网页,则表示 broker 实例已成功启动并运行
单击 `Management Console` 链接以打开登录页面 (注意:用户名=admin,密码=admin,如之前在 broker 创建期间指定的那样)。
ActiveMQ 管理控制台初始页面
恭喜,您已成功安装了第一个 ActiveMQ broker!
测试运行 #2:将 Broker 启动为守护进程
要将 broker 作为后台服务运行,请先终止当前 broker 实例 (即,在 broker 运行的控制台中按 Ctrl+C),然后运行此命令
"/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service" start
输出
Starting artemis-service
artemis-service is now running (14289)
验证
ps -ef| grep 14289
输出
root 14289 1 10 18:18 pts/0 00:00:23 /usr/lib/jvm/java-11-openjdk-arm64/bin/java
-XX:+PrintClassHistogram -XX:+UseG1GC -XX:+UseStringDeduplication -Xms512M -Xmx2G
-Dhawtio.realm=activemq -Dhawtio.offline=true -Dhawtio.role=amq
-Dhawtio.rolePrincipalClasses=org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal
-Djolokia.policyLocation=file:/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/etc/jolokia-
access.xml -Xbootclasspath/a:/opt/apache-artemis-2.12.0/lib/jboss-logmanager-2.1.10.Final.jar:
/opt/apache-artemis-2.12.0/lib/wildfly-common-1.5.2.Final.jar
-Djava.security.auth.login.config=/opt
/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/etc/login.config
-classpath /opt/apache-artemis-2.12.0
/lib/artemis-boot.jar -Dartemis.home=/opt/apache-artemis-2.12.0 -Dartemis.instance=/opt/apache-
artemis-2.12.0/bin/ActiveMQ-Odroid-110
-Djava.library.path=/opt/apache-artemis-2.12.0/bin/lib/linux-
aarch64 -Djava.io.tmpdir=/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/tmp -Ddata.dir=/opt
/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/data -Dartemis.instance.etc=/opt/apache-artemis-
2.12.0/bin/ActiveMQ-Odroid-110/etc -Djava.util.logging.manager=org.jboss.logmanager.LogManager
-Dlogging.configuration=file:/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/etc
//logging.properties org.apache.activemq.artemis.boot.Artemis run
再次注销/登录到 Web 控制台,以确保一切正常。
现在,停止守护进程
"/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service" stop
此时,我们已准备好为 broker 实例创建一个新的 systemd 服务。
创建 Broker 的系统服务
此时,我们已在前景和守护进程模式下测试了 broker。但是,在这些模式下,进程将不会在系统重启后继续运行。为了避免每次系统重启时手动重新启动 broker 进程,我们需要创建一个系统服务,并让 Linux 操作系统自动处理所有启动/停止。
创建系统服务脚本
touch /etc/systemd/system/activemq-artemis.service
chmod 644 /etc/systemd/system/activemq-artemis.service
vim /etc/systemd/system/activemq-artemis.service
将以下行添加到文件中
[Unit]
Description=Apache ActiveMQ Artemis
After=network.target
[Service]
Type=forking
User=activemq
Group=activemq
ExecStart=/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service start
ExecStop=/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service stop
[Install]
WantedBy=multi-user.target
加载/启动服务
systemctl daemon-reload
systemctl start activemq-artemis
启用服务 (以便它能在系统重启后继续运行)
systemctl enable activemq-artemis
输出
Created symlink /etc/systemd/system/multi-user.target.wants/
activemq-artemis.service → /etc/systemd
/system/activemq-artemis.
使用 system-service 进行验证
systemctl status activemq-artemis
输出应类似于以下内容
Loaded: loaded (/etc/systemd/system/activemq-artemis.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-05-02 16:17:52 UTC; 34s ago
Process: 2894
ExecStart=/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service start
(code=exited, sta
Main PID: 2919 (java)
Tasks: 50 (limit: 3838)
CGroup: /system.slice/activemq-odroid-110.service
└─2919 java -XX:+PrintClassHistogram -XX:+UseG1GC
-XX:+UseStringDeduplication -Xms512M -Xmx2G -Dhawtio.realm=
May 02 16:17:30 odroid systemd[1]: Starting Apache ActiveMQ Artemis...
May 02 16:17:30 odroid artemis-service[2894]: Starting artemis-service
May 02 16:17:52 odroid artemis-service[2894]: artemis-service is now running (2919)
May 02 16:17:52 odroid systemd[1]: Started Apache ActiveMQ Artemis.
可选:通过 broker 的状态报告进行验证
/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service status
输出应类似于以下内容
artemis-service is running (16934)
测试系统停止服务
systemctl stop activemq-odroid-110
使用 system-service 进行验证
systemctl status activemq-odroid-110
最后的几行输出应类似于以下内容
May 02 16:35:13 odroid artemis-service[4031]: Gracefully Stopping artemis-service
May 02 16:35:14 odroid systemd[1]: activemq-odroid-110.service:
Main process exited, code=exited, status=143/n/a
May 02 16:35:14 odroid systemd[1]: activemq-odroid-110.service:
Failed with result 'exit-code'.
May 02 16:35:14 odroid systemd[1]: Stopped Apache ActiveMQ Artemis.
通过 broker 的状态报告进行验证
/opt/apache-artemis-2.12.0/bin/ActiveMQ-Odroid-110/bin/artemis-service status
输出应类似于以下内容
artemis-service is stopped
关于创建其他 Broker 的说明
Artemis 支持在同一主机上同时运行多个 broker,因此可以按照本教程中的步骤根据需要创建其他 broker。
至此,在 Linux Ubuntu/ARM64 系统上安装 Apache-Artemis 的步骤完成。
历史
- 2020 年 5 月 8 日:初始版本