优化 mq starter 组件,增加 RedisMQTemplate 模板类

This commit is contained in:
YunaiV 2021-12-05 23:00:41 +08:00
parent ade55d89a4
commit d29b0beb9b
26 changed files with 137 additions and 75 deletions

View File

@ -1,13 +1,13 @@
package cn.iocoder.yudao.adminserver.modules.infra.mq.message.config;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
/**
* 配置数据刷新 Message
*/
@Data
public class InfConfigRefreshMessage implements ChannelMessage {
public class InfConfigRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.infra.mq.producer.config;
import cn.iocoder.yudao.adminserver.modules.infra.mq.message.config.InfConfigRefreshMessage;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class InfConfigProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link InfConfigRefreshMessage} 消息
*/
public void sendConfigRefreshMessage() {
InfConfigRefreshMessage message = new InfConfigRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.dept;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 部门数据刷新 Message
*
* @author 芋道源码
*/
@Data
public class SysDeptRefreshMessage implements ChannelMessage {
@EqualsAndHashCode(callSuper = true)
public class SysDeptRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,13 +1,15 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.dict;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 字典数据数据刷新 Message
*/
@Data
public class SysDictDataRefreshMessage implements ChannelMessage {
@EqualsAndHashCode(callSuper = true)
public class SysDictDataRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,7 +1,8 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.mail;
import cn.iocoder.yudao.framework.mq.core.stream.StreamMessage;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull;
import java.util.Map;
@ -12,7 +13,8 @@ import java.util.Map;
* @author 芋道源码
*/
@Data
public class SysMailSendMessage implements StreamMessage {
@EqualsAndHashCode(callSuper = true)
public class SysMailSendMessage extends AbstractStreamMessage {
/**
* 邮箱地址

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 菜单数据刷新 Message
*
* @author 芋道源码
*/
@Data
public class SysMenuRefreshMessage implements ChannelMessage {
@EqualsAndHashCode(callSuper = true)
public class SysMenuRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 角色与菜单数据刷新 Message
*
* @author 芋道源码
*/
@Data
public class SysRoleMenuRefreshMessage implements ChannelMessage {
@EqualsAndHashCode(callSuper = true)
public class SysRoleMenuRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 角色数据刷新 Message
*
* @author 芋道源码
*/
@Data
public class SysRoleRefreshMessage implements ChannelMessage {
@EqualsAndHashCode(callSuper = true)
public class SysRoleRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 短信渠道的数据刷新 Message
*
* @author 芋道源码
*/
@Data
public class SysSmsChannelRefreshMessage implements ChannelMessage {
@EqualsAndHashCode(callSuper = true)
public class SysSmsChannelRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 短信模板的数据刷新 Message
*
* @author 芋道源码
*/
@Data
public class SysSmsTemplateRefreshMessage implements ChannelMessage {
@EqualsAndHashCode(callSuper = true)
public class SysSmsTemplateRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dept;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.dept.SysDeptRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysDeptProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SysDeptRefreshMessage} 消息
*/
public void sendDeptRefreshMessage() {
SysDeptRefreshMessage message = new SysDeptRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dict;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.dict.SysDictDataRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysDictDataProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SysDictDataRefreshMessage} 消息
*/
public void sendDictDataRefreshMessage() {
SysDictDataRefreshMessage message = new SysDictDataRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysMenuRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysMenuProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SysMenuRefreshMessage} 消息
*/
public void sendMenuRefreshMessage() {
SysMenuRefreshMessage message = new SysMenuRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleMenuRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysPermissionProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SysRoleMenuRefreshMessage} 消息
*/
public void sendRoleMenuRefreshMessage() {
SysRoleMenuRefreshMessage message = new SysRoleMenuRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -16,14 +15,14 @@ import javax.annotation.Resource;
public class SysRoleProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SysRoleRefreshMessage} 消息
*/
public void sendRoleRefreshMessage() {
SysRoleRefreshMessage message = new SysRoleRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,15 +1,12 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsChannelRefreshMessage;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsTemplateRefreshMessage;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* Sms 短信相关消息的 Producer
@ -22,14 +19,14 @@ import java.util.List;
public class SysSmsProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SysSmsChannelRefreshMessage} 消息
*/
public void sendSmsChannelRefreshMessage() {
SysSmsChannelRefreshMessage message = new SysSmsChannelRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
/**
@ -37,7 +34,7 @@ public class SysSmsProducer {
*/
public void sendSmsTemplateRefreshMessage() {
SysSmsTemplateRefreshMessage message = new SysSmsTemplateRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,8 +1,9 @@
package cn.iocoder.yudao.coreservice.modules.system.mq.message.sms;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.stream.StreamMessage;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull;
import java.util.List;
@ -13,7 +14,8 @@ import java.util.List;
* @author 芋道源码
*/
@Data
public class SysSmsSendMessage implements StreamMessage {
@EqualsAndHashCode(callSuper = true)
public class SysSmsSendMessage extends AbstractStreamMessage {
/**
* 短信日志编号

View File

@ -2,9 +2,8 @@ package cn.iocoder.yudao.coreservice.modules.system.mq.producer.sms;
import cn.iocoder.yudao.coreservice.modules.system.mq.message.sms.SysSmsSendMessage;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -21,7 +20,7 @@ import java.util.List;
public class SysSmsCoreProducer {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SysSmsSendMessage} 消息
@ -36,7 +35,7 @@ public class SysSmsCoreProducer {
Long channelId, String apiTemplateId, List<KeyValue<String, Object>> templateParams) {
SysSmsSendMessage message = new SysSmsSendMessage().setLogId(logId).setMobile(mobile);
message.setChannelId(channelId).setApiTemplateId(apiTemplateId).setTemplateParams(templateParams);
RedisMessageUtils.sendStreamMessage(stringRedisTemplate, message);
redisMQTemplate.send(message);
}
}

View File

@ -1,12 +1,12 @@
package cn.iocoder.yudao.framework.mq.config;
import cn.hutool.system.SystemUtil;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@ -15,6 +15,7 @@ import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
@ -31,6 +32,13 @@ import java.util.List;
@Slf4j
public class YudaoMQAutoConfiguration {
@Bean
public RedisMQTemplate redisMQTemplate(StringRedisTemplate stringRedisTemplate) {
return new RedisMQTemplate(stringRedisTemplate);
}
// ========== 消费者相关 ==========
/**
* 创建 Redis Pub/Sub 广播消费的容器
*/
@ -71,7 +79,6 @@ public class YudaoMQAutoConfiguration {
// 第二步注册监听器消费对应的 Stream 主题
String consumerName = buildConsumerName();
// String consumerName = "110";
listeners.forEach(listener -> {
// 创建 listener 对应的消费者分组
try {

View File

@ -1,37 +1,39 @@
package cn.iocoder.yudao.framework.mq.core.util;
package cn.iocoder.yudao.framework.mq.core;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.stream.StreamMessage;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.RedisTemplate;
/**
* Redis 消息工具
* Redis MQ 操作模板
*
* @author 芋道源码
*/
public class RedisMessageUtils {
@AllArgsConstructor
public class RedisMQTemplate {
private final RedisTemplate<String, ?> redisTemplate;
/**
* 发送 Redis 消息基于 Redis pub/sub 实现
*
* @param redisTemplate Redis 操作模板
* @param message 消息
*/
public static <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T message) {
public <T extends AbstractChannelMessage> void send(T message) {
redisTemplate.convertAndSend(message.getChannel(), JsonUtils.toJsonString(message));
}
/**
* 发送 Redis 消息基于 Redis Stream 实现
*
* @param redisTemplate Redis 操作模板
* @param message 消息
* @return 消息记录的编号对象
*/
public static <T extends StreamMessage> RecordId sendStreamMessage(RedisTemplate<String, ?> redisTemplate, T message) {
public <T extends AbstractStreamMessage> RecordId send(T message) {
return redisTemplate.opsForStream().add(StreamRecords.newRecord()
.ofObject(JsonUtils.toJsonString(message)) // 设置内容
.withStreamKey(message.getStreamKey())); // 设置 stream key

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.framework.mq.core.message;
import java.util.HashMap;
import java.util.Map;
/**
* Redis 消息抽象基类
*
* @author 芋道源码
*/
public abstract class AbstractRedisMessage {
/**
*
*/
private final Map<String, String> headers = new HashMap<>();
public String getHeader(String key) {
return headers.get(key);
}
public void addHeader(String key, String value) {
headers.put(key, value);
}
}

View File

@ -1,13 +1,14 @@
package cn.iocoder.yudao.framework.mq.core.pubsub;
import cn.iocoder.yudao.framework.mq.core.message.AbstractRedisMessage;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Redis Channel Message 接口
* Redis Channel Message 抽象类
*
* @author 芋道源码
*/
public interface ChannelMessage {
public abstract class AbstractChannelMessage extends AbstractRedisMessage {
/**
* 获得 Redis Channel
@ -15,6 +16,6 @@ public interface ChannelMessage {
* @return Channel
*/
@JsonIgnore // 避免序列化
String getChannel();
public abstract String getChannel();
}

View File

@ -15,7 +15,7 @@ import java.lang.reflect.Type;
*
* @author 芋道源码
*/
public abstract class AbstractChannelMessageListener<T extends ChannelMessage> implements MessageListener {
public abstract class AbstractChannelMessageListener<T extends AbstractChannelMessage> implements MessageListener {
/**
* 消息类型

View File

@ -3,11 +3,11 @@ package cn.iocoder.yudao.framework.mq.core.stream;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Redis Stream Message 接口
* Redis Stream Message 抽象类
*
* @author 芋道源码
*/
public interface StreamMessage {
public abstract class AbstractStreamMessage {
/**
* 获得 Redis Stream Key
@ -15,6 +15,6 @@ public interface StreamMessage {
* @return Channel
*/
@JsonIgnore // 避免序列化
String getStreamKey();
public abstract String getStreamKey();
}

View File

@ -19,7 +19,7 @@ import java.lang.reflect.Type;
*
* @author 芋道源码
*/
public abstract class AbstractStreamMessageListener<T extends StreamMessage>
public abstract class AbstractStreamMessageListener<T extends AbstractStreamMessage>
implements StreamListener<String, ObjectRecord<String, String>> {
/**
@ -39,7 +39,7 @@ public abstract class AbstractStreamMessageListener<T extends StreamMessage>
@Getter
private String group;
/**
*
* RedisTemplate
*/
@Setter
private RedisTemplate<String, ?> redisTemplate;

View File

@ -4,5 +4,11 @@
* 2. Web请求 HTTP API Header 带上 tenant-id 租户编号
* 3. Job JobHandler 执行任务时会按照每个租户都独立并行执行一次
* 4. MQTODO
* 5. Async异步需要保证 ThreadLocal 的传递性通过使用阿里开源的 TransmittableThreadLocal 实现相关的改造点可见
* 1Spring Async
* {@link cn.iocoder.yudao.framework.quartz.config.YudaoAsyncAutoConfiguration#threadPoolTaskExecutorBeanPostProcessor()}
* 2Spring Security
* TransmittableThreadLocalSecurityContextHolderStrategy
* YudaoSecurityAutoConfiguration#securityContextHolderMethodInvokingFactoryBean() 方法
*/
package cn.iocoder.yudao.framework.tenant;