mq 重构:默认的 mq 实现,采用 event,减少对 redis、rocketmq 等消息队列的依赖

This commit is contained in:
YunaiV 2023-10-30 12:53:54 +08:00
parent 87fb47ca6a
commit d048daf7d6
21 changed files with 110 additions and 129 deletions

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.promotion.mq.consumer.coupon;
import cn.iocoder.yudao.module.member.message.user.MemberUserCreateMessage;
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 用户注册时发送优惠劵的消费者 {@link MemberUserCreateMessage} 消息
*
* @author owen
*/
@Component
@Slf4j
public class CouponTakeByRegisterConsumer {
@Resource
private CouponService couponService;
@EventListener
@Async // Spring Event 默认在 Producer 发送的线程通过 @Async 实现异步
public void onMessage(MemberUserCreateMessage message) {
log.info("[onMessage][消息内容({})]", message);
couponService.takeCouponByRegister(message.getUserId());
}
}

View File

@ -1,29 +0,0 @@
package cn.iocoder.yudao.module.promotion.mq.consumer.coupon;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
import cn.iocoder.yudao.module.promotion.mq.message.coupon.UserCreateMessage;
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 针对 {@link UserCreateMessage} 的消费者
*
* @author owen
*/
@Component
@Slf4j
public class UserCreateConsumer extends AbstractStreamMessageListener<UserCreateMessage> {
@Resource
private CouponService couponService;
@Override
public void onMessage(UserCreateMessage message) {
log.info("[onMessage][消息内容({})]", message);
couponService.takeCouponByRegister(message.getUserId());
}
}

View File

@ -0,0 +1,4 @@
/**
* 消息队列的消费者
*/
package cn.iocoder.yudao.module.promotion.mq.consumer;

View File

@ -1,29 +0,0 @@
package cn.iocoder.yudao.module.promotion.mq.message.coupon;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull;
/**
* 会员用户创建消息
*
* @author owen
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class UserCreateMessage extends AbstractStreamMessage {
/**
* 用户编号
*/
@NotNull(message = "用户编号不能为空")
private Long userId;
@Override
public String getStreamKey() {
return "member.user.create";
}
}

View File

@ -0,0 +1,4 @@
/**
* 消息队列的消息
*/
package cn.iocoder.yudao.module.promotion.mq.message;

View File

@ -0,0 +1,4 @@
/**
* 消息队列的生产者
*/
package cn.iocoder.yudao.module.promotion.mq.producer;

View File

@ -0,0 +1,4 @@
/**
* 消息队列的消息
*/
package cn.iocoder.yudao.module.member.message;

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.member.message.user;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 会员用户创建消息
*
* @author owen
*/
@Data
public class MemberUserCreateMessage {
/**
* 用户编号
*/
@NotNull(message = "用户编号不能为空")
private Long userId;
}

View File

@ -4,7 +4,7 @@ Content-Type: application/json
tenant-id: {{appTenentId}} tenant-id: {{appTenentId}}
{ {
"mobile": "15601691300", "mobile": "15601691388",
"password": "admin123" "password": "admin123"
} }
@ -14,7 +14,7 @@ Content-Type: application/json
tenant-id: {{appTenentId}} tenant-id: {{appTenentId}}
{ {
"mobile": "15601691399", "mobile": "15601691388",
"scene": 1 "scene": 1
} }
@ -22,9 +22,10 @@ tenant-id: {{appTenentId}}
POST {{appApi}}/member/auth/sms-login POST {{appApi}}/member/auth/sms-login
Content-Type: application/json Content-Type: application/json
tenant-id: {{appTenentId}} tenant-id: {{appTenentId}}
terminal: 30
{ {
"mobile": "15601691301", "mobile": "15601691388",
"code": 9999 "code": 9999
} }

View File

@ -0,0 +1,4 @@
/**
* 消息队列的消费者
*/
package cn.iocoder.yudao.module.member.mq.consumer;

View File

@ -0,0 +1,4 @@
/**
* 消息队列的消息
*/
package cn.iocoder.yudao.module.member.mq.message;

View File

@ -1,29 +0,0 @@
package cn.iocoder.yudao.module.member.mq.message.user;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull;
/**
* 会员用户创建消息
*
* @author owen
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class MemberUserCreateMessage extends AbstractStreamMessage {
/**
* 用户编号
*/
@NotNull(message = "用户编号不能为空")
private Long userId;
@Override
public String getStreamKey() {
return "member.user.create";
}
}

View File

@ -0,0 +1,4 @@
/**
* 消息队列的生产者
*/
package cn.iocoder.yudao.module.member.mq.producer;

View File

@ -1,8 +1,8 @@
package cn.iocoder.yudao.module.member.mq.producer.user; package cn.iocoder.yudao.module.member.mq.producer.user;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import cn.iocoder.yudao.module.member.message.user.MemberUserCreateMessage;
import cn.iocoder.yudao.module.member.mq.message.user.MemberUserCreateMessage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -17,7 +17,7 @@ import javax.annotation.Resource;
public class MemberUserProducer { public class MemberUserProducer {
@Resource @Resource
private RedisMQTemplate redisMQTemplate; private ApplicationContext applicationContext;
/** /**
* 发送 {@link MemberUserCreateMessage} 消息 * 发送 {@link MemberUserCreateMessage} 消息
@ -25,7 +25,7 @@ public class MemberUserProducer {
* @param userId 用户编号 * @param userId 用户编号
*/ */
public void sendUserCreateMessage(Long userId) { public void sendUserCreateMessage(Long userId) {
redisMQTemplate.send(new MemberUserCreateMessage().setUserId(userId)); applicationContext.publishEvent(new MemberUserCreateMessage().setUserId(userId));
} }
} }

View File

@ -6,9 +6,9 @@ tenant-id: {{adminTenentId}}
{ {
"templateCode": "test_01", "templateCode": "test_01",
"mobile": "156016913900", "mobile": "15601691390",
"params": { "templateParams": {
"key01": "value01", "operation": "value01",
"key02": "value02" "code": "value02"
} }
} }

View File

@ -1,10 +1,10 @@
package cn.iocoder.yudao.module.system.mq.consumer.mail; package cn.iocoder.yudao.module.system.mq.consumer.mail;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
import cn.iocoder.yudao.module.system.service.mail.MailSendService; import cn.iocoder.yudao.module.system.service.mail.MailSendService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -16,12 +16,13 @@ import javax.annotation.Resource;
*/ */
@Component @Component
@Slf4j @Slf4j
public class MailSendConsumer extends AbstractStreamMessageListener<MailSendMessage> { public class MailSendConsumer {
@Resource @Resource
private MailSendService mailSendService; private MailSendService mailSendService;
@Override @EventListener
@Async // Spring Event 默认在 Producer 发送的线程通过 @Async 实现异步
public void onMessage(MailSendMessage message) { public void onMessage(MailSendMessage message) {
log.info("[onMessage][消息内容({})]", message); log.info("[onMessage][消息内容({})]", message);
mailSendService.doSendMail(message); mailSendService.doSendMail(message);

View File

@ -2,8 +2,9 @@ package cn.iocoder.yudao.module.system.mq.consumer.sms;
import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage; import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
import cn.iocoder.yudao.module.system.service.sms.SmsSendService; import cn.iocoder.yudao.module.system.service.sms.SmsSendService;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -15,12 +16,13 @@ import javax.annotation.Resource;
*/ */
@Component @Component
@Slf4j @Slf4j
public class SmsSendConsumer extends AbstractStreamMessageListener<SmsSendMessage> { public class SmsSendConsumer {
@Resource @Resource
private SmsSendService smsSendService; private SmsSendService smsSendService;
@Override @EventListener
@Async // Spring Event 默认在 Producer 发送的线程通过 @Async 实现异步
public void onMessage(SmsSendMessage message) { public void onMessage(SmsSendMessage message) {
log.info("[onMessage][消息内容({})]", message); log.info("[onMessage][消息内容({})]", message);
smsSendService.doSendSms(message); smsSendService.doSendSms(message);

View File

@ -1,8 +1,6 @@
package cn.iocoder.yudao.module.system.mq.message.mail; package cn.iocoder.yudao.module.system.mq.message.mail;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -13,8 +11,7 @@ import javax.validation.constraints.NotNull;
* @author 芋道源码 * @author 芋道源码
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) public class MailSendMessage {
public class MailSendMessage extends AbstractStreamMessage {
/** /**
* 邮件日志编号 * 邮件日志编号
@ -47,9 +44,4 @@ public class MailSendMessage extends AbstractStreamMessage {
@NotEmpty(message = "邮件内容不能为空") @NotEmpty(message = "邮件内容不能为空")
private String content; private String content;
@Override
public String getStreamKey() {
return "system.mail.send";
}
} }

View File

@ -1,9 +1,7 @@
package cn.iocoder.yudao.module.system.mq.message.sms; package cn.iocoder.yudao.module.system.mq.message.sms;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
@ -14,8 +12,7 @@ import java.util.List;
* @author 芋道源码 * @author 芋道源码
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) public class SmsSendMessage {
public class SmsSendMessage extends AbstractStreamMessage {
/** /**
* 短信日志编号 * 短信日志编号
@ -42,9 +39,4 @@ public class SmsSendMessage extends AbstractStreamMessage {
*/ */
private List<KeyValue<String, Object>> templateParams; private List<KeyValue<String, Object>> templateParams;
@Override
public String getStreamKey() {
return "system.sms.send";
}
} }

View File

@ -1,8 +1,8 @@
package cn.iocoder.yudao.module.system.mq.producer.mail; package cn.iocoder.yudao.module.system.mq.producer.mail;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -18,7 +18,7 @@ import javax.annotation.Resource;
public class MailProducer { public class MailProducer {
@Resource @Resource
private RedisMQTemplate redisMQTemplate; private ApplicationContext applicationContext;
/** /**
* 发送 {@link MailSendMessage} 消息 * 发送 {@link MailSendMessage} 消息
@ -35,7 +35,7 @@ public class MailProducer {
MailSendMessage message = new MailSendMessage() MailSendMessage message = new MailSendMessage()
.setLogId(sendLogId).setMail(mail).setAccountId(accountId) .setLogId(sendLogId).setMail(mail).setAccountId(accountId)
.setNickname(nickname).setTitle(title).setContent(content); .setNickname(nickname).setTitle(title).setContent(content);
redisMQTemplate.send(message); applicationContext.publishEvent(message);
} }
} }

View File

@ -1,9 +1,9 @@
package cn.iocoder.yudao.module.system.mq.producer.sms; package cn.iocoder.yudao.module.system.mq.producer.sms;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage; import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -20,7 +20,7 @@ import java.util.List;
public class SmsProducer { public class SmsProducer {
@Resource @Resource
private RedisMQTemplate redisMQTemplate; private ApplicationContext applicationContext;
/** /**
* 发送 {@link SmsSendMessage} 消息 * 发送 {@link SmsSendMessage} 消息
@ -35,7 +35,7 @@ public class SmsProducer {
Long channelId, String apiTemplateId, List<KeyValue<String, Object>> templateParams) { Long channelId, String apiTemplateId, List<KeyValue<String, Object>> templateParams) {
SmsSendMessage message = new SmsSendMessage().setLogId(logId).setMobile(mobile); SmsSendMessage message = new SmsSendMessage().setLogId(logId).setMobile(mobile);
message.setChannelId(channelId).setApiTemplateId(apiTemplateId).setTemplateParams(templateParams); message.setChannelId(channelId).setApiTemplateId(apiTemplateId).setTemplateParams(templateParams);
redisMQTemplate.send(message); applicationContext.publishEvent(message);
} }
} }