3.基于消息队列实现分布式事务
3.1 准备工作
3.1.1 changgou_order库新增数据表
DROP TABLE IF EXISTS `tb_task`;
CREATE TABLE `tb_task` (
`id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '任务id',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`delete_time` datetime DEFAULT NULL,
`task_type` varchar(32) DEFAULT NULL COMMENT '任务类型',
`mq_exchange` varchar(64) DEFAULT NULL COMMENT '交换机名称',
`mq_routingkey` varchar(64) DEFAULT NULL COMMENT 'routingkey',
`request_body` varchar(512) DEFAULT NULL COMMENT '任务请求的内容',
`status` varchar(32) DEFAULT NULL COMMENT '任务状态',
`errormsg` varchar(512) DEFAULT NULL COMMENT '任务错误信息',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `tb_task_his`;
CREATE TABLE `tb_task_his` (
`id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '任务id',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`delete_time` datetime DEFAULT NULL,
`task_type` varchar(32) DEFAULT NULL COMMENT '任务类型',
`mq_exchange` varchar(64) DEFAULT NULL COMMENT '交换机名称',
`mq_routingkey` varchar(64) DEFAULT NULL COMMENT 'routingkey',
`request_body` varchar(512) DEFAULT NULL COMMENT '任务请求的内容',
`status` varchar(32) DEFAULT NULL COMMENT '任务状态',
`errormsg` varchar(512) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
3.1.2 changgou_service_order_api添加相关实体类
package com.changgou.order.pojo;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Table(name = "tb_task")
public class Task {
@Id
private Long id;
@Column(name = "create_time")
private Date createTime;
@Column(name = "update_time")
private Date updateTime;
@Column(name = "delete_time")
private Date deleteTime;
@Column(name = "task_type")
private String taskType;
@Column(name = "mq_exchange")
private String mqExchange;
@Column(name = "mq_routingkey")
private String mqRoutingkey;
@Column(name = "request_body")
private String requestBody;
@Column(name = "status")
private String status;
@Column(name = "errormsg")
private String errormsg;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getDeleteTime() {
return deleteTime;
}
public void setDeleteTime(Date deleteTime) {
this.deleteTime = deleteTime;
}
public String getTaskType() {
return taskType;
}
public void setTaskType(String taskType) {
this.taskType = taskType;
}
public String getMqExchange() {
return mqExchange;
}
public void setMqExchange(String mqExchange) {
this.mqExchange = mqExchange;
}
public String getMqRoutingkey() {
return mqRoutingkey;
}
public void setMqRoutingkey(String mqRoutingkey) {
this.mqRoutingkey = mqRoutingkey;
}
public String getRequestBody() {
return requestBody;
}
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getErrormsg() {
return errormsg;
}
public void setErrormsg(String errormsg) {
this.errormsg = errormsg;
}
}
package com.changgou.order.pojo;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Table(name = "tb_task_his")
public class TaskHis {
@Id
private Long id;
@Column(name = "create_time")
private Date createTime;
@Column(name = "update_time")
private Date updateTime;
@Column(name = "delete_time")
private Date deleteTime;
@Column(name = "task_type")
private String taskType;
@Column(name = "mq_exchange")
private String mqExchange;
@Column(name = "mq_routingkey")
private String mqRoutingkey;
@Column(name = "request_body")
private String requestBody;
@Column(name = "status")
private String status;
@Column(name = "errormsg")
private String errormsg;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getDeleteTime() {
return deleteTime;
}
public void setDeleteTime(Date deleteTime) {
this.deleteTime = deleteTime;
}
public String getTaskType() {
return taskType;
}
public void setTaskType(String taskType) {
this.taskType = taskType;
}
public String getMqExchange() {
return mqExchange;
}
public void setMqExchange(String mqExchange) {
this.mqExchange = mqExchange;
}
public String getMqRoutingkey() {
return mqRoutingkey;
}
public void setMqRoutingkey(String mqRoutingkey) {
this.mqRoutingkey = mqRoutingkey;
}
public String getRequestBody() {
return requestBody;
}
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getErrormsg() {
return errormsg;
}
public void setErrormsg(String errormsg) {
this.errormsg = errormsg;
}
}
3.1.3 changgou_user新增积分日志表
DROP TABLE IF EXISTS `tb_point_log`;
CREATE TABLE `tb_point_log` (
`order_id` varchar(200) NOT NULL,
`user_id` varchar(200) NOT NULL,
`point` int(11) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.1.4 changgou_service_user_api添加实体类 PointLog
@Table(name="tb_point_log")
public class PointLog {
private String orderId;
private String userId;
private Integer point;
//getter,setter略
}
3.1.5 changgou_service_order添加rabbitMQ配置类
package com.changgou.order.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
//添加积分任务交换机
public static final String EX_BUYING_ADDPOINTUSER = "ex_buying_addpointuser";
//添加积分消息队列
public static final String CG_BUYING_ADDPOINT = "cg_buying_addpoint";
//完成添加积分消息队列
public static final String CG_BUYING_FINISHADDPOINT = "cg_buying_finishaddpoint";
//添加积分路由key
public static final String CG_BUYING_ADDPOINT_KEY = "addpoint";
//完成添加积分路由key
public static final String CG_BUYING_FINISHADDPOINT_KEY = "finishaddpoint";
//声明交换机
@Bean(EX_BUYING_ADDPOINTUSER)
public Exchange EX_BUYING_ADDPOINTUSER(){
return ExchangeBuilder.directExchange(EX_BUYING_ADDPOINTUSER).durable(true).build();
}
//声明队列
@Bean(CG_BUYING_ADDPOINT)
public Queue CG_BUYING_ADDPOINT(){
Queue queue = new Queue(CG_BUYING_ADDPOINT);
return queue;
}
@Bean(CG_BUYING_FINISHADDPOINT)
public Queue CG_BUYING_FINISHADDPOINT(){
Queue queue = new Queue(CG_BUYING_FINISHADDPOINT);
return queue;
}
//队列绑定交换机
@Bean
public Binding BINDING_CG_BUYING_ADDPOINT(@Qualifier(CG_BUYING_ADDPOINT) Queue queue,@Qualifier(EX_BUYING_ADDPOINTUSER)Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_ADDPOINT_KEY).noargs();
}
@Bean
public Binding BINDING_CG_BUYING_FINISHADDPOINT(@Qualifier(CG_BUYING_FINISHADDPOINT) Queue queue,@Qualifier(EX_BUYING_ADDPOINTUSER)Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_FINISHADDPOINT_KEY).noargs();
}
}
3.2 订单服务添加任务并发送
3.2.1 修改添加订单方法
当添加订单的时候,添加任务表中相关数据, 局部代码如下
/
/增加任务表记录
Task task = new Task();
task.setCreateTime(new Date());
task.setUpdateTime(new Date());
task.setMqExchange(RabbitMQConfig.EX_BUYING_ADDPOINTURSE);
task.setMqRoutingkey(RabbitMQConfig.CG_BUYING_ADDPOINT_KEY);
Map map = new HashMap();
map.put("userName",order.getUsername());
map.put("orderId",order.getId());
map.put("point",order.getPayMoney());
task.setRequestBody(JSON.toJSONString(map));
taskMapper.insertSelective(task);
3.2.2 定时扫描任务表最新数据
3.2.2.1 修改订单服务启动类,添加开启定时任务注解
@EnableScheduling
3.2.2.2 定义定时任务类
3.2.2.2.1 查询最新数据
更新taskMapper新增方法,查询所有小于系统当前时间的数据
public interface TaskMapper extends Mapper<Task> {
@Select("SELECT * from tb_task WHERE update_time<#{currentTime}")
@Results({@Result(column = "create_time",property = "createTime"),
@Result(column = "update_time",property = "updateTime"),
@Result(column = "delete_time",property = "deleteTime"),
@Result(column = "task_type",property = "taskType"),
@Result(column = "mq_exchange",property = "mqExchange"),
@Result(column = "mq_routingkey",property = "mqRoutingkey"),
@Result(column = "request_body",property = "requestBody"),
@Result(column = "status",property = "status"),
@Result(column = "errormsg",property = "errormsg")})
List<Task> findTaskLessTanCurrentTime(Date currentTime);
}
3.2.2.2.2 任务类实现
package com.changgou.order.task;
import com.alibaba.fastjson.JSON;
import com.changgou.order.config.RabbitMQConfig;
import com.changgou.order.dao.TaskMapper;
import com.changgou.order.pojo.Task;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
@Component
public class QueryPointTask {
@Autowired
private TaskMapper taskMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
@Scheduled(cron = "0/2 * * * * ?")
public void queryTask(){
//1. 获取小于系统当前时间的数据
List<Task> taskList = taskMapper.findTaskLessThanCurrentTime(new Date());
if (taskList != null && taskList.size()>0){
//2.将任务发送到消息队列上
for (Task task : taskList) {
rabbitTemplate.convertAndSend(RabbitMQConfig.EX_BUYING_ADDPOINTUSER,RabbitMQConfig.CG_BUYING_ADDPOINT_KEY, JSON.toJSONString(task));
System.out.println("订单服务向添加积分队列发送了一条消息");
}
}
}
}
3.3.1 添加rabbitmq配置类(与订单服务相同)
package com.changgou.order.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
//添加积分任务交换机
public static final String EX_BUYING_ADDPOINTUSER = "ex_buying_addpointuser";
//添加积分消息队列
public static final String CG_BUYING_ADDPOINT = "cg_buying_addpoint";
//完成添加积分消息队列
public static final String CG_BUYING_FINISHADDPOINT = "cg_buying_finishaddpoint";
//添加积分路由key
public static final String CG_BUYING_ADDPOINT_KEY = "addpoint";
//完成添加积分路由key
public static final String CG_BUYING_FINISHADDPOINT_KEY = "finishaddpoint";
//声明交换机
@Bean(EX_BUYING_ADDPOINTUSER)
public Exchange EX_BUYING_ADDPOINTUSER(){
return ExchangeBuilder.directExchange(EX_BUYING_ADDPOINTUSER).durable(true).build();
}
//声明队列
@Bean(CG_BUYING_ADDPOINT)
public Queue CG_BUYING_ADDPOINT(){
Queue queue = new Queue(CG_BUYING_ADDPOINT);
return queue;
}
@Bean(CG_BUYING_FINISHADDPOINT)
public Queue CG_BUYING_FINISHADDPOINT(){
Queue queue = new Queue(CG_BUYING_FINISHADDPOINT);
return queue;
}
//队列绑定交换机
@Bean
public Binding BINDING_CG_BUYING_ADDPOINT(@Qualifier(CG_BUYING_ADDPOINT) Queue queue,@Qualifier(EX_BUYING_ADDPOINTUSER)Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_ADDPOINT_KEY).noargs();
}
@Bean
public Binding BINDING_CG_BUYING_FINISHADDPOINT(@Qualifier(CG_BUYING_FINISHADDPOINT) Queue queue,@Qualifier(EX_BUYING_ADDPOINTUSER)Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_FINISHADDPOINT_KEY).noargs();
}
}
3.3.2 定义消息监听类 (user_service)
package com.changgou.user.listener;
import com.alibaba.fastjson.JSON;
import com.changgou.order.pojo.Task;
import com.changgou.user.config.RabbitMQConfig;
import com.changgou.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class AddPointListener {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private UserService userService;
@Autowired
private RabbitTemplate rabbitTemplate;
@RabbitListener(queues = RabbitMQConfig.CG_BUYING_ADDPOINT)
public void receiveAddPointMessage(String message){
System.out.println("用户服务接收到了任务消息");
//转换消息
Task task = JSON.parseObject(message, Task.class);
if (task == null || StringUtils.isEmpty(task.getRequestBody())){
return;
}
//判断redis中当前的任务是否存在
Object value = redisTemplate.boundValueOps(task.getId()).get();
if (value != null){
return;
}
//更新用户积分
int result = userService.updateUserPoint(task);
if (result == 0){
return;
}
//向订单服务返回通知消息
rabbitTemplate.convertAndSend(RabbitMQConfig.EX_BUYING_ADDPOINTUSER,RabbitMQConfig.CG_BUYING_FINISHADDPOINT_KEY,JSON.toJSONString(task));
System.out.println("用户服务向完成添加积分队列发送了一条消息");
}
}
3.3.3 定义修改用户积分实现
1)判断当前订单是否操作过
2)将任务存入redis
3)修改用户积分
4)添加积分日志表记录
5)删除redis中记录
@Autowired
private PointLogMapper pointLogMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
@Transactional
public int updateUserPoint(Task task) {
System.out.println("用户服务现在开始对任务进行处理");
//1.从task中获取相关数据
Map map = JSON.parseObject(task.getRequestBody(), Map.class);
String username = map.get("username").toString();
String orderId = map.get("orderId").toString();
int point = (int) map.get("point");
//2.判断当前的任务是否操作过
PointLog pointLog = pointLogMapper.findPointLogByOrderId(orderId);
if (pointLog != null){
return 0;
}
//3.将任务存入到redis中
redisTemplate.boundValueOps(task.getId()).set("exist",30, TimeUnit.SECONDS);
//4.修改用户积分
int result = userMapper.updateUserPoint(username,point);
if (result<=0){
return 0;
}
//5.记录积分日志信息
pointLog = new PointLog();
pointLog.setUserId(username);
pointLog.setOrderId(orderId);
pointLog.setPoint(point);
result = pointLogMapper.insertSelective(pointLog);
if (result <= 0){
return 0;
}
//删除redis中的记录
redisTemplate.delete(task.getId());
return 1;
3.3.4 定义根据订单id查询积分日志表
定义PointLogMapper,实现根据订单id查询
public interface PointLogMapper extends Mapper<PointLog> {
@Select("select * from tb_point_log where order_id=#{orderId}")
PointLog findLogInfoByOrderId(@Param("orderId") String orderId);
}
3.4 订单服务删除原任务
3.4.1 定义监听类
在订单服务中定义监听类,用于监听队列,如果队列中有消息,则删除原任务防止消息重复发送,并对任务信息进行记录
@Component
public class DelTaskListener {
@Autowired
private TaskService taskService;
@RabbitListener(queues = RabbitMQConfig.CG_BUYING_FINISHADDPOINT)
public void receiveMessage(String message){
Task task = JSON.parseObject(message, Task.class);
taskService.delTask(task);
}
}
3.4.2 定义任务service
public interface TaskService {
void delTask(Task task);
}
package com.changgou.order.service.impl;
import com.changgou.order.dao.TaskHisMapper;
import com.changgou.order.dao.TaskMapper;
import com.changgou.order.pojo.Task;
import com.changgou.order.pojo.TaskHis;
import com.changgou.order.service.TaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
@Service
public class TaskServiceImpl implements TaskService {
@Autowired
private TaskHisMapper taskHisMapper;
@Autowired
private TaskMapper taskMapper;
@Override
@Transactional
public void delTask(Task task) {
//1.记录删除时间
task.setDeleteTime(new Date());
Long taskId = task.getId();
task.setId(null);
//bean拷贝
TaskHis taskHis = new TaskHis();
BeanUtils.copyProperties(task,taskHis);
//记录历史任务数据
taskHisMapper.insertSelective(taskHis);
//删除原有任务数据
task.setId(taskId);
taskMapper.deleteByPrimaryKey(task);
System.out.println("订单服务完成了添加历史任务并删除原有任务的操作");
}
}