mq:默认使用 event 替代 spring cloud stream

This commit is contained in:
YunaiV 2023-11-02 12:57:25 +08:00
parent fdb479ef49
commit 02693836b2
18 changed files with 95 additions and 89 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.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;
import java.util.function.Consumer;
/**
* 针对 {@link UserCreateMessage} 的消费者
*
* @author owen
*/
@Component
@Slf4j
public class UserCreateConsumer implements Consumer<UserCreateMessage> {
@Resource
private CouponService couponService;
@Override
public void accept(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,21 +0,0 @@
package cn.iocoder.yudao.module.promotion.mq.message.coupon;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 会员用户创建消息
*
* @author owen
*/
@Data
public class UserCreateMessage {
/**
* 用户编号
*/
@NotNull(message = "用户编号不能为空")
private Long userId;
}

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

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.member.mq.message.user; package cn.iocoder.yudao.module.member.message.user;
import lombok.Data; import lombok.Data;
@ -10,7 +10,7 @@ import javax.validation.constraints.NotNull;
* @author owen * @author owen
*/ */
@Data @Data
public class UserCreateMessage { public class MemberUserCreateMessage {
/** /**
* 用户编号 * 用户编号

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

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

View File

@ -1,9 +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.bus.AbstractBusProducer; import cn.iocoder.yudao.module.member.message.user.MemberUserCreateMessage;
import cn.iocoder.yudao.module.member.mq.message.user.UserCreateMessage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -15,19 +14,18 @@ import javax.annotation.Resource;
*/ */
@Slf4j @Slf4j
@Component @Component
public class MemberUserProducer extends AbstractBusProducer { public class MemberUserProducer {
@Resource @Resource
private StreamBridge streamBridge; private ApplicationContext applicationContext;
// TODO 芋艿后续要在细看下
/** /**
* 发送 {@link UserCreateMessage} 消息 * 发送 {@link MemberUserCreateMessage} 消息
* *
* @param userId 用户编号 * @param userId 用户编号
*/ */
public void sendUserCreateMessage(Long userId) { public void sendUserCreateMessage(Long userId) {
streamBridge.send("member-create-out-0",new UserCreateMessage().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

@ -3,10 +3,11 @@ package cn.iocoder.yudao.module.system.mq.consumer.mail;
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.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;
import java.util.function.Consumer;
/** /**
* 针对 {@link MailSendMessage} 的消费者 * 针对 {@link MailSendMessage} 的消费者
@ -15,14 +16,15 @@ import java.util.function.Consumer;
*/ */
@Component @Component
@Slf4j @Slf4j
public class MailSendConsumer implements Consumer<MailSendMessage> { public class MailSendConsumer {
@Resource @Resource
private MailSendService mailSendService; private MailSendService mailSendService;
@Override @EventListener
public void accept(MailSendMessage message) { @Async // Spring Event 默认在 Producer 发送的线程通过 @Async 实现异步
log.info("[accept][消息内容({})]", message); public void onMessage(MailSendMessage message) {
log.info("[onMessage][消息内容({})]", message);
mailSendService.doSendMail(message); mailSendService.doSendMail(message);
} }

View File

@ -3,10 +3,11 @@ 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 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;
import java.util.function.Consumer;
/** /**
* 针对 {@link SmsSendMessage} 的消费者 * 针对 {@link SmsSendMessage} 的消费者
@ -15,14 +16,16 @@ import java.util.function.Consumer;
*/ */
@Component @Component
@Slf4j @Slf4j
public class SmsSendConsumer implements Consumer<SmsSendMessage> { public class SmsSendConsumer {
@Resource @Resource
private SmsSendService smsSendService; private SmsSendService smsSendService;
@Override @EventListener
public void accept(SmsSendMessage message) { @Async // Spring Event 默认在 Producer 发送的线程通过 @Async 实现异步
log.info("[accept][消息内容({})]", message); public void onMessage(SmsSendMessage message) {
log.info("[onMessage][消息内容({})]", message);
smsSendService.doSendSms(message); smsSendService.doSendSms(message);
} }
} }

View File

@ -1,9 +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.bus.AbstractBusProducer;
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.cloud.stream.function.StreamBridge; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -16,10 +15,10 @@ import javax.annotation.Resource;
*/ */
@Slf4j @Slf4j
@Component @Component
public class MailProducer extends AbstractBusProducer { public class MailProducer {
@Resource @Resource
private StreamBridge streamBridge; private ApplicationContext applicationContext;
/** /**
* 发送 {@link MailSendMessage} 消息 * 发送 {@link MailSendMessage} 消息
@ -36,7 +35,7 @@ public class MailProducer extends AbstractBusProducer {
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);
streamBridge.send("mailSend-out-0", message); applicationContext.publishEvent(message);
} }
} }

View File

@ -1,4 +0,0 @@
/**
* 占位
*/
package cn.iocoder.yudao.module.system.mq.producer;

View File

@ -1,10 +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.bus.AbstractBusProducer;
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.cloud.stream.function.StreamBridge; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -14,14 +13,14 @@ import java.util.List;
* Sms 短信相关消息的 Producer * Sms 短信相关消息的 Producer
* *
* @author zzf * @author zzf
* @date 2021/3/9 16:35 * @since 2021/3/9 16:35
*/ */
@Slf4j @Slf4j
@Component @Component
public class SmsProducer extends AbstractBusProducer { public class SmsProducer {
@Resource @Resource
private StreamBridge streamBridge; private ApplicationContext applicationContext;
/** /**
* 发送 {@link SmsSendMessage} 消息 * 发送 {@link SmsSendMessage} 消息
@ -36,7 +35,7 @@ public class SmsProducer extends AbstractBusProducer {
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);
streamBridge.send("smsSend-out-0", message); applicationContext.publishEvent(message);
} }
} }