From 2a349972b4768eab72c559ba9750008c3852ceee Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 22 Jan 2021 21:04:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Redis=20pub=20sub=20?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/redis/config/RedisConfig.java | 26 +++++++++++++++++++ .../framework/redis/core/RedisKeyDefine.java | 2 -- .../listener/AbstractMessageListener.java | 19 ++++++++++++++ .../mq/consumer/SysMenuRefreshConsumer.java | 21 +++++++++++++++ .../system/mq/message/package-info.java | 1 + .../permission/SysMenuRefreshMessage.java | 13 ++++++++++ .../system/mq/producer/package-info.java | 1 + .../producer/permission/SysMenuProducer.java | 26 +++++++++++++++++++ .../permission/impl/SysMenuServiceImpl.java | 6 +++++ 9 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/redis/core/listener/AbstractMessageListener.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/SysMenuRefreshConsumer.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/mq/message/package-info.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/mq/message/permission/SysMenuRefreshMessage.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/package-info.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/permission/SysMenuProducer.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java b/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java index 8f6de1469..239860bdc 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java @@ -1,13 +1,23 @@ package cn.iocoder.dashboard.framework.redis.config; +import cn.iocoder.dashboard.framework.redis.core.listener.AbstractMessageListener; import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; +import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.listener.ChannelTopic; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.serializer.RedisSerializer; +import java.util.List; + +/** + * Redis 配置类 + */ @Configuration +@Slf4j public class RedisConfig { @Bean @@ -23,4 +33,20 @@ public class RedisConfig { return template; } + @Bean + public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory, + List> listeners) { + // 创建 RedisMessageListenerContainer 对象 + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + // 设置 RedisConnection 工厂。 + container.setConnectionFactory(factory); + // 添加监听器 + listeners.forEach(listener -> { + container.addMessageListener(listener, new ChannelTopic(listener.getChannel())); + log.info("[redisMessageListenerContainer][注册 Channel({}) 对应的监听器({})]", + listener.getChannel(), listener.getClass().getName()); + }); + return container; + } + } diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java b/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java index ffae0ec46..a126f623a 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java @@ -19,8 +19,6 @@ public class RedisKeyDefine { HASH, SET, ZSET, - STREAM, - PUBSUB; } diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/core/listener/AbstractMessageListener.java b/src/main/java/cn/iocoder/dashboard/framework/redis/core/listener/AbstractMessageListener.java new file mode 100644 index 000000000..d0f199c7b --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/core/listener/AbstractMessageListener.java @@ -0,0 +1,19 @@ +package cn.iocoder.dashboard.framework.redis.core.listener; + +import org.springframework.data.redis.connection.MessageListener; + +/** + * Redis Pub/Sub 监听器抽象类,用于实现广播消费 + * + * @author 芋道源码 + */ +public abstract class AbstractMessageListener implements MessageListener { + + /** + * 获得 Sub 订阅的 Redis Channel 通道 + * + * @return channel + */ + public abstract String getChannel(); + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/SysMenuRefreshConsumer.java b/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/SysMenuRefreshConsumer.java new file mode 100644 index 000000000..c252cdd7d --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/SysMenuRefreshConsumer.java @@ -0,0 +1,21 @@ +package cn.iocoder.dashboard.modules.system.mq.consumer; + +import cn.iocoder.dashboard.framework.redis.core.listener.AbstractMessageListener; +import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage; +import org.springframework.data.redis.connection.Message; +import org.springframework.stereotype.Component; + +@Component +public class SysMenuRefreshConsumer extends AbstractMessageListener { + + @Override + public void onMessage(Message message, byte[] bytes) { + System.out.println(message); + } + + @Override + public String getChannel() { + return SysMenuRefreshMessage.TOPIC; + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/mq/message/package-info.java b/src/main/java/cn/iocoder/dashboard/modules/system/mq/message/package-info.java new file mode 100644 index 000000000..1096439af --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/mq/message/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.dashboard.modules.system.mq.message; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/mq/message/permission/SysMenuRefreshMessage.java b/src/main/java/cn/iocoder/dashboard/modules/system/mq/message/permission/SysMenuRefreshMessage.java new file mode 100644 index 000000000..17c4ef82b --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/mq/message/permission/SysMenuRefreshMessage.java @@ -0,0 +1,13 @@ +package cn.iocoder.dashboard.modules.system.mq.message.permission; + +import lombok.Data; + +/** + * 菜单数据刷新 Message + */ +@Data +public class SysMenuRefreshMessage { + + public static final String TOPIC = "system.menu.refresh"; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/package-info.java b/src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/package-info.java new file mode 100644 index 000000000..e16b5cada --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.dashboard.modules.system.mq.producer; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/permission/SysMenuProducer.java b/src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/permission/SysMenuProducer.java new file mode 100644 index 000000000..c3450f8c5 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/mq/producer/permission/SysMenuProducer.java @@ -0,0 +1,26 @@ +package cn.iocoder.dashboard.modules.system.mq.producer.permission; + +import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * Menu 菜单相关消息的 Producer + */ +@Component +public class SysMenuProducer { + + @Resource + private RedisTemplate redisTemplate; + + /** + * 发送 {@link SysMenuRefreshMessage} 消息 + */ + public void sendMenuRefreshMessage() { + SysMenuRefreshMessage message = new SysMenuRefreshMessage(); + redisTemplate.convertAndSend(SysMenuRefreshMessage.TOPIC, message); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java index ba611836e..92d228faa 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java @@ -11,6 +11,7 @@ import cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission.SysMenuMappe import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; import cn.iocoder.dashboard.modules.system.enums.permission.MenuIdEnum; import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum; +import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProducer; import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService; import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; import cn.iocoder.dashboard.util.collection.CollectionUtils; @@ -68,6 +69,9 @@ public class SysMenuServiceImpl implements SysMenuService { @Resource private SysPermissionService permissionService; + @Resource + private SysMenuProducer menuProducer; + /** * 初始化 {@link #menuCache} 和 {@link #permMenuCache} 缓存 */ @@ -183,6 +187,8 @@ public class SysMenuServiceImpl implements SysMenuService { SysMenuDO updateObject = SysMenuConvert.INSTANCE.convert(reqVO); initMenuProperty(updateObject); menuMapper.updateById(updateObject); + // 发送刷新消息 + menuProducer.sendMenuRefreshMessage(); } /**