RocketMQ安装以及遇到的坑
- 下载安装参考这个地址(很详细)
- 第一个坑:Producer /Consumer 出现No route info of this topic, TopicTestjjj
- 遇到的另外一个坑
下载安装参考这个地址(很详细) 【RocketMQ遇到的坑】大佬安装教程(包含windows版和linux版)
官方文档
第一个坑:Producer /Consumer 出现No route info of this topic, TopicTestjjj
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 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();
}
}
public class OrderedConsumer {
public static void main(String[] args) throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("TopicTest", "TagA || TagC || TagD");
consumer.registerMessageListener(new MessageListenerOrderly() {AtomicLong consumeTimes = new AtomicLong(0);
@Override
public ConsumeOrderlyStatus consumeMessage(List msgs,
ConsumeOrderlyContext context) {
context.setAutoCommit(false);
System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
this.consumeTimes.incrementAndGet();
if ((this.consumeTimes.get() % 2) == 0) {
return ConsumeOrderlyStatus.SUCCESS;
} else if ((this.consumeTimes.get() % 3) == 0) {
return ConsumeOrderlyStatus.ROLLBACK;
} else if ((this.consumeTimes.get() % 4) == 0) {
return ConsumeOrderlyStatus.COMMIT;
} else if ((this.consumeTimes.get() % 5) == 0) {
context.setSuspendCurrentQueueTimeMillis(3000);
return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.out.printf("Consumer Started.%n");
}
}
原因:producer &consumer 未设置NamesrvAddr分别添加如下代码即可
producer.setNamesrvAddr("127.0.0.1:9876");
consumer.setNamesrvAddr("127.0.0.1:9876");
遇到的另外一个坑 描述:由于客户端版本和服务端版本不一致,也会出现No route info of this topic, XXX这个报错,我的服务端版本为4.3.0,客户端pom中的jar包版本是4.2.0, 这个问题在百度csdn找了很长时间但是没有找到,终于在github 项目的Issues中找到了解决办法github issues
推荐阅读
- RocketMQ - 理论篇
- RocketMQ - 应用篇
- RocketMQ-单机版安装及远程连接测试
- RocketMQ安装及测试
- RocketMQ原理
- Kafka|Kafka VS RocketMQ VS RabbitMQ
- 【运维】【一】RocketMQ 运维命令详解
- RocketMQ
- Rocketmq创建topic报错org.apache.rocketmq.acl.common.AclException: [10015:signature-failed] unable to cal