RocketMQ learning environment construction (RocketMQ installation and IDEA Debug environment construction)

Click "Middleware Interest Circle" above and select "Set as Star"

Be a positive person, the harder you work, the luckier you get!

This article is mainly divided into the following parts:

  • Linux server installation RocketMQ, RocketMQ-Console

  • Build a debuggable environment in IDEA

1. Install RocketMQ and RocketMQ-Console on Linux


1.1 Install RocketMQ

Step1: Download the RocketMQ installation package from the following address

cd /opt/application
wget https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.7.1/rocketmq-all-4.7.1-bin-release.zip

Step2: Unzip the installation package

unzip rocketmq-all-4.7.1-bin-release.zip
ls -l

The decompressed file is as shown below:

The conf folder stores RocketMQ configuration files, which provide sample configurations for various deployment structures. For example 2m-2s-async is an example configuration for 2-master 2-slave asynchronous replication; 2m-noslave is an example configuration for 2-master. Since this article is mainly to build a learning environment, the deployment architecture adopted is a primary deployment architecture. How to build a RocketMQ cluster in a production environment and how to tune parameters will be specially introduced in the subsequent articles of this column.

Step3: Modify Nameserver jvm parameters

cd bin
vi runserver.sh

#
 定位到如下代码
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

#
 修改 "-Xms -Xmx -Xmn" 参数
JAVA_OPT="${JAVA_OPT} -server -Xms512M -Xmx512M -Xmn256M -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

Reminder: The main purpose of modifying the JVM parameters here is that the memory of the personal learning computer is not enough, and the default NameServer will occupy 4G.

Step4: Start the nameserver

nohup ./mqnamesrv &

Check the ${ user_home }/logs/rocketmqlogs/namesrv.log log file, if the output is as shown in the figure below, it means the startup is successful.

After the above steps, the RocketMQ Nameserver server and the Broker server were successfully installed on the Linux environment.

Reminder: If an error occurs during the installation process above, you can check the logs in ${ user_home }/logs/rocketmqlogs. Through the error log, you can more intuitively judge the cause of the error. Where ${ user_home } is the user home directory.

该目录下会有众多的日志文件,如果一开始对这些文件的含义不了解也没关系,大家可以通过 ls -l 命令,逐一查看文件大小不为0的文件,从而寻找错误日志,便于快速解决问题。

RocketMQ提供了众多的运维命令来查看RocketMQ集群的运行状态,在这里我先简单使用clusterList命令来查看集群的状态,用于验证一下集群的状态。

sh ./mqadmin clusterList -n 127.0.0.1:9876

其运行结果如下图所示:

1.2 安装RocketMQ-Console

使用运维命令不太直观,学习成本较大,为此RocketMQ官方提供了一个运维管理界面RokcetMQ-Console,用于对RocketMQ集群提供常用的运维功能,故本节主要讲解如何在Linux环境安装rocketmq-console。

RocketMQ官方并未提供rocketmq-console的安装包,故需要通过源码进行编译。

Step1:下载源码

wget https://github.com/apache/rocketmq-externals/archive/rocketmq-console-1.0.0.tar.gz
tar -xf rocketmq-console-1.0.0.tar.gz
# 重命名,为了方便后续操作
mv rocketmq-externals-rocketmq-console-1.0.0/rocketmq-console  rocketmq-console

Step2:修改配置文件

cd rocketmq-console
vi src/main/resources/applications.properties

主要是修改指向的nameserver地址,修改结果如下图所示:

Step3: 使用maven命令编译源代码。
mvn clean  package -DskipTests

编译后在target目录下会生成可运行的jar包,如下图所示:

Step4: 我们可以将该包复制到自己常用的软件安装目录,例如笔者喜欢将其放在/opt/application下。
cp rocketmq-console-ng-1.0.0.jar /opt/application/

Step5:启动rocketmq-conolse

nohup java -jar rocketmq-console-ng-1.0.0.jar &

在浏览器中输入:http://localhost:8080查看是否安装成功,如果出现如下图则表示安装成功。

1.3 异常分析与解决思路

如果在安装过程中出现意想不到的错误,别慌,通过查看相关的日志文件,寻找错误日志,根据错误日志进行思考或百度,相信能够轻易将其解决。

例如使用的baseuser 启动的rocketmq,rocketmq-console,那相关的日志路径如下:

rocketmq:/home/baseuser/logs/rocketmqlogs/
rocketmq-console:/home/baseuser/logs/consolelogs

2. Install RocketMQ in IDEA


The most trusted development and debugging tools for the vast majority of programmers are basically DEBUG. Can you debug the source code of RocketMQ in IDEA? The answer is of course yes. This section will demonstrate how to run RocketMQ's Nameserver and Broker components in IDEA and debug them.

Setp1: Download the RocketMQ source code from github and import it into IEDA. The screenshots are as follows:

Step2:namesrv/src/main/java/org/apache/rocketmq/namesrv/NamesrvStartup Set the environment variable ROCKETMQ_HOME, the operation steps are shown in the following figure:
Set the name of the environment variable: ROCKETMQ_HOME, and its value is used to specify the home directory where RocketMQ runs. The path set by the author is: /home/dingwpmz/tmp/rocketmq.

Step3: Copy the distribution/conf/logback_namesrv.xml file to the main directory set in [Step2], and the effect after execution is shown in the following figure:

Reminder: This file is the log path of the nameserver. You can manually modify the log directory in the logback_namesrv.xml file. Since this is the basic knowledge of logback, the configuration method of logback will not be introduced in detail here.

Step4: Run NamesrvStartup in the debug method. The execution effect is shown in the figure below, indicating that the startup is successful.


Step5: Copy the distribution/conf/logback_brokerxml and broker.conf files to the main directory set in [Step2]. The effect after execution is shown in the following figure:
Step6:修改broker.conf中的配置,主要设置nameserver的地址,broker的名称等相关属性。
vi broker.conf
# 使用如下配置文件
brokerClusterName = DefaultCluster
brokerName = broker-a
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
storePathRootDir=/home/dingwpmz/tmp/rocketmq/store
storePathCommitLog=/home/dingwpmz/tmp/rocketmq/store/commitlog
namesrvAddr=127.0.0.1:9876
brokerIP1=192.168.3.10
brokerIP2=192.168.3.10
autoCreateTopicEnable=true

Step7:broker/src/main/java/org/apache/rocketmq/broker/BrokerStartup设置环境变量ROCKETMQ_HOME,操作步骤如下图所示:


Step8:以Debug模式运行BrokerStartup,其运行结果如下图所示:

看到这样的提示就表示大功告成。

接下来简单来做一个验证。

首先先在AbstractSendMessageProcessor类的parseRequestHeader方法中打上一个断点。

然后运行example中org/apache/rocketmq/example/quickstart/Producer,看是否能进入到断点中,运行结果如下图所示,已进入到Debug模式。

3、小结


本篇作为RocketMQ实战系列的第一篇文章,其目的就是构建一个研究RocketMQ的学习环境,故从两个方面进行展开:

1、在Linux环境安装RocketMQ、RocketMQ-Console。

2、在IDEA中运行RocketMQ,构建一个可以调试RocketMQ的环境。

温馨提示:搭建一个可调试的环境,但绝不是学习RocketMQ源码,就从Debug一步异步跟踪,这样会陷入其中而不可自拔,DEBUG只是一种辅助,应该用在无法理解某一端代码时,使用DEBUG,借助运行时的一些数据,使之更容易理解。


本文来自笔者的另一力作《RocketMQ实战与进阶》,专栏从使用场景入手介绍如何使用 RocketMQ,使用过程中遇到什么问题,如何解决这些问题,以及为什么可以这样解决,即原理讲解(图)穿插在实战中专栏的设计思路重在强调实战二字,旨在让一位 RocketMQ 初学者通过对本专栏的学习,快速“打怪升级”,理论与实战结合,成为该领域的佼佼者。


本文分享自微信公众号 - 中间件兴趣圈(dingwpmz_zjj)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324148304&siteId=291194637