rocketmq 源码调试报错 No route info of this topic

本文基于 v3.2.6版本代码分析

rocketmq 源码调试报错 No route info of this topic_第1张图片

直接用main方法启动了nameserver和broker

com.alibaba.rocketmq.namesrv.NamesrvStartup
com.alibaba.rocketmq.broker.BrokerStartup

进入bin目录下用mqadmin.exe创建了一个topic并且成功了

然后写了一个简单的producer

package com.alibaba.rocketmq.client;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;

public class Producer {

	public static void main(String[] args) throws Exception{
		DefaultMQProducer producer = new DefaultMQProducer("groupa");
		producer.setNamesrvAddr("localhost:9876");
		producer.start();
		
		Message msg = new Message("mytopic", ("Hello RocketMQ ").getBytes());
		msg.setKeys("mykey");
		msg.setTags("mytag");
        SendResult sendResult = producer.send(msg);
        System.out.println(sendResult);
        
        producer.shutdown();
	}
}

但是始终报错:

rocketmq 源码调试报错 No route info of this topic_第2张图片

 

最后查了很多资料,也跟了源码(网上很多说是设置 autoCreateTopic,其实是错的,因为broker默认就是true)

最终发现是broker启动的方式有问题,启动的时候必须指明nameserver的地址

com.alibaba.rocketmq.broker.BrokerStartup.createBrokerController(String[])

稍微加了一句代码后ok了

rocketmq 源码调试报错 No route info of this topic_第3张图片

或者是设置一个 NAMESRV_ADDR 的环境变量,因为broker启动的时候会去获取这个环境变量

 

结果:

 

你可能感兴趣的:(RocketMq)