linxk 1 день назад
Родитель
Сommit
5d5ccddee2

+ 34 - 34
operating-service/src/main/java/com/txz/operating/configurer/RedisConfig.java

@@ -1,34 +1,34 @@
-//package com.txz.operating.configurer;
-//
-//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.GenericJackson2JsonRedisSerializer;
-//import org.springframework.data.redis.serializer.StringRedisSerializer;
-//
-//@Configuration
-//public class RedisConfig {
-//
-//    @Bean
-//    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
-//        // 1. 创建 RedisTemplate 实例
-//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
-//        redisTemplate.setConnectionFactory(connectionFactory);
-//
-//        // 2. 配置 key 序列化器(String 格式,避免乱码)
-//        StringRedisSerializer stringSerializer = new StringRedisSerializer();
-//        redisTemplate.setKeySerializer(stringSerializer);         // 普通 key 序列化
-//        redisTemplate.setHashKeySerializer(stringSerializer);     // Hash key 序列化
-//
-//        // 3. 配置 value 序列化器(JSON 格式,支持对象自动转换)
-//        GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
-//        redisTemplate.setValueSerializer(jsonSerializer);         // 普通 value 序列化
-//        redisTemplate.setHashValueSerializer(jsonSerializer);     // Hash value 序列化
-//
-//        // 4. 初始化配置
-//        redisTemplate.afterPropertiesSet();
-//        return redisTemplate;
-//    }
-//
-//}
+package com.txz.operating.configurer;
+
+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.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+@Configuration
+public class RedisConfig {
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
+        // 1. 创建 RedisTemplate 实例
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory(connectionFactory);
+
+        // 2. 配置 key 序列化器(String 格式,避免乱码)
+        StringRedisSerializer stringSerializer = new StringRedisSerializer();
+        redisTemplate.setKeySerializer(stringSerializer);         // 普通 key 序列化
+        redisTemplate.setHashKeySerializer(stringSerializer);     // Hash key 序列化
+
+        // 3. 配置 value 序列化器(JSON 格式,支持对象自动转换)
+        GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
+        redisTemplate.setValueSerializer(jsonSerializer);         // 普通 value 序列化
+        redisTemplate.setHashValueSerializer(jsonSerializer);     // Hash value 序列化
+
+        // 4. 初始化配置
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
+
+}

+ 35 - 35
operating-service/src/main/java/com/txz/operating/core/cache/lock/AbstractDistributedLock.java

@@ -1,35 +1,35 @@
-package com.txz.operating.core.cache.lock;
-
-/**
- * 分布式锁抽象类
- *
- * @author zlt
- * @date 2018/5/29 14:14
- */
-public abstract class AbstractDistributedLock implements DistributedLock{
-
-    @Override
-    public boolean lock(String key) {
-        return lock(key, TIMEOUT_MILLIS, RETRY_TIMES, SLEEP_MILLIS);
-    }
-
-    @Override
-    public boolean lock(String key, int retryTimes) {
-        return lock(key, TIMEOUT_MILLIS, retryTimes, SLEEP_MILLIS);
-    }
-
-    @Override
-    public boolean lock(String key, int retryTimes, long sleepMillis) {
-        return lock(key, TIMEOUT_MILLIS, retryTimes, sleepMillis);
-    }
-
-    @Override
-    public boolean lock(String key, long expire) {
-        return lock(key, expire, RETRY_TIMES, SLEEP_MILLIS);
-    }
-
-    @Override
-    public boolean lock(String key, long expire, int retryTimes) {
-        return lock(key, expire, retryTimes, SLEEP_MILLIS);
-    }
-}
+//package com.txz.operating.core.cache.lock;
+//
+///**
+// * 分布式锁抽象类
+// *
+// * @author zlt
+// * @date 2018/5/29 14:14
+// */
+//public abstract class AbstractDistributedLock implements DistributedLock{
+//
+//    @Override
+//    public boolean lock(String key) {
+//        return lock(key, TIMEOUT_MILLIS, RETRY_TIMES, SLEEP_MILLIS);
+//    }
+//
+//    @Override
+//    public boolean lock(String key, int retryTimes) {
+//        return lock(key, TIMEOUT_MILLIS, retryTimes, SLEEP_MILLIS);
+//    }
+//
+//    @Override
+//    public boolean lock(String key, int retryTimes, long sleepMillis) {
+//        return lock(key, TIMEOUT_MILLIS, retryTimes, sleepMillis);
+//    }
+//
+//    @Override
+//    public boolean lock(String key, long expire) {
+//        return lock(key, expire, RETRY_TIMES, SLEEP_MILLIS);
+//    }
+//
+//    @Override
+//    public boolean lock(String key, long expire, int retryTimes) {
+//        return lock(key, expire, retryTimes, SLEEP_MILLIS);
+//    }
+//}

+ 93 - 93
operating-service/src/main/java/com/txz/operating/core/cache/lock/DistributedLock.java

@@ -1,93 +1,93 @@
-package com.txz.operating.core.cache.lock;
-
-/**
- * 分布式锁顶级接口
- * 例如:
- * RETRY_TIMES=100,SLEEP_MILLIS=100
- * RETRY_TIMES * SLEEP_MILLIS = 10000 意味着如果一直获取不了锁,最长会等待10秒后抛超时异常
- *
- * @author zlt
- * @date 2018/5/29 14:12
- */
-public interface DistributedLock {
-
-    /**
-     * 默认超时时间
-     */
-    long TIMEOUT_MILLIS = 5000;
-
-    /**
-     * 重试次数
-     */
-    int RETRY_TIMES = 100;
-
-    /**
-     * 每次重试后等待的时间
-     */
-    long SLEEP_MILLIS = 100;
-
-    /**
-     * 获取锁
-     *
-     * @param key key
-     * @return 成功/失败
-     */
-    boolean lock(String key);
-
-    /**
-     * 获取锁
-     *
-     * @param key        key
-     * @param retryTimes 重试次数
-     * @return 成功/失败
-     */
-    boolean lock(String key, int retryTimes);
-
-    /**
-     * 获取锁
-     *
-     * @param key         key
-     * @param retryTimes  重试次数
-     * @param sleepMillis 获取锁失败的重试间隔
-     * @return 成功/失败
-     */
-    boolean lock(String key, int retryTimes, long sleepMillis);
-
-    /**
-     * 获取锁
-     *
-     * @param key    key
-     * @param expire 获取锁超时时间
-     * @return 成功/失败
-     */
-    boolean lock(String key, long expire);
-
-    /**
-     * 获取锁
-     *
-     * @param key        key
-     * @param expire     获取锁超时时间
-     * @param retryTimes 重试次数
-     * @return 成功/失败
-     */
-    boolean lock(String key, long expire, int retryTimes);
-
-    /**
-     * 获取锁
-     *
-     * @param key         key
-     * @param expire      获取锁超时时间
-     * @param retryTimes  重试次数
-     * @param sleepMillis 获取锁失败的重试间隔
-     * @return 成功/失败
-     */
-    boolean lock(String key, long expire, int retryTimes, long sleepMillis);
-
-    /**
-     * 释放锁
-     *
-     * @param key key值
-     * @return 释放结果
-     */
-    boolean releaseLock(String key);
-}
+//package com.txz.operating.core.cache.lock;
+//
+///**
+// * 分布式锁顶级接口
+// * 例如:
+// * RETRY_TIMES=100,SLEEP_MILLIS=100
+// * RETRY_TIMES * SLEEP_MILLIS = 10000 意味着如果一直获取不了锁,最长会等待10秒后抛超时异常
+// *
+// * @author zlt
+// * @date 2018/5/29 14:12
+// */
+//public interface DistributedLock {
+//
+//    /**
+//     * 默认超时时间
+//     */
+//    long TIMEOUT_MILLIS = 5000;
+//
+//    /**
+//     * 重试次数
+//     */
+//    int RETRY_TIMES = 100;
+//
+//    /**
+//     * 每次重试后等待的时间
+//     */
+//    long SLEEP_MILLIS = 100;
+//
+//    /**
+//     * 获取锁
+//     *
+//     * @param key key
+//     * @return 成功/失败
+//     */
+//    boolean lock(String key);
+//
+//    /**
+//     * 获取锁
+//     *
+//     * @param key        key
+//     * @param retryTimes 重试次数
+//     * @return 成功/失败
+//     */
+//    boolean lock(String key, int retryTimes);
+//
+//    /**
+//     * 获取锁
+//     *
+//     * @param key         key
+//     * @param retryTimes  重试次数
+//     * @param sleepMillis 获取锁失败的重试间隔
+//     * @return 成功/失败
+//     */
+//    boolean lock(String key, int retryTimes, long sleepMillis);
+//
+//    /**
+//     * 获取锁
+//     *
+//     * @param key    key
+//     * @param expire 获取锁超时时间
+//     * @return 成功/失败
+//     */
+//    boolean lock(String key, long expire);
+//
+//    /**
+//     * 获取锁
+//     *
+//     * @param key        key
+//     * @param expire     获取锁超时时间
+//     * @param retryTimes 重试次数
+//     * @return 成功/失败
+//     */
+//    boolean lock(String key, long expire, int retryTimes);
+//
+//    /**
+//     * 获取锁
+//     *
+//     * @param key         key
+//     * @param expire      获取锁超时时间
+//     * @param retryTimes  重试次数
+//     * @param sleepMillis 获取锁失败的重试间隔
+//     * @return 成功/失败
+//     */
+//    boolean lock(String key, long expire, int retryTimes, long sleepMillis);
+//
+//    /**
+//     * 释放锁
+//     *
+//     * @param key key值
+//     * @return 释放结果
+//     */
+//    boolean releaseLock(String key);
+//}

+ 101 - 101
operating-service/src/main/java/com/txz/operating/core/cache/lock/RedisDistributedLock.java

@@ -1,101 +1,101 @@
-package com.txz.operating.core.cache.lock;
-
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.data.redis.connection.RedisStringCommands;
-import org.springframework.data.redis.connection.ReturnType;
-import org.springframework.data.redis.core.RedisCallback;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.core.types.Expiration;
-import org.springframework.stereotype.Component;
-
-import java.util.UUID;
-import java.util.concurrent.TimeUnit;
-
-/**
- * redis分布式锁实现
- *
- * @author zlt
- * @date 2018/5/29 14:16
- */
-@Slf4j
-@Component
-public class RedisDistributedLock extends AbstractDistributedLock{
-    public RedisDistributedLock(RedisTemplate redisTemplate) {
-        this.redisTemplate = redisTemplate;
-    }
-
-    private RedisTemplate<String, Object> redisTemplate;
-
-    private ThreadLocal<String> lockFlag = new ThreadLocal<>();
-
-    private static final String UNLOCK_LUA;
-
-    /*
-     * 通过lua脚本释放锁,来达到释放锁的原子操作
-     */
-    static {
-        UNLOCK_LUA = "if redis.call(\"get\",KEYS[1]) == ARGV[1] " +
-                "then " +
-                "    return redis.call(\"del\",KEYS[1]) " +
-                "else " +
-                "    return 0 " +
-                "end ";
-    }
-
-    @Override
-    public boolean lock(String key, long expire, int retryTimes, long sleepMillis) {
-        boolean result = setRedis(key, expire);
-        // 如果获取锁失败,按照传入的重试次数进行重试
-        while ((!result) && retryTimes-- > 0) {
-            try {
-                log.debug("get redisDistributeLock failed, retrying..." + retryTimes);
-                Thread.sleep(sleepMillis);
-            } catch (InterruptedException e) {
-                log.warn("Interrupted!", e);
-                Thread.currentThread().interrupt();
-            }
-            result = setRedis(key, expire);
-        }
-        return result;
-    }
-
-    @Override
-    public boolean releaseLock(String key) {
-        // 释放锁的时候,有可能因为持锁之后方法执行时间大于锁的有效期,此时有可能已经被另外一个线程持有锁,所以不能直接删除
-        try {
-            // 使用lua脚本删除redis中匹配value的key,可以避免由于方法执行时间过长而redis锁自动过期失效的时候误删其他线程的锁
-            // spring自带的执行脚本方法中,集群模式直接抛出不支持执行脚本的异常,所以只能拿到原redis的connection来执行脚本
-            Boolean result = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
-                byte[] scriptByte = redisTemplate.getStringSerializer().serialize(UNLOCK_LUA);
-                return connection.eval(scriptByte, ReturnType.BOOLEAN, 1
-                        , redisTemplate.getStringSerializer().serialize(key)
-                        , redisTemplate.getStringSerializer().serialize(lockFlag.get()));
-            });
-            return result;
-        } catch (Exception e) {
-            log.error("release redisDistributeLock occured an exception", e);
-        } finally {
-            lockFlag.remove();
-        }
-        return false;
-    }
-
-
-    private boolean setRedis(final String key, final long expire) {
-        try {
-            boolean status = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
-                String uuid = UUID.randomUUID().toString();
-                lockFlag.set(uuid);
-                byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);
-                byte[] uuidByte = redisTemplate.getStringSerializer().serialize(uuid);
-
-                boolean result = connection.set(keyByte, uuidByte, Expiration.from(expire, TimeUnit.MILLISECONDS), RedisStringCommands.SetOption.ifAbsent());
-                return result;
-            });
-            return status;
-        } catch (Exception e) {
-            log.error("set redisDistributeLock occured an exception", e);
-        }
-        return false;
-    }
-}
+//package com.txz.operating.core.cache.lock;
+//
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.data.redis.connection.RedisStringCommands;
+//import org.springframework.data.redis.connection.ReturnType;
+//import org.springframework.data.redis.core.RedisCallback;
+//import org.springframework.data.redis.core.RedisTemplate;
+//import org.springframework.data.redis.core.types.Expiration;
+//import org.springframework.stereotype.Component;
+//
+//import java.util.UUID;
+//import java.util.concurrent.TimeUnit;
+//
+///**
+// * redis分布式锁实现
+// *
+// * @author zlt
+// * @date 2018/5/29 14:16
+// */
+//@Slf4j
+//@Component
+//public class RedisDistributedLock extends AbstractDistributedLock{
+//    public RedisDistributedLock(RedisTemplate redisTemplate) {
+//        this.redisTemplate = redisTemplate;
+//    }
+//
+//    private RedisTemplate<String, Object> redisTemplate;
+//
+//    private ThreadLocal<String> lockFlag = new ThreadLocal<>();
+//
+//    private static final String UNLOCK_LUA;
+//
+//    /*
+//     * 通过lua脚本释放锁,来达到释放锁的原子操作
+//     */
+//    static {
+//        UNLOCK_LUA = "if redis.call(\"get\",KEYS[1]) == ARGV[1] " +
+//                "then " +
+//                "    return redis.call(\"del\",KEYS[1]) " +
+//                "else " +
+//                "    return 0 " +
+//                "end ";
+//    }
+//
+//    @Override
+//    public boolean lock(String key, long expire, int retryTimes, long sleepMillis) {
+//        boolean result = setRedis(key, expire);
+//        // 如果获取锁失败,按照传入的重试次数进行重试
+//        while ((!result) && retryTimes-- > 0) {
+//            try {
+//                log.debug("get redisDistributeLock failed, retrying..." + retryTimes);
+//                Thread.sleep(sleepMillis);
+//            } catch (InterruptedException e) {
+//                log.warn("Interrupted!", e);
+//                Thread.currentThread().interrupt();
+//            }
+//            result = setRedis(key, expire);
+//        }
+//        return result;
+//    }
+//
+//    @Override
+//    public boolean releaseLock(String key) {
+//        // 释放锁的时候,有可能因为持锁之后方法执行时间大于锁的有效期,此时有可能已经被另外一个线程持有锁,所以不能直接删除
+//        try {
+//            // 使用lua脚本删除redis中匹配value的key,可以避免由于方法执行时间过长而redis锁自动过期失效的时候误删其他线程的锁
+//            // spring自带的执行脚本方法中,集群模式直接抛出不支持执行脚本的异常,所以只能拿到原redis的connection来执行脚本
+//            Boolean result = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
+//                byte[] scriptByte = redisTemplate.getStringSerializer().serialize(UNLOCK_LUA);
+//                return connection.eval(scriptByte, ReturnType.BOOLEAN, 1
+//                        , redisTemplate.getStringSerializer().serialize(key)
+//                        , redisTemplate.getStringSerializer().serialize(lockFlag.get()));
+//            });
+//            return result;
+//        } catch (Exception e) {
+//            log.error("release redisDistributeLock occured an exception", e);
+//        } finally {
+//            lockFlag.remove();
+//        }
+//        return false;
+//    }
+//
+//
+//    private boolean setRedis(final String key, final long expire) {
+//        try {
+//            boolean status = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
+//                String uuid = UUID.randomUUID().toString();
+//                lockFlag.set(uuid);
+//                byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);
+//                byte[] uuidByte = redisTemplate.getStringSerializer().serialize(uuid);
+//
+//                boolean result = connection.set(keyByte, uuidByte, Expiration.from(expire, TimeUnit.MILLISECONDS), RedisStringCommands.SetOption.ifAbsent());
+//                return result;
+//            });
+//            return status;
+//        } catch (Exception e) {
+//            log.error("set redisDistributeLock occured an exception", e);
+//        }
+//        return false;
+//    }
+//}