RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

时间:2024-04-06 12:20:32

这篇文章仅用来记录如何在Linux环境下安装RocketMQ,方便下次要再次安装可以快速找到教程。同时记录了在安装过程中可能出现的问题。本文下载的是4.4.0版本的RocketMQ。需要环境:CentOS6系统,JDK1.8+(配置好$JAVA_HOME),Maven 3.2+。


1、下载安装包,从官网下载对应的压缩包(https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.4.0/rocketmq-all-4.4.0-source-release.zip),在弹出页面选择红框内的镜像链接。

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

 2、将压缩包保存到/opt/RocketMQ并解压

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

3、在控制台下依次输入下面代码,最终路径:/opt/RocketMQ/rocketmq-all-4.4.0/distribution/target/apache-rocketmq

cd /opt/RocketMQ/rocketmq-all-4.4.0
mvn -Prelease-all -DskipTests clean install -U
cd distribution/target/apache-rocketmq

4、启动nameServer,在控制台依次输入

nohup sh bin/mqnamesrv &
tail -f nohup.out

如果控制台显示以下信息(3)则说明启动成功

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

注意:可能在运行的时候会报错:

Java HotSpot(TM) 64-Bit Server VM warning: Using the DefNew young collector with the CMS collector is deprecated and will likely be removed in a future release
Java HotSpot(TM) 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.
Java HotSpot(TM) 64-Bit Server VM warning: MaxNewSize (4194304k) is equal to or greater than the entire heap (4194304k).  A new max generation size of 4194240k will be used.
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000006ec800000, 2147418112, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2147418112 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /opt/RocketMQ/rocketmq-all-4.4.0/distribution/target/apache-rocketmq/hs_err_pid3162.log

主要原因是启动nameserver默认的最小内存是4个g,但是服务器内存不足(我起的是虚拟机,只分配了2G的内存)。所以要修改nameserver的配置文件(runserver.sh)。(启动broker的时候可能也会因为这个原因报错,具体操作差不多,只不过是修改runbroker.sh)

修改/opt/RocketMQ/rocketmq-all-4.4.0/distribution/target/apache-rocketmq/bin/runserver.sh文件,修改内存分配,然后再次启动。

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

5、启动broker,在控制台依次输入:

nohup sh bin/mqbroker -n localhost:9876 &
tail -f nohup.out

如果控制台出现红框内容,则说明启动成功,红框上面的是刚刚启动nameserver时的信息。

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

注意:除了出现上面说的启动broker时内存不足,还可能出现无法识别主机名:

java.net.UnknownHostException: czz.centOS: czz.centOS: Name or service not known
        at java.net.InetAddress.getLocalHost(InetAddress.java:1506) ~[na:1.8.0_201]
        at org.apache.rocketmq.common.BrokerConfig.localHostName(BrokerConfig.java:185) [rocketmq-common-4.4.0.jar:4.4.0]
        at org.apache.rocketmq.common.BrokerConfig.<init>(BrokerConfig.java:38) [rocketmq-common-4.4.0.jar:4.4.0]
        at org.apache.rocketmq.broker.BrokerStartup.createBrokerController(BrokerStartup.java:110) [rocketmq-broker-4.4.0.jar:4.4.0]
        at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:58) [rocketmq-broker-4.4.0.jar:4.4.0]
Caused by: java.net.UnknownHostException: czz.centOS: Name or service not known
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_201]
        at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_201]
        at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_201]
        at java.net.InetAddress.getLocalHost(InetAddress.java:1501) ~[na:1.8.0_201]
        ... 4 common frames omitted

解决方法:在控制台输入 

 vi /etc/hosts

在后面添加一条主机信息。由于我的主机名是czz.centOS,所以我添加。保存后再次启动broker

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

6、使用jps查看是否启动成功

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

7、关闭nameserver broker执行的命令

#官方建议先关闭broker,因为broker依赖nameserver
sh bin/mqshutdown broker
#再关闭nameserver
sh bin/mqshutdown namesrv

8、验证是否能够生产和消费消息,依次输入以下代码

生产消息:

#设置名称服务地址
export NAMESRV_ADDR=localhost:9876
#投递消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer

结果:

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

消费消息:

#消费消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer

结果:

RocketMQ学习记录-----安装和启动RocketMQ以及可视化控制台

官方文档:http://rocketmq.apache.org/docs/quick-start/

控制台安装可以参考:https://blog.csdn.net/chenzz2560/article/details/90238769后面的教程