浏览代码

add 添加redis 并且使用 fastjson 序列化

GITZYY 9 月之前
父节点
当前提交
77b85860b2

+ 4 - 0
egress-gateway-service-infrastructure/pom.xml

@@ -54,5 +54,9 @@
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 0 - 1
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/business/HttpRequestCheckIntoService.java

@@ -13,7 +13,6 @@ import com.hrsk.cloud.eg.infrastructure.service.EgApiEndpointService;
 import com.hrsk.cloud.eg.infrastructure.service.EgApiService;
 import com.hrsk.cloud.eg.vo.response.DataVo;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.http.conn.ConnectTimeoutException;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;

+ 1 - 1
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/checkIntoApi/CheckIntoDisplayApi.java

@@ -4,7 +4,7 @@ package com.hrsk.cloud.eg.infrastructure.checkIntoApi;
 /**
  * @author zhangyy
  * @version 1.0
- * @description: CheckIntoDisplayApi
+ * @description: CheckIntoDisplayApi 直接对接api 的方法
  * @date 2024/9/10 10:21
  */
 public interface CheckIntoDisplayApi  {

+ 56 - 0
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/config/FastJson2RedisSerializer.java

@@ -0,0 +1,56 @@
+package com.hrsk.cloud.eg.infrastructure.config;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONReader;
+import com.alibaba.fastjson2.JSONWriter;
+import com.alibaba.fastjson2.filter.Filter;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ArrayUtils;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.data.redis.serializer.SerializationException;
+
+import java.util.Objects;
+
+@Slf4j
+public class FastJson2RedisSerializer<T> implements RedisSerializer<T> {
+
+    static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(
+            // 按需加上需要支持自动类型的类名前缀,范围越小越安全
+            "com.***.***"
+    );
+
+    private final Class<T> clazz;
+
+    public FastJson2RedisSerializer(Class<T> clazz) {
+        super();
+        this.clazz = clazz;
+    }
+
+    @Override
+    public byte[] serialize(T t) throws SerializationException {
+        if (Objects.isNull(t)) {
+            return new byte[0];
+        }
+        try {
+            return JSON.toJSONBytes(t, JSONWriter.Feature.WriteClassName);
+        } catch (Exception e) {
+            log.error("Fastjson2 序列化错误:{}", e.getMessage());
+            throw new SerializationException("无法序列化: " + e.getMessage(), e);
+        }
+
+    }
+
+    @Override
+    public T deserialize(byte[] bytes) throws SerializationException {
+        if (ArrayUtils.isEmpty(bytes)) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(bytes, clazz, AUTO_TYPE_FILTER);
+        } catch (Exception e) {
+            log.error("Fastjson2 反序列化错误:{}", e.getMessage());
+            throw new SerializationException("无法反序列化: " + e.getMessage(), e);
+        }
+
+    }
+}

+ 50 - 0
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/config/RedisConfig.java

@@ -0,0 +1,50 @@
+
+
+package com.hrsk.cloud.eg.infrastructure.config;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+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.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import javax.annotation.Resource;
+
+
+/**
+ * Redis配置
+ *
+ * @author yuanying
+ */
+@Configuration
+public class RedisConfig {
+
+    @Resource
+    private RedisConnectionFactory factory;
+
+    @Bean
+    public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(){
+        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
+        ObjectMapper objectMapper = new ObjectMapper();
+        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
+        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
+
+        return jackson2JsonRedisSerializer;
+    }
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate() {
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setKeySerializer(new StringRedisSerializer());
+        redisTemplate.setValueSerializer(new FastJson2RedisSerializer<Object>(Object.class));
+        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+        redisTemplate.setHashValueSerializer(new FastJson2RedisSerializer<Object>(Object.class));
+        redisTemplate.setConnectionFactory(factory);
+        return redisTemplate;
+    }
+}

+ 17 - 0
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/config/RedisKeys.java

@@ -0,0 +1,17 @@
+
+
+package com.hrsk.cloud.eg.infrastructure.config;
+
+/**
+ * @author yuanying
+ * @since 1.0.0
+ */
+public class RedisKeys {
+    /**
+     * 前缀
+     */
+    public static String getPrefixKey(){
+        return "eg:";
+    }
+
+}

+ 262 - 0
egress-gateway-service-infrastructure/src/main/java/com/hrsk/cloud/eg/infrastructure/utils/RedisUtil.java

@@ -0,0 +1,262 @@
+package com.hrsk.cloud.eg.infrastructure.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.redis.core.HashOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+
+/**
+ * @author zhangyy
+ * @version 1.0
+ * @description: Redisutil
+ * @date 2024/9/10 10:54
+ */
+@Component
+public class RedisUtil {
+
+    private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
+    @Resource
+    private RedisTemplate<String, Object> redisTemplate;
+
+    /**  默认过期时长为24小时,单位:秒 */
+    public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;
+    /**  过期时长为1小时,单位:秒 */
+    public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
+    /**  过期时长为6小时,单位:秒 */
+    public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
+    /**  不设置过期时长 */
+    public final static long NOT_EXPIRE = -1L;
+
+    /**
+     * 添加值
+     * @param key
+     * @param value
+     * @param expire
+     */
+    public void set(String key, Object value, long expire) {
+        redisTemplate.opsForValue().set(key, value,expire, TimeUnit.MINUTES);
+        if (expire != NOT_EXPIRE) {
+            expire(key, expire);
+        }
+    }
+    /**
+     * 添加值
+     * @param key
+     * @param value
+     * @param
+     */
+    public void set(String key, Object value) {
+        set(key, value, DEFAULT_EXPIRE);
+    }
+
+    /**
+     * 获取值
+     * @param key
+     * @param
+     * @param
+     */
+    public Object get(String key, long expire) {
+        Object value = redisTemplate.opsForValue().get(key);
+        if (expire != NOT_EXPIRE) {
+            expire(key, expire);
+        }
+        return value;
+    }
+    /**
+     * 获取值
+     * @param key
+     * @param
+     * @param
+     */
+    public Object get(String key) {
+        return get(key, NOT_EXPIRE);
+    }
+    /**
+     * 删除key
+     * @param key
+     * @param
+     * @param
+     */
+    public void delete(String key) {
+        redisTemplate.delete(key);
+    }
+    /**
+     * 删除key
+     * @param
+     * @param
+     * @param
+     */
+    public void delete(Collection<String> keys) {
+        redisTemplate.delete(keys);
+    }
+
+    /**
+     * 获取map中某个key的数据
+     * @param
+     * @param
+     * @param
+     */
+    public Object hGet(String key, String field) {
+        return redisTemplate.opsForHash().get(key, field);
+    }
+    /**
+     * 获取map中的数据
+     * @param
+     * @param
+     * @param
+     */
+    public Map<String, Object> hGetAll(String key) {
+        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
+        return hashOperations.entries(key);
+    }
+
+    /**
+     * 添加map中的数据
+     * @param
+     * @param
+     * @param
+     */
+    public void hMSet(String key, Map<String, Object> map) {
+        hMSet(key, map, DEFAULT_EXPIRE);
+    }
+
+    /**
+     * 添加整个map中的数据
+     * @param
+     * @param
+     * @param
+     */
+    public void hMSet(String key, Map<String, Object> map, long expire) {
+        redisTemplate.opsForHash().putAll(key, map);
+
+        if (expire != NOT_EXPIRE) {
+            expire(key, expire);
+        }
+    }
+    /**
+     * 添加set中的数据
+     * @param
+     * @param
+     * @param
+     */
+    public void hSet(String key, String field, Object value) {
+        hSet(key, field, value, DEFAULT_EXPIRE);
+    }
+
+    /**
+     * 添加set中的数据
+     * @param
+     * @param
+     * @param
+     */
+    public void hSet(String key, String field, Object value, long expire) {
+        redisTemplate.opsForHash().put(key, field, value);
+
+        if (expire != NOT_EXPIRE) {
+            expire(key, expire);
+        }
+    }
+
+    /**
+     * @description: 查看是否过期
+     * @param:
+     * @return:
+     * @author zhangyy
+     * @date: 2024/9/10 13:38
+     */
+    public void expire(String key, long expire) {
+        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
+    }
+
+    /** 
+     * @description: 删除hash 
+     * @param:  
+     * @return:  
+     * @author zhangyy
+     * @date: 2024/9/10 13:40
+     */ 
+    public void hDel(String key, Object... fields) {
+        redisTemplate.opsForHash().delete(key, fields);
+    }
+
+    /** 
+     * @description: 队列推送
+     * @param:  
+     * @return:  
+     * @author zhangyy
+     * @date: 2024/9/10 13:40
+     */ 
+    public void leftPush(String key, Object value) {
+        leftPush(key, value, DEFAULT_EXPIRE);
+    }
+
+    /** 
+     * @description: 队列推送 
+     * @param:  
+     * @return:  
+     * @author zhangyy
+     * @date: 2024/9/10 13:40
+     */ 
+    public void leftPush(String key, Object value, long expire) {
+        redisTemplate.opsForList().leftPush(key, value);
+
+        if (expire != NOT_EXPIRE) {
+            expire(key, expire);
+        }
+    }
+
+    /** 
+     * @description: 队列弹出
+     * @param:  
+     * @return:  
+     * @author zhangyy
+     * @date: 2024/9/10 13:44
+     */ 
+    public Object rightPop(String key) {
+        return redisTemplate.opsForList().rightPop(key);
+    }
+
+
+    /**
+     * @description: 是否存在redisKey
+     * @param:
+     * @return:
+     * @author zhangyy
+     * @date: 2024/6/30 14:58
+     */
+    public Boolean getHashKey(String key){
+        try {
+            if (redisTemplate.hasKey(key)){
+                return true;
+            }
+            return false;
+        }catch (Exception e){
+            log.error("判断出现错误",e);
+            return false;
+        }
+
+    }
+    /**
+     * @description: 删除key
+     * @param:
+     * @return:
+     * @author zhangyy
+     * @date: 2024/7/8 21:51
+     */
+    public Boolean removeKey(String key){
+        Boolean delete = redisTemplate.delete(key);
+        if (delete){
+            return  true;
+        }
+        return false;
+    }
+
+
+}

+ 6 - 18
pom.xml

@@ -27,9 +27,9 @@
         <mybatis-plus-generator.version>3.5.1</mybatis-plus-generator.version>
         <fast2json.version>2.0.7</fast2json.version>
         <guva.version>33.2.1-jre</guva.version>
-        <redis.version>3.2.6</redis.version>
         <jedis.version>4.2.2</jedis.version>
         <jetcache.version>2.5.14</jetcache.version>
+        <redis.version>2.7.6</redis.version>
     </properties>
 
     <dependencies>
@@ -86,17 +86,6 @@
                 <scope>import</scope>
             </dependency>
             <!-- 应用模块依赖-end -->
-            <dependency>
-                <groupId>org.apache.commons</groupId>
-                <artifactId>commons-lang3</artifactId>
-                <version>${commons-lang3.version}</version>
-                <exclusions>
-                    <exclusion>
-                        <groupId>redis.clients</groupId>
-                        <artifactId>jedis</artifactId>
-                    </exclusion>
-                </exclusions>
-            </dependency>
             <dependency>
                 <groupId>commons-codec</groupId>
                 <artifactId>commons-codec</artifactId>
@@ -140,12 +129,6 @@
                 <groupId>org.apache.commons</groupId>
                 <artifactId>commons-pool2</artifactId>
                 <version>${common-pools.version}</version>
-                <exclusions>
-                    <exclusion>
-                        <groupId>redis.clients</groupId>
-                        <artifactId>jedis</artifactId>
-                    </exclusion>
-                </exclusions>
             </dependency>
             <dependency>
                 <groupId>com.alibaba.fastjson2</groupId>
@@ -157,6 +140,11 @@
                 <artifactId>guava</artifactId>
                 <version>${guva.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-starter-data-redis</artifactId>
+                <version>${redis.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 

+ 11 - 11
start/src/main/resources/application-dev.yaml

@@ -25,17 +25,17 @@ spring:
       poolPreparedStatements: true
       maxOpenPreparedStatements: 20
       logSlowSql: true
-#  redis:
-#    port: 6379
-#    password:
-#    host: 192.168.1.169
-#    database: 0
-#    lettuce:
-#      pool:
-#        max-active: 8
-#        max-idle: 8
-#        min-idle: 5
-#    timeout: 3
+  redis:
+    port: 6379
+    password:
+    host: 192.168.1.169
+    database: 0
+    lettuce:
+      pool:
+        max-active: 8
+        max-idle: 8
+        min-idle: 5
+    timeout: 3
 
 #jetcache:
 #  statIntervalMinutes: 15 # 指定统计间隔,以分钟为单位。0表示没有统计数据。

+ 14 - 12
start/src/main/resources/application-pre.yaml

@@ -20,18 +20,20 @@ spring:
       poolPreparedStatements: true
       maxOpenPreparedStatements: 20
       logSlowSql: true
-#  redis:
-#    timeout: 3
-#    database: 8
-#    host: 47.96.139.31
-#    port: 6380
-#    lettuce:
-#      pool:
-#        min-idle: 50
-#        max-idle: 50
-#        max-active: 100
-#        max-wait: -1
-#    password: IICdQIBA#DANB%gkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK8f%
+  redis:
+
+    database: 8
+    host: 47.96.139.31
+    port: 6380
+    password: IICdQIBA#DANB%gkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK8f%
+    lettuce:
+      pool:
+        min-idle: 50
+        max-idle: 50
+        max-active: 100
+        max-wait: -1
+    timeout: 500
+