Producer类型
Producer主要职能就是生产消息,发送消息。它可以对多个主题发送消息,甚至可以通过Tag定义些简单的过滤。更复杂的过滤可以使用filter组件来进行相应的业务操作
RocketMQ提供了3种不同模式的Producer
1.NormalProducer(普通)
2.OrderProducer(顺序)
3.TransactionProducer(事务)
Producer配置:
producer组名:多个Producer如果属于同一个应用,发送同样的消息,则应该把它们归为同一组
createTopicKey:在发送消息时,自动创建服务器不存在的topic,需要指定key
defaultTopicQueueNums :4,在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
sendMsgTimeout:10000 发送消息超时时间,单位毫秒
compressMsgBodyOverHowmuch:4096 消息Body超过多大开始压缩(Consumer收到消息会自动解压缩),单位字节
retryTimesWhenSendFailed:重试次数(可以配置)
retryAnotherBrokerWhenNotStoreOk: FALSE 如果发送消息返回sendResult,但是sendStatus!=SEND_OK,是否重试发送
maxMessageSize :131092 客户端限制的消息大小,超过报错,同时服务端也会限制(默认128K)
transactionCheckListener:事务消息回查监听器,如果发送事务消息,必须设置
checkThreadPoolMinSize:1 Broker回查Producer事务状态时,线程池大小
checkThreadPoolMaxSize : 1 Broker回查Producer事务状态时,线程池大小
checkRequestHoldMax:2000 Broker回查Producer事务状态时,Producer本地缓冲请求队列大小
普通消息:
就如前面Helloworld的示例,就是一个普通消息
顺序消息:
消息有序是指一类消息消费时,要按照一定的发送顺序进行消费,才有意义。比如:一个订单产生3条消息,分别是订单创建,订单付款,订单完成。消费时要按照这个顺序去消费才会是一个完整的有效订单。
RocketMQ采用局部顺序一致性的机制,实现了单个队列中消息的有序性,使用FIFO顺序提供有序消息。activemq支持消息有序是采用独有者机制。简而言之,我们的消息要保证有序,就必须把一组消息存放在同一个队列,然后由消息进行逐一消费。
但是如果碰到高并发的情况,消息不就会阻塞了吗?
RocketMQ给的解决方案是安装业务区划分不同的队列,然后丙型消费,提高消息的处理速度的同时避免消息堆积。
即:全局顺序时使用一个queue,局部顺序时多个queue并行消费
全局顺序消息
只使用一个queue
生产者类
package org.apache.rocketmq.example.ordermessage;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
/**
* Producer,发送顺序消息
* rocketmq的顺序消息需要满足2点
* 1.producer端保证发送消息有序,且发送到同一个队列
* 2.consumer端保证消费同一个队列
*
*/
public class Producer {
public static void main(String[] args) throws UnsupportedEncodingException {
try {
DefaultMQProducer producer = new DefaultMQProducer("producer-group-order-test");
producer.setNamesrvAddr("120.77.178.74:9876;111.230.132.162:9876");
producer.start();
String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
for (int i = 0; i < 5; i++) {
//参数分别是:主题,标签,key名,内容
Message msg =
new Message("TopicTest", tags[i % tags.length], "KEY" + i,
("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
//顺序消费的关键,必须自己实现MessageQueueSelector,重写select方法,保证消息都进入同一个队列
//该重载的send方法的三个参数分别是消息Message, 消息队列选择器MessageQueueSelector,将要进入的队列下标
SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
@Override
//select方法第一个参数是指该Topic下有的队列集合第二个是你要发送的消息,第三个是消息将要进入的队列下标,它与send方法的第三个参数相同
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
Integer id = (Integer) arg;
System.out.println("队列长度:"+mqs.size()+"队列下标:"+id);
return mqs.get(id);
}
}, 1);
System.out.printf("%s%n", sendResult);
}
producer.shutdown();
} catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
e.printStackTrace();
}
}
}
消费者类
/**
* 顺序消息消费,consumer端保证消费同一个队列
*
*/
public class Consumer {
public static void main(String[] args) throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group-order-test");
consumer.setNamesrvAddr("120.77.178.74:9876;111.230.132.162:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("TopicTest", "*");
/**
* 顺序消费的关键。与普通消费一样需要注册消息监听器。但是传入的不再是MessageListenerConcurrently
* 而是需要传入MessageListenerOrderly的子类,重写consumeMessage方法。该consumeMessage的返回值与Helloworld的返回值不一致
* 意思为顺序消费同一队列消息
*/
consumer.registerMessageListener(new MessageListenerOrderly() {
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
context.setAutoCommit(false);
for(MessageExt msg : msgs){
System.out.println("收到消息:"+msg);
System.out.println("消息内容为:"+new String(msg.getBody()));
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.out.printf("Consumer Started.%n");
}
}
消费者打印日志:
收到消息:MessageExt [queueId=1, storeSize=188, queueOffset=155, sysFlag=0, bornTimestamp=1535286033307, bornHost=/14.24.64.123:44336, storeTimestamp=1535286041572, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015048, commitLogOffset=86088, bodyCRC=613185359, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=156, KEYS=KEY0, UNIQ_KEY=C0A82B3B23E473D16E93851C6F9B0000, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 48], transactionId='null'}]
消息内容为:Hello RocketMQ 0
收到消息:MessageExt [queueId=1, storeSize=188, queueOffset=156, sysFlag=0, bornTimestamp=1535286033405, bornHost=/14.24.64.123:44336, storeTimestamp=1535286041620, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015104, commitLogOffset=86276, bodyCRC=1401636825, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=157, KEYS=KEY1, UNIQ_KEY=C0A82B3B23E473D16E93851C6FFD0001, WAIT=true, TAGS=TagB}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49], transactionId='null'}]
消息内容为:Hello RocketMQ 1
收到消息:MessageExt [queueId=1, storeSize=188, queueOffset=157, sysFlag=0, bornTimestamp=1535286033452, bornHost=/14.24.64.123:44336, storeTimestamp=1535286041670, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F00000000000151C0, commitLogOffset=86464, bodyCRC=1250039395, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=158, KEYS=KEY2, UNIQ_KEY=C0A82B3B23E473D16E93851C702C0002, WAIT=true, TAGS=TagC}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 50], transactionId='null'}]
消息内容为:Hello RocketMQ 2
收到消息:MessageExt [queueId=1, storeSize=188, queueOffset=158, sysFlag=0, bornTimestamp=1535286033505, bornHost=/14.24.64.123:44336, storeTimestamp=1535286041718, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F000000000001527C, commitLogOffset=86652, bodyCRC=1032136437, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=159, KEYS=KEY3, UNIQ_KEY=C0A82B3B23E473D16E93851C70610003, WAIT=true, TAGS=TagD}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 51], transactionId='null'}]
消息内容为:Hello RocketMQ 3
收到消息:MessageExt [queueId=1, storeSize=188, queueOffset=159, sysFlag=0, bornTimestamp=1535286033547, bornHost=/14.24.64.123:44336, storeTimestamp=1535286041759, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015338, commitLogOffset=86840, bodyCRC=601994070, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=160, KEYS=KEY4, UNIQ_KEY=C0A82B3B23E473D16E93851C708B0004, WAIT=true, TAGS=TagE}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 52], transactionId='null'}]
消息内容为:Hello RocketMQ 4
生产者打印日志:
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B23E473D16E93851C6F9B0000, offsetMsgId=784DB24A00002A9F0000000000015048, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=155]
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B23E473D16E93851C6FFD0001, offsetMsgId=784DB24A00002A9F0000000000015104, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=156]
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B23E473D16E93851C702C0002, offsetMsgId=784DB24A00002A9F00000000000151C0, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=157]
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B23E473D16E93851C70610003, offsetMsgId=784DB24A00002A9F000000000001527C, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=158]
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B23E473D16E93851C708B0004, offsetMsgId=784DB24A00002A9F0000000000015338, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=159]
局部顺序消息
多个queue并行消费
多个队列并行消费在于send方法第三个参数的传入,他决定了进入队列的下标。
在此,我更改了项目的example的ordermessage包下的示例,因为示例的消费者类做了返回消息重试的状态
示例源码:
该代码在消费者类的注册监听器的MessageListenerOrderly子类下。它根据long长度的变化返回不同的状态码,且还做了消息重试的时间间隔。
更改后的示例代码:
生产者
package org.apache.rocketmq.example.ordermessage;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
/**
* Producer,发送顺序消息
* rocketmq的顺序消息需要满足2点
* 1.producer端保证发送消息有序,且发送到同一个队列
* 2.consumer端保证消费同一个队列
*
*/
public class Producer {
public static void main(String[] args) throws UnsupportedEncodingException {
try {
DefaultMQProducer producer = new DefaultMQProducer("producer-group-order-test");
producer.setNamesrvAddr("120.77.178.74:9876;111.230.132.162:9876");
producer.start();
String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
for (int i = 0; i < 15; i++) {
int orderId = i % 5;
//参数分别是:主题,标签,key名,内容
Message msg =
new Message("TopicTest", tags[i % tags.length], "KEY" + i,
("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
//顺序消费的关键,必须自己实现MessageQueueSelector,重写select方法,保证消息都进入同一个队列
//该重载的send方法的三个参数分别是消息Message, 消息队列选择器MessageQueueSelector,队列下标
SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
@Override
//select方法第一个参数是指该Topic下有的队列集合第二个是你要发送的消息,第三个是消息将要进入的队列下标,它与send方法的第三个参数对应
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
Integer id = (Integer) arg;
System.out.println("队列长度:"+mqs.size()+"队列下标:"+id);
//int index = id % mqs.size();
return mqs.get(id);
}
}, orderId);
System.out.printf("%s%n", sendResult);
}
producer.shutdown();
} catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
e.printStackTrace();
}
}
}
请注意队列的长度。队列的长度是你在配置文件中指定的长度*你的机器个数。比如双主部署中都指定4,那么队列长度为8
select返回的是消息将要进入的队列,所以你找的队列应该得是存在的。那么要注意数组下标越界异常。如果将消息发送改为100条,上例将会出错。因为99%10等于9,下标最大7,那么自然会报异常。
int index = id % mqs.size();是示例源码提供的,它的意思是,如果将要下标异常,那么把消息投给另外一个队列,但是这样就会产生消息不一定顺序了。所以要注意队列的长度
生产者打印日志:
队列长度:8队列下标:0
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7A200000, offsetMsgId=784DB24A00002A9F0000000000015CCC, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=0], queueOffset=148]
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7A780001, offsetMsgId=784DB24A00002A9F0000000000015D88, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=163]
队列长度:8队列下标:2
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7A9F0002, offsetMsgId=784DB24A00002A9F0000000000015E44, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=2], queueOffset=79]
队列长度:8队列下标:3
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7AD60003, offsetMsgId=784DB24A00002A9F0000000000015F00, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=3], queueOffset=81]
队列长度:8队列下标:4
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7B020004, offsetMsgId=6FE684A200002A9F000000000001BFC6, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-b, queueId=0], queueOffset=163]
队列长度:8队列下标:0
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7B6A0005, offsetMsgId=784DB24A00002A9F0000000000015FBC, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=0], queueOffset=149]
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7B9A0006, offsetMsgId=784DB24A00002A9F0000000000016078, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=164]
队列长度:8队列下标:2
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7BC20007, offsetMsgId=784DB24A00002A9F0000000000016134, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=2], queueOffset=80]
队列长度:8队列下标:3
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7BF90008, offsetMsgId=784DB24A00002A9F00000000000161F0, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=3], queueOffset=82]
队列长度:8队列下标:4
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7C5D0009, offsetMsgId=6FE684A200002A9F000000000001C082, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-b, queueId=0], queueOffset=164]
队列长度:8队列下标:0
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7C82000A, offsetMsgId=784DB24A00002A9F00000000000162AC, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=0], queueOffset=150]
队列长度:8队列下标:1
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7CAD000B, offsetMsgId=784DB24A00002A9F000000000001636A, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=1], queueOffset=165]
队列长度:8队列下标:2
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7CD1000C, offsetMsgId=784DB24A00002A9F0000000000016428, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=2], queueOffset=81]
队列长度:8队列下标:3
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7D03000D, offsetMsgId=784DB24A00002A9F00000000000164E6, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-a, queueId=3], queueOffset=83]
队列长度:8队列下标:4
SendResult [sendStatus=SEND_OK, msgId=C0A82B3B19EC73D16E93852D7D2B000E, offsetMsgId=6FE684A200002A9F000000000001C13E, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-b, queueId=0], queueOffset=165]
消费者打印日志:
收到消息:MessageExt [queueId=0, storeSize=188, queueOffset=148, sysFlag=0, bornTimestamp=1535287150113, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158367, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015CCC, commitLogOffset=89292, bodyCRC=613185359, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=149, KEYS=KEY0, UNIQ_KEY=C0A82B3B19EC73D16E93852D7A200000, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 48], transactionId='null'}]
消息内容为:Hello RocketMQ 0
收到消息:MessageExt [queueId=1, storeSize=188, queueOffset=163, sysFlag=0, bornTimestamp=1535287150200, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158408, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015D88, commitLogOffset=89480, bodyCRC=1401636825, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=164, KEYS=KEY1, UNIQ_KEY=C0A82B3B19EC73D16E93852D7A780001, WAIT=true, TAGS=TagB}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49], transactionId='null'}]
消息内容为:Hello RocketMQ 1
收到消息:MessageExt [queueId=2, storeSize=188, queueOffset=79, sysFlag=0, bornTimestamp=1535287150239, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158459, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015E44, commitLogOffset=89668, bodyCRC=1250039395, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=80, KEYS=KEY2, UNIQ_KEY=C0A82B3B19EC73D16E93852D7A9F0002, WAIT=true, TAGS=TagC}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 50], transactionId='null'}]
消息内容为:Hello RocketMQ 2
收到消息:MessageExt [queueId=3, storeSize=188, queueOffset=81, sysFlag=0, bornTimestamp=1535287150294, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158509, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015F00, commitLogOffset=89856, bodyCRC=1032136437, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=82, KEYS=KEY3, UNIQ_KEY=C0A82B3B19EC73D16E93852D7AD60003, WAIT=true, TAGS=TagD}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 51], transactionId='null'}]
消息内容为:Hello RocketMQ 3
收到消息:MessageExt [queueId=0, storeSize=188, queueOffset=163, sysFlag=0, bornTimestamp=1535287150338, bornHost=/14.24.64.123:44094, storeTimestamp=1535287158599, storeHost=/111.230.132.162:10911, msgId=6FE684A200002A9F000000000001BFC6, commitLogOffset=114630, bodyCRC=601994070, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=164, KEYS=KEY4, UNIQ_KEY=C0A82B3B19EC73D16E93852D7B020004, WAIT=true, TAGS=TagE}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 52], transactionId='null'}]
消息内容为:Hello RocketMQ 4
收到消息:MessageExt [queueId=0, storeSize=188, queueOffset=149, sysFlag=0, bornTimestamp=1535287150442, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158659, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000015FBC, commitLogOffset=90044, bodyCRC=1424393152, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=150, KEYS=KEY5, UNIQ_KEY=C0A82B3B19EC73D16E93852D7B6A0005, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 53], transactionId='null'}]
消息内容为:Hello RocketMQ 5
收到消息:MessageExt [queueId=1, storeSize=188, queueOffset=164, sysFlag=0, bornTimestamp=1535287150490, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158699, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000016078, commitLogOffset=90232, bodyCRC=1307562618, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=165, KEYS=KEY6, UNIQ_KEY=C0A82B3B19EC73D16E93852D7B9A0006, WAIT=true, TAGS=TagB}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 54], transactionId='null'}]
消息内容为:Hello RocketMQ 6
收到消息:MessageExt [queueId=2, storeSize=188, queueOffset=80, sysFlag=0, bornTimestamp=1535287150530, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158750, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000016134, commitLogOffset=90420, bodyCRC=988340972, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=81, KEYS=KEY7, UNIQ_KEY=C0A82B3B19EC73D16E93852D7BC20007, WAIT=true, TAGS=TagC}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 55], transactionId='null'}]
消息内容为:Hello RocketMQ 7
收到消息:MessageExt [queueId=3, storeSize=188, queueOffset=82, sysFlag=0, bornTimestamp=1535287150585, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158854, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F00000000000161F0, commitLogOffset=90608, bodyCRC=710410109, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=83, KEYS=KEY8, UNIQ_KEY=C0A82B3B19EC73D16E93852D7BF90008, WAIT=true, TAGS=TagD}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 56], transactionId='null'}]
消息内容为:Hello RocketMQ 8
收到消息:MessageExt [queueId=0, storeSize=188, queueOffset=164, sysFlag=0, bornTimestamp=1535287150685, bornHost=/14.24.64.123:44094, storeTimestamp=1535287158882, storeHost=/111.230.132.162:10911, msgId=6FE684A200002A9F000000000001C082, commitLogOffset=114818, bodyCRC=1565577195, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=165, KEYS=KEY9, UNIQ_KEY=C0A82B3B19EC73D16E93852D7C5D0009, WAIT=true, TAGS=TagE}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 57], transactionId='null'}]
消息内容为:Hello RocketMQ 9
收到消息:MessageExt [queueId=0, storeSize=190, queueOffset=150, sysFlag=0, bornTimestamp=1535287150722, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158934, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F00000000000162AC, commitLogOffset=90796, bodyCRC=193412630, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=151, KEYS=KEY10, UNIQ_KEY=C0A82B3B19EC73D16E93852D7C82000A, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49, 48], transactionId='null'}]
消息内容为:Hello RocketMQ 10
收到消息:MessageExt [queueId=1, storeSize=190, queueOffset=165, sysFlag=0, bornTimestamp=1535287150765, bornHost=/14.24.64.123:44429, storeTimestamp=1535287158969, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F000000000001636A, commitLogOffset=90986, bodyCRC=2088767104, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=166, KEYS=KEY11, UNIQ_KEY=C0A82B3B19EC73D16E93852D7CAD000B, WAIT=true, TAGS=TagB}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49, 49], transactionId='null'}]
消息内容为:Hello RocketMQ 11
收到消息:MessageExt [queueId=2, storeSize=190, queueOffset=81, sysFlag=0, bornTimestamp=1535287150801, bornHost=/14.24.64.123:44429, storeTimestamp=1535287159020, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F0000000000016428, commitLogOffset=91176, bodyCRC=1703501626, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=82, KEYS=KEY12, UNIQ_KEY=C0A82B3B19EC73D16E93852D7CD1000C, WAIT=true, TAGS=TagC}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49, 50], transactionId='null'}]
消息内容为:Hello RocketMQ 12
收到消息:MessageExt [queueId=3, storeSize=190, queueOffset=83, sysFlag=0, bornTimestamp=1535287150851, bornHost=/14.24.64.123:44429, storeTimestamp=1535287159060, storeHost=/120.77.178.74:10911, msgId=784DB24A00002A9F00000000000164E6, commitLogOffset=91366, bodyCRC=311324588, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=84, KEYS=KEY13, UNIQ_KEY=C0A82B3B19EC73D16E93852D7D03000D, WAIT=true, TAGS=TagD}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49, 51], transactionId='null'}]
消息内容为:Hello RocketMQ 13
收到消息:MessageExt [queueId=0, storeSize=190, queueOffset=165, sysFlag=0, bornTimestamp=1535287150891, bornHost=/14.24.64.123:44094, storeTimestamp=1535287159092, storeHost=/111.230.132.162:10911, msgId=6FE684A200002A9F000000000001C13E, commitLogOffset=115006, bodyCRC=216726031, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=166, KEYS=KEY14, UNIQ_KEY=C0A82B3B19EC73D16E93852D7D2B000E, WAIT=true, TAGS=TagE}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49, 52], transactionId='null'}]
消息内容为:Hello RocketMQ 14
上面的例子意思是:一共发15条消息,第一条发给队列下标0,第二条发给队列下标1,第三条发给队列下标2,第四条发给队列下标3,第五条发给队列下标4,第六条发给队列下标0。如此往复。
由于消费者是同时监听多个队列进行消费的,所以有时候在同一个主题同一个标签进行消息的传递,你会发现上一条还在消费队列下标为0的,下一条就消费下标4的。但只要在同一个队列中,其消息的消费顺序是有序就是,因为是串行消费,所以出现该情况很正常,这就是局部多个队列并行消费