一、RockMQ简介
二、下载RocketMQ
官网下载地址:http://rocketmq.apache.org/dowloading/releases/
选择合适的版本下载即可,小笔这里选的是4.2.0版。值得注意的是,官网提供了源码包和运行包两种,这里看个人需求,选择相应的包,我比较懒,直接选可运行包 rocketmq-all-4.2.0-bin-release.zip
下载完成之后,目录结构大概是这样的。
三、配置环境变量
在启动前,我们要先配置RocketMQ的环境变量。
新增一个系统变量ROCKETMQ_HOME
变量名:ROCKETMQ_HOME
变量值:MQ解压路径\MQ文件夹名
四、启动RocketMQ
启动NAMESERVER
cmd命令框进入到 RocketMQ的安装路径的bin目录下,执行:
start mqnamesrv.cmd
或者直接双击 mqnamesrv.cmd 文件,启动成功会弹出提示框
启动BROKER
cmd命令框进入到 RocketMQ的安装路径的bin目录下,执行:
start mqbroker.cmd -n localhost:9876
启动成功会弹出提示框
假如弹出提示框提示 “错误: 找不到或无法加载主类 xxxxxx”。编辑 runbroker.cmd 文件,然后将 %CLASSPATH% 加上英文双引号。保存并重新执行start语句
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.
if not exist "%JAVA_HOME%\bin\java.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! & EXIT /B 1
set "JAVA=%JAVA_HOME%\bin\java.exe"
setlocal
set BASE_DIR=%~dp0
set BASE_DIR=%BASE_DIR:~0,-1%
for %%d in (%BASE_DIR%) do set BASE_DIR=%%~dpd
set CLASSPATH=.;%BASE_DIR%conf;%CLASSPATH%
rem ===========================================================================================
rem JVM Configuration
rem ===========================================================================================
set "JAVA_OPT=%JAVA_OPT% -server -Xms2g -Xmx2g -Xmn1g"
set "JAVA_OPT=%JAVA_OPT% -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8"
set "JAVA_OPT=%JAVA_OPT% -verbose:gc -Xloggc:%USERPROFILE%\mq_gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
set "JAVA_OPT=%JAVA_OPT% -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
set "JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow"
set "JAVA_OPT=%JAVA_OPT% -XX:+AlwaysPreTouch"
set "JAVA_OPT=%JAVA_OPT% -XX:MaxDirectMemorySize=15g"
set "JAVA_OPT=%JAVA_OPT% -XX:-UseLargePages -XX:-UseBiasedLocking"
set "JAVA_OPT=%JAVA_OPT% -Djava.ext.dirs=%BASE_DIR%lib"
set "JAVA_OPT=%JAVA_OPT% -cp "%CLASSPATH%""
"%JAVA%" %JAVA_OPT% %*
五、下载安装RocketMQ控制台
Github下载地址:git clone https://github.com/apache/rocketmq-externals.git
直接下载:https://github.com/apache/rocketmq-externals/archive/master.zip
这里我们只需要用到 rocketmq-console 这个项目
下载完成之后,需要修改 rocketmq-externals\rocketmq-console\src\main\resources 目录下的 application.properties
主要是修改 rocketmq.config.namesrvAddr 的值,即 rocketmq.config.namesrvAddr=localhost:9876
端口也可以自行修改,这里我改成了8070
server.contextPath=
server.port=8070
### SSL setting
#server.ssl.key-store=classpath:rmqcngkeystore.jks
#server.ssl.key-store-password=rocketmq
#server.ssl.keyStoreType=PKCS12
#server.ssl.keyAlias=rmqcngkey
#spring.application.index=true
spring.application.name=rocketmq-console
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
logging.config=classpath:logback.xml
#if this value is empty,use env value rocketmq.config.namesrvAddr NAMESRV_ADDR | now, you can set it in ops page.default localhost:9876
rocketmq.config.namesrvAddr=localhost:9876
#if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
rocketmq.config.isVIPChannel=
#rocketmq-console's data path:dashboard/monitor
rocketmq.config.dataPath=/tmp/rocketmq-console/data
#set it false if you don't want use dashboard.default true
rocketmq.config.enableDashBoardCollect=true
#set the message track trace topic if you don't want use the default one
rocketmq.config.msgTrackTopicName=
rocketmq.config.ticketKey=ticket
#Must create userInfo file: ${rocketmq.config.dataPath}/users.properties if the login is required
rocketmq.config.loginRequired=false
修改 rocketmq-console 的pom文件
这个里根据个人情况而定, rocketmq-console 的rocketmq版本默认是4.4.0,把版本换成自己下载的rocketmq版本,前面我们下载的是4.2.0,但是笔者这里遇到一个小坑,就是无论如何4.2.0的依赖就是下载不下来,索性就直接使用4.4.0版,因为版本也是向下兼容,如果下下载的版本号是高于4.4.0的,最好将其替换
4.4.0
如果上一步我们修改rocketmq版本之后,org.apache.rocketmq.console.service.impl.MessageServiceImpl 类的 queryMessageByTopic 这个方法可能会报错,需要修改。
将
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, null);
替换为
RPCHook rpcHook = null;
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, rpcHook);
上述完成之后,开始打包运行
#切换到代码根目录
cd rocketmq-externals
# 切换到控制台目录
cd rocketmq-console
# 构建
mvn clean package -DskipTests
笔者是直接丢到IDEA里去了,所以直接运行项目的,遇到打包出问题的盆友也可以参考
六、启动RocketMQ控制台
最后就是启动 rocketmq-console
通过打包方式的同学,先切换到打包目录,运行
java -jar rocketmq-console-ng-1.0.1.jar
丢IDEA里的同学则直接正常启动项目就行,启动成功之后,浏览器访问:http://localhost:8070
即可看到如下页面