RocketMQ 源码阅读 -- 环境搭建
程序员文章站
2022-07-14 23:40:43
...
一、 NameServer 和 Broker 启动
根据官网的 quik start 指引,我们可以看到在 distribution/bin 目录下的 mqnamesrv 和 mqbroker 是启动的关键。
例如 mqnamesrv
#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [ -z "$ROCKETMQ_HOME" ] ; then
## resolve links - $0 may be a link to maven's home
PRG="$0"
# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done
saveddir=`pwd`
ROCKETMQ_HOME=`dirname "$PRG"`/..
# make it fully qualified
ROCKETMQ_HOME=`cd "$ROCKETMQ_HOME" && pwd`
cd "$saveddir"
fi
export ROCKETMQ_HOME
sh ${ROCKETMQ_HOME}/bin/runserver.sh org.apache.rocketmq.namesrv.NamesrvStartup aaa@qq.com
看到最后一行,org.apache.rocketmq.namesrv.NamesrvStartup 是 Namesrv 的启动类,但是直接 run main 方法启动不了,需要设置 ROCKETMQ_HOME 的环境变量。Windows 10 设置环境变量无效,于是乎把在代码中直接写死环境变量
然后就可以 run 这个 main 方法了。 broker 启动类似。
二、Producer 和 Consumer 启动
直接使用官方提供的样例启动, org.apache.rocketmq.example.quickstart.Producer,只要配置一下 NameServer 地址,让 producer 可以注册到 NameServer 就可以直接启动了
Producer {
public static void main(String[] args) throws MQClientException, InterruptedException {
/*
* Instantiate with a producer group name.
*/
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
// 指定 NameServer 地址
producer.setNamesrvAddr("127.0.0.1:9876");
Consumer 启动类似