新增rocketmq-demo演示工程
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
package com.rocketmq;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* Rocketmq事务 demo
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@EnableTransactionManagement
|
||||
@SpringBootApplication
|
||||
public class RocketMqTxApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RocketMqTxApplication.class, args);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.rocketmq.demo.config;
|
||||
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
import org.springframework.cloud.stream.messaging.Source;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
import com.rocketmq.demo.config.RocketMqConfig.MySink;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@EnableBinding({ Source.class, MySink.class })
|
||||
public class RocketMqConfig {
|
||||
public interface MySink {
|
||||
@Input(Sink.INPUT)
|
||||
SubscribableChannel input();
|
||||
|
||||
@Input("inputDlq")
|
||||
SubscribableChannel inputDlq();
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.rocketmq.demo.controller;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.central.common.utils.IdGenerator;
|
||||
import com.rocketmq.demo.model.Order;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.stream.messaging.Source;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@RestController
|
||||
public class OrderController {
|
||||
private final Source source;
|
||||
|
||||
@Autowired
|
||||
public OrderController(Source source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
/**
|
||||
* 正常情况
|
||||
*/
|
||||
@GetMapping("/success")
|
||||
public String success() {
|
||||
Order order = new Order();
|
||||
order.setOrderId(IdGenerator.getId());
|
||||
order.setOrderNo(RandomUtil.randomString(4));
|
||||
|
||||
Message message = MessageBuilder
|
||||
.withPayload(order)
|
||||
.setHeader("orderId", order.getOrderId())
|
||||
.build();
|
||||
//发送半消息
|
||||
source.output().send(message);
|
||||
return "下单成功";
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息失败
|
||||
*/
|
||||
@GetMapping("/produceError")
|
||||
public String produceError() {
|
||||
Order order = new Order();
|
||||
order.setOrderId(IdGenerator.getId());
|
||||
order.setOrderNo(RandomUtil.randomString(4));
|
||||
|
||||
Message message = MessageBuilder
|
||||
.withPayload(order)
|
||||
.setHeader("orderId", order.getOrderId())
|
||||
.setHeader("produceError", "1")
|
||||
.build();
|
||||
//发送半消息
|
||||
source.output().send(message);
|
||||
return "发送消息失败";
|
||||
}
|
||||
|
||||
/**
|
||||
* 消费消息失败
|
||||
*/
|
||||
@GetMapping("/consumeError")
|
||||
public String consumeError() {
|
||||
Order order = new Order();
|
||||
order.setOrderId(IdGenerator.getId());
|
||||
order.setOrderNo(RandomUtil.randomString(4));
|
||||
|
||||
Message message = MessageBuilder
|
||||
.withPayload(order)
|
||||
.setHeader("orderId", order.getOrderId())
|
||||
.setHeader("consumeError", "1")
|
||||
.build();
|
||||
//发送半消息
|
||||
source.output().send(message);
|
||||
return "消费消息失败";
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.rocketmq.demo.listener;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.rocketmq.demo.model.Order;
|
||||
import com.rocketmq.demo.service.IOrderService;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@RocketMQTransactionListener(txProducerGroup = "order-tx-produce-group", corePoolSize = 5, maximumPoolSize = 10)
|
||||
public class OrderTransactionListenerImpl implements RocketMQLocalTransactionListener {
|
||||
@Autowired
|
||||
private IOrderService orderService;
|
||||
|
||||
/**
|
||||
* 提交本地事务
|
||||
*/
|
||||
@Override
|
||||
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object arg) {
|
||||
//插入订单数据
|
||||
String orderJson = new String(((byte[])message.getPayload()));
|
||||
Order order = JSON.parseObject(orderJson, Order.class);
|
||||
orderService.save(order);
|
||||
|
||||
String produceError = (String)message.getHeaders().get("produceError");
|
||||
if ("1".equals(produceError)) {
|
||||
System.err.println("============Exception:订单进程挂了,事务消息没提交");
|
||||
//模拟插入订单后服务器挂了,没有commit事务消息
|
||||
throw new RuntimeException("============订单服务器挂了");
|
||||
}
|
||||
|
||||
//提交事务消息
|
||||
return RocketMQLocalTransactionState.COMMIT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 事务回查接口
|
||||
*
|
||||
* 如果事务消息一直没提交,则定时判断订单数据是否已经插入
|
||||
* 是:提交事务消息
|
||||
* 否:回滚事务消息
|
||||
*/
|
||||
@Override
|
||||
public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
|
||||
String orderId = (String)message.getHeaders().get("orderId");
|
||||
System.out.println("============事务回查-orderId:" + orderId);
|
||||
//判断之前的事务是否已经提交:订单记录是否已经保存
|
||||
int count = 1;
|
||||
//select count(1) from t_order where order_id = ${orderId}
|
||||
System.out.println("============事务回查-订单已生成-提交事务消息");
|
||||
return count > 0 ? RocketMQLocalTransactionState.COMMIT : RocketMQLocalTransactionState.ROLLBACK;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.rocketmq.demo.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 订单实体
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class Order implements Serializable {
|
||||
private static final long serialVersionUID = 2801814838883246461L;
|
||||
|
||||
private Long orderId;
|
||||
private String orderNo;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.rocketmq.demo.service;
|
||||
|
||||
import com.rocketmq.demo.model.Order;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
public interface IOrderService {
|
||||
void save(Order order);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.rocketmq.demo.service.impl;
|
||||
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 积分服务的消费者,接收到下单成功后增加积分
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
@Service
|
||||
public class IntegralReceiveService {
|
||||
@StreamListener(Sink.INPUT)
|
||||
public void receive(Message message) {
|
||||
//模拟消费异常
|
||||
String consumeError = (String)message.getHeaders().get("consumeError");
|
||||
if ("1".equals(consumeError)) {
|
||||
System.err.println("============Exception:积分进程挂了,消费消息失败");
|
||||
//模拟插入订单后服务器挂了,没有commit事务消息
|
||||
throw new RuntimeException("积分服务器挂了");
|
||||
}
|
||||
|
||||
System.out.println("============收到订单信息,增加积分:" + message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消费死信队列
|
||||
*/
|
||||
@StreamListener("inputDlq")
|
||||
public void receiveDlq(Message message) {
|
||||
String orderId = (String)message.getHeaders().get("orderId");
|
||||
System.err.println("============消费死信队列消息,记录日志并预警:" + orderId);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.rocketmq.demo.service.impl;
|
||||
|
||||
import com.rocketmq.demo.model.Order;
|
||||
import com.rocketmq.demo.service.IOrderService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class OrderServiceImpl implements IOrderService {
|
||||
@Override
|
||||
public void save(Order order) {
|
||||
System.out.println("============保存订单成功:" + order.getOrderId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user