1、项目总体结构,如下图所示
package com.yj.mq.config;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RocketMqConfig {
@Value("${rocketmq.producer.groupName}")
private String producerGroupName;
@Value("${rocketmq.consumer.groupName}")
private String consumerGroupName;
@Value("${rocketmq.consumer.order.groupName}")
private String consumerOrderGroupName;
@Value("${rocketmq.namesrv.addr}")
private String nameServerAddress;
@Value("${rocketmq.topic}")
private String topic;
@Value("${rocketmq.order.topic}")
private String orderTopic;
@Value("${rocketmq.sendMsg.timeOut:30000}")
private Integer sendMsgTimeOut;
@Value("${rocketmq.retry.times:3}")
private Integer retryTimes;
@Autowired
private MessageListenerConcurrently messageListener;
@Autowired
private MessageListenerOrderly orderMessageListener;
@Bean(initMethod = "start", destroyMethod = "shutdown")
public DefaultMQProducer producer() {
DefaultMQProducer producer = new DefaultMQProducer(producerGroupName);
//VipChannel阿里内部使用版本才用,开源版本没有,默认为true,占用10909端口,此时虚拟机需要开放10909端口,否则会报 :connect to <:10909> failed异常,可以直接设置为false
//producer.setVipChannelEnabled(false);
producer.setNamesrvAddr(nameServerAddress);
producer.setSendMsgTimeout(sendMsgTimeOut);
producer.setRetryTimesWhenSendFailed(retryTimes);
return producer;
}
@Bean(initMethod = "start", destroyMethod = "shutdown")
public DefaultMQPushConsumer consumer() {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroupName);
consumer.setNamesrvAddr(nameServerAddress);
consumer.setMaxReconsumeTimes(0);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.registerMessageListener(messageListener);
//consumer.setMessageModel(MessageModel.CLUSTERING);集群模式(默认)
//consumer.setMessageModel(MessageModel.BROADCASTING);//广播模式
try {
consumer.subscribe(topic, "*");
} catch (MQClientException e) {
e.printStackTrace();
}
return consumer;
}
@Bean(initMethod = "start", destroyMethod = "shutdown")
public DefaultMQPushConsumer orderConsumer() {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerOrderGroupName);
consumer.setNamesrvAddr(nameServerAddress);
consumer.setMaxReconsumeTimes(0);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
//consumer.setMessageModel(MessageModel.CLUSTERING);集群模式(默认)
//consumer.setMessageModel(MessageModel.BROADCASTING);//广播模式
consumer.registerMessageListener(orderMessageListener);
try {
consumer.subscribe(orderTopic, "*");
} catch (MQClientException e) {
e.printStackTrace();
}
return consumer;
}
}
2.MessageProcessor文件:
package com.yj.mq.consumer;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class MessageProcessor {
@Value("${server.port}")
private String port;
private static final Logger LOGGER = LoggerFactory.getLogger(MessageProcessor.class);
public Boolean handleMessage(MessageExt messageExt) {
try {
String body = null;
// 消费者消费
if (null == messageExt || null == messageExt.getBody()) {
LOGGER.info("消息体为空");
return false;
}
body = new String(messageExt.getBody());
LOGGER.info("port:"+port+",消费Tag:" + messageExt.getTags() + ",body:" + body);
//Order order = JSON.parseObject(body, Order.class);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error(e.getMessage(), e);
return false;
}
return true;
}
}
3.OrderMessageProcessor文件:
package com.yj.mq.consumer;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class OrderMessageProcessor {
@Value("${server.port}")
private String port;
private static final Logger LOGGER = LoggerFactory.getLogger(OrderMessageProcessor.class);
public Boolean handleMessage(MessageExt messageExt) {
try {
String body = null;
// 消费者消费
if (null == messageExt || null == messageExt.getBody()) {
LOGGER.info("消息体为空");
return false;
}
body = new String(messageExt.getBody());
LOGGER.info("port:"+port+",消费Tag:" + messageExt.getTags() + ",body:" + body);
//Order order = JSON.parseObject(body, Order.class);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error(e.getMessage(), e);
return false;
}
return true;
}
}
4.MQController文件:
package com.yj.mq.controller;
import java.util.ArrayList;
import java.util.List;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.yj.mq.entity.BusinessException;
import com.yj.mq.entity.Order;
import com.yj.mq.producer.MQProducer;
@RestController
public class MQController {
private static final Logger LOGGER = LoggerFactory.getLogger(MQController.class);
@Autowired
private MQProducer producer;
@Value("${rocketmq.topic}")
private String topic;
@Value("${rocketmq.order.topic}")
private String orderTopic;
@RequestMapping("/send")
public SendResult send(@RequestBody String desc) {
SendResult result = null;
try {
Order order = new Order();
order.setOrderId(1L);
order.setDesc(desc);
String userStr = JSON.toJSONString(order);
Message message = new Message();
message.setTopic(topic);
message.setBody(userStr.getBytes());
message.setTags(desc + "_tag");
result = producer.syncSend(message);
LOGGER.info("SendResult:" + result);
} catch (BusinessException e) {
LOGGER.info("e:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@RequestMapping("/sendlist")
public SendResult sendlist(@RequestBody String name) {
SendResult result = null;
try {
List orderList = buildOrders();
for (int i = 0; i < orderList.size(); i++) {
String body = JSON.toJSONString(orderList.get(i));
Message msg = new Message(topic, body.getBytes());
result = producer.syncSend(msg);
LOGGER.info("SendResult:" + result);
}
} catch (BusinessException e) {
LOGGER.info("e:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 发送有序消息
@RequestMapping("/sendOrderlist")
public SendResult sendOrderlist(@RequestBody String name) {
SendResult result = null;
try {
List orderList = buildOrders();
for (int i = 0; i < orderList.size(); i++) {
String body = JSON.toJSONString(orderList.get(i));
Message msg = new Message(orderTopic, body.getBytes());
result = producer.sendOrder(msg, orderList.get(i).getOrderId());
LOGGER.info("SendResult:" + result);
}
} catch (BusinessException e) {
LOGGER.info("e:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private List buildOrders() {
List orderList = new ArrayList();
for (int i = 0; i < 20; i++) {
Order orderDemo = new Order();
orderDemo.setOrderId(new Long((long) i));
orderDemo.setDesc("创建订单");
orderList.add(orderDemo);
orderDemo = new Order();
orderDemo.setOrderId(new Long((long) i));
orderDemo.setDesc("订单付款");
orderList.add(orderDemo);
orderDemo = new Order();
orderDemo.setOrderId(new Long((long) i));
orderDemo.setDesc("付款成功");
orderList.add(orderDemo);
orderDemo = new Order();
orderDemo.setOrderId(new Long((long) i));
orderDemo.setDesc("订单完成");
orderList.add(orderDemo);
}
return orderList;
}
}
5.BusinessException文件:
package com.yj.mq.entity;
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = -8138602623241348983L;
private String errorMessage = null;
public BusinessException() {
super();
}
public BusinessException(String errorMessage) {
super(errorMessage);
this.errorMessage = errorMessage;
}
public String getMessage() {
if (errorMessage != null) {
return errorMessage;
}
if (super.getMessage() != null)
return super.getMessage();
return errorMessage;
}
}
6.Order文件:
package com.yj.mq.entity;
import java.io.Serializable;
public class Order implements Serializable{
private static final long serialVersionUID = -2203625690315456938L;
private Long orderId;
private String desc;
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Order [orderId=" + orderId + ", desc=" + desc + "]";
}
}
7.MQListener文件:
package com.yj.mq.listener;
import java.util.List;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yj.mq.consumer.MessageProcessor;
@Component
public class MessageListener implements MessageListenerConcurrently {
@Autowired
private MessageProcessor messageProcessor;
@Override
public ConsumeConcurrentlyStatus consumeMessage(List list, ConsumeConcurrentlyContext context) {
for (MessageExt messageExt : list) {
boolean result = messageProcessor.handleMessage(messageExt);
if (!result) {
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
}
8.OrderMessageListener文件:
package com.yj.mq.listener;
import java.util.List;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yj.mq.consumer.MessageProcessor;
@Component
public class OrderMessageListener implements MessageListenerOrderly{
@Autowired
private MessageProcessor messageProcessor;
@Override
public ConsumeOrderlyStatus consumeMessage(List msgs, ConsumeOrderlyContext context) {//有序监听
context.setAutoCommit(true);
for (MessageExt messageExt: msgs) {
boolean result = messageProcessor.handleMessage(messageExt);
if (!result) {
return ConsumeOrderlyStatus.SUCCESS;
}
}
return ConsumeOrderlyStatus.SUCCESS;
}
}
9.MQProducer文件:
package com.yj.mq.producer;
import java.util.List;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yj.mq.entity.BusinessException;
@Component
public class MQProducer {
private static final Logger logger = LoggerFactory.getLogger(MQProducer.class);
@Autowired
private DefaultMQProducer producer;
public SendResult syncSend(Message message) throws BusinessException {
SendResult result = null;
try {
result = producer.send(message);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException("发送同步消息发生异常");
}
return result;
}
public void asyncSend(Message message) throws BusinessException {
try {
producer.send(message, new SendCallback() {
@Override
public void onSuccess(SendResult result) {
logger.info("消息发布成功,messageId:" + result.getMsgId());
}
@Override
public void onException(Throwable throwable) {
logger.error("消息发布失败:" + throwable.getMessage(), throwable);
}
});
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException("发送异步消息发生异常");
}
}
public SendResult sendOrder(Message message, Long orderId) throws BusinessException {
SendResult result = null;
try {
result = producer.send(message, new MessageQueueSelector() {
@Override
public MessageQueue select(List mqs, Message msg, Object arg) {
Long id = (Long) arg;
long index = id % mqs.size();
return mqs.get((int) index);
}
}, orderId);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException("发送有序消息发生异常");
}
return result;
}
}
10.RocketMqApplication文件:
package com.yj.mq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RocketMqApplication {
public static void main(String[] args) {
SpringApplication.run(RocketMqApplication.class, args);
}
}
11.application.properties文件:
server.port=9001
## rocketmq
rocketmq.namesrv.addr=192.168.81.130:9876
rocketmq.producer.groupName=PRODUCER_GROUP_CSDN
rocketmq.consumer.groupName=CONSUMER_GROUP_CSDN
rocketmq.consumer.order.groupName=CONSUMER_ORDER_GROUP_CSDN
rocketmq.topic=CSDN
rocketmq.order.topic=CSDN_ORDER
12.pom.xml文件:
4.0.0
com.yj
RocketMq
0.0.1-SNAPSHOT
jar
RocketMq
http://maven.apache.org
UTF-8
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.apache.rocketmq
rocketmq-client
4.2.0
com.google.code.gson
gson
2、验证
①访问:
http://127.0.0.1:9001/send
出现结果:
②访问:
http://127.0.0.1:9001/sendlist
出现结果:
不同订单号之间会出现乱序,相同的订单号内的订单,也发现有乱序消费的情况。
③访问:
http://127.0.0.1:9001/sendOrderlist
出现结果:
不同订单号之间会出现乱序,但是相同的订单号内的订单,未发现有乱序消费的情况,都是顺序消费的。