RocketMQ 官网提供的顺序消息 demo 运行报错:No route info for this topic, TopicTestjjj ?

官网完整的顺序消息代码 demo,未做改动,跑起来报错,信息如下:

Exception in thread "main" org.apache.rocketmq.client.exception.MQClientException: No route info for this topic, TopicTestjjj
For more information, please visit the url, http://rocketmq.apache.org/docs/faq/
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendSelectImpl(DefaultMQProducerImpl.java:1031)
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:997)
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:992)
	at org.apache.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:389)
	at site.exception.springbootsimplerocketmqproducer.producer.OrderedProducer.main(OrderedProducer.java:30)

官方 demo 如下:

public class OrderedProducer {
	public static void main(String[] args) throws Exception {
		//Instantiate with a producer group name.
		MQProducer producer = new DefaultMQProducer("example_group_name");
		//Launch the instance.
		producer.start();
		String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
		for (int i = 0; i < 100; i++) {
			int orderId = i % 10;
			//Create a message instance, specifying topic, tag and message body.
			Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i,
					("Hello RocketMQ" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
			SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
			@Override
			public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
				Integer id = (Integer) arg;
				int index = id % mqs.size();
				return mqs.get(index);
			}
			}, orderId);

			System.out.printf("%s%n", sendResult);
		}
		//server shutdown
		producer.shutdown();}
}

1 个解决方案

AllenJiang
非著名 Java 工程师,欢迎关注微信公众号 : Java技术说

几番排查后,发现一个很坑的问题,producer 并未调用 setNamesrvAddr() 方法来指定 Name Server 的 ip 和 端口,当然找不到 topic 相关的路由信息了,有点坑,RocketMQ 文档维护人员都不自己跑一下 demo 的嘛?

解决方案:

需要更改代码如下,显示去指定一下 Name Server 的地址:

//Instantiate with a producer group name.
DefaultMQProducer producer = new DefaultMQProducer("my-group1");
producer.setNamesrvAddr("localhost:9876");