01-rocketmq环境搭建

2022-07-18

01-rocketmq环境搭建

rocketmq环境搭建

一. 本机源码环境

  1. 从git上拉取源码
  2. 启动namesrv

​ 在NamesrvStartup.java中启动

  1. 启动broker

    有如下的注意事项:

    从源码中可以看到这样的语句

    String namesrvAddr = brokerConfig.getNamesrvAddr();
    private String namesrvAddr = System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY, System.getenv(MixAll.NAMESRV_ADDR_ENV));
    

    因此需要在启动时,携带环境变量rocketmq.namesrv.addr或者NAMESRV_ADDR

    在idea启动项中添加即可

  2. 运行测试producer和consumer即可

    注意设置namesrv的地址即可

二. Broker单机集群启动

以本机mac搭建为例:

本机是通过rocket源码启动的,步骤如下:

1. 配置ROCKETMQ_HOME

从源码中可以看到,ROCKETMQ_HOME是很重要的一项配置,没有则不会启动

if (null == brokerConfig.getRocketmqHome()) {
  System.out.printf("Please set the %s variable in your environment to match the location of the RocketMQ installation", MixAll.ROCKETMQ_HOME_ENV);
  System.exit(-2);
}


private String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));

//这两个有一个即可
public static final String ROCKETMQ_HOME_ENV = "ROCKETMQ_HOME";
public static final String ROCKETMQ_HOME_PROPERTY = "rocketmq.home.dir";

除了可以在环境变量中配置ROCKETMQ_HOME外,也可以将ROCKET_HOME写入配置文件中,利用启动项参数-c去解析配置文件rocketmqHome=/User/mahaonan

其实所有关于BrokerConfig这个类中的参数都可以在配置文件中指定,然后启动项-c fileName读取配置文件,就可以达到配置Broker的目的,并不一定局限于其他方法。因为Broker在启动时,会判断是否有-c参数,然后去读取配置文件信息

if (commandLine.hasOption('c')) {
  String file = commandLine.getOptionValue('c');
  if (file != null) {
    configFile = file;
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    properties = new Properties();
    properties.load(in);

    properties2SystemEnv(properties);
    MixAll.properties2Object(properties, brokerConfig);
    MixAll.properties2Object(properties, nettyServerConfig);
    MixAll.properties2Object(properties, nettyClientConfig);
    MixAll.properties2Object(properties, messageStoreConfig);

    BrokerPathConfigHelper.setBrokerConfigPath(file);
    in.close();
  }
}

从源码中可以看到很多有意思的设计

我本机的ROCKETMQ_HOME = /Users/mahaonan/mhn/my_file/下载/rocketmq-all-4.7.1-source-release/distribution

2. 修改配置文件

这里,简单起见,我搭建了一个1主1从的broker环境,因此就需要配置两个broker的配置文件

broker-a.propertiesbroker-a-s.properties

其中master配置文件如下:

brokerClusterName=DefaultCluster
#禁止自动创建topic,生产环境一般需要,否则会影响topic分布
autoCreateTopicEnable=false
brokerName=broker-a
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
storePathRootDir=/Users/mahaonan/mhn/test/rocketmq/store/master_a
storePathCommitLog= /Users/mahaonan/mhn/test/rocketmq/store/master_a/commitlog
storePathConsumeQueue=/Users/mahaonan/mhn/test/rocketmq/store/master_a/consumequeue
storePathIndex=/Users/mahaonan/mhn/test/rocketmq/store/master_a/index
listenPort=10921

slave配置文件如下:

brokerClusterName=DefaultCluster
brokerName=broker-a
autoCreateTopicEnable=false
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
storePathRootDir=/Users/mahaonan/mhn/test/rocketmq/store/slave-a
storePathCommitLog= /Users/mahaonan/mhn/test/rocketmq/store/slave-a/commitlog
storePathConsumeQueue=/Users/mahaonan/mhn/test/rocketmq/store/slave-a/consumequeue
storePathIndex=/Users/mahaonan/mhn/test/rocketmq/store/slave-a/index
listenPort=10911

注意事项:

  1. brokerName一致,表明这是一个主从架构下的broker
  2. storePathRootDir,storePathCommitLog,storePathConsumeQueue,storePathIndex这四个必须不一样,不然会报错,代表broker存储相关的位置
  3. listenPort必须相差2以上,代表broker监听的端口
  4. 从broker的brokerRole=SLAVE,brokerId大于0
  5. 主broker的brokerRole=SYNC_MASTER,brokerId=0

3. 启动Broker

在idea中,添加启动项参数,首先启动master,使用的配置文件为broker-a.properties

The broker[broker-a, 192.168.1.149:10921] boot success. serializeType=JSON and name server is localhost:9876

然后启动slave,使用的配置文件为broker-a-s.properties

启动完成后,在rocketmq-consle可以看到broker启动情况如图:


标题:01-rocketmq环境搭建
作者:mahaonan
地址:https://mahaonan.fun/articles/2022/07/18/1658147022333.html