tanlie vor 1 Jahr
Ursprung
Commit
8735235f22

+ 6 - 0
demo/pom.xml

@@ -59,6 +59,12 @@
 			<artifactId>commons-lang3</artifactId>
 			<version>3.17.0</version>
 		</dependency>
+		<dependency>
+			<groupId>com.alibaba.fastjson2</groupId>
+			<artifactId>fastjson2</artifactId>
+			<version>2.0.53</version>
+		</dependency>
+
 	</dependencies>
 
 	<build>

+ 16 - 0
demo/src/main/java/com/example/demo/config/redis/RedisConfig.java

@@ -11,6 +11,8 @@ import org.springframework.data.redis.cache.RedisCacheManager;
 import org.springframework.data.redis.cache.RedisCacheWriter;
 import org.springframework.data.redis.connection.RedisConnectionFactory;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.listener.PatternTopic;
+import org.springframework.data.redis.listener.RedisMessageListenerContainer;
 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
 import org.springframework.data.redis.serializer.RedisSerializationContext;
 import org.springframework.data.redis.serializer.RedisSerializer;
@@ -80,4 +82,18 @@ public class RedisConfig {
         template.afterPropertiesSet();
         return template;
     }
+
+
+    @Bean
+    public RedisMessageListenerContainer container(RedisConnectionFactory factory){
+        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
+        container.setConnectionFactory(factory);
+
+        //订阅频道,通配符*表示任意多个占位符
+        container.addMessageListener(new RedisSubscribe(), new PatternTopic(RedisKeyEnum.ACCESS_LOGS.getKey()));
+
+        return container;
+
+    }
+
 }

+ 4 - 19
demo/src/main/java/com/example/demo/config/redis/RedisKeyEnum.java

@@ -11,30 +11,15 @@ import lombok.Getter;
 @Getter
 public enum RedisKeyEnum {
 
-    //登录相关
-    CAPTCHA("captcha:", "验证码", 300),
+    ACCESS_LOGS("ACCESS_LOGS", "请求记录", 7200);
 
-    CURRENT_USER("current_user:", "当前用户", 3600 * 12),
-    CURRENT_USER_TOKEN("current_user_token:", "当前用户token", 3600 * 12),
-
-    LOGIN_FAIL("login_fail:", "登录错误尝试", 3600 * 24),
-    DICTIONARY("dictionary", "字典", 2 * 3600),
-
-    // 行销行政编码
-    MARKET_MAP_USER_INFO("MARKET_MAP_USER_INFO:", "行销工具用户信息", 600),
-    MARKET_MAP_NAME_BY_CODES("MARKET_MAP_NAME_BY_CODES:", "行销行政编码", 3600 * 7 * 24),
-
-    // 验证码相关
-    SMS_COUNT("sms_count:", "每半个小时发送的短信数量", 1800),
-    SMS_VALIDATE("sms_validate:", "短信验证码", 300);
-
-    RedisKeyEnum(String code, String msg, Integer expireTime) {
-        this.code = code;
+    RedisKeyEnum(String key, String msg, Integer expireTime) {
+        this.key = key;
         this.msg = msg;
         this.expireTime = expireTime;
     }
 
-    private final String code;
+    private final String key;
     private final String msg;
     private final Integer expireTime;
 }

+ 31 - 0
demo/src/main/java/com/example/demo/config/redis/RedisSubscribe.java

@@ -0,0 +1,31 @@
+package com.example.demo.config.redis;
+
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.redis.connection.Message;
+import org.springframework.data.redis.connection.MessageListener;
+
+/**
+ * Description:
+ * Auth: tanlie
+ * Date: 2023/8/30 16:06
+ */
+public class RedisSubscribe implements MessageListener {
+    Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    /**
+     * 发布订阅多个客户端并行时,可能会产生重复消费,所以要注意加锁
+     *
+     * @param message message must not be {@literal null}.
+     * @param bytes   pattern matching the channel (if specified) - can be {@literal null}.
+     */
+    @Override
+    public void onMessage(Message message, byte[] bytes) {
+        String msg = new String(message.getBody());
+        JSONObject json = JSON.parseObject(msg);
+        logger.info("收到订阅发布信息={}", json);
+    }
+}

+ 15 - 0
demo/src/test/java/com/example/demo/config/redis/RedisConfigTest.java

@@ -1,5 +1,6 @@
 package com.example.demo.config.redis;
 
+import com.alibaba.fastjson2.JSON;
 import jakarta.annotation.Resource;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -91,4 +92,18 @@ class RedisConfigTest {
 
     }
 
+    //发布订阅
+    // 发布订阅多个客户端并行时,可能会产生重复消费
+    @Test
+    void convertAndSendTest(){
+        Map<String, String> map = new HashMap<>();
+        map.put("key1", "value1");
+        map.put("key2", "value2");
+        map.put("key3", "value3");
+        redisTemplate.convertAndSend(RedisKeyEnum.ACCESS_LOGS.getKey(), JSON.toJSONString(map));
+    }
+
+
+
+
 }