wei 5 lat temu
rodzic
commit
a2227bf4f8
15 zmienionych plików z 94 dodań i 53 usunięć
  1. 1 1
      base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/config/MybatisPlusConfig.java
  2. 1 1
      base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/constants/GeneratorParam.java
  3. 1 1
      base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/env/InitializeCustomConfiguration.java
  4. 2 2
      base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/generator/Generator.java
  5. 2 2
      base-springframework/base-springframework-mysql/src/main/resources/META-INF/spring.factories
  6. 24 0
      base-springframework/base-springframework-redis/src/main/java/com/wei/base/springframework/redis/config/RedisProperties.java
  7. 40 36
      base-springframework/base-springframework-redis/src/main/java/com/wei/base/springframework/redis/utils/RedisLock.java
  8. 3 2
      base-springframework/base-springframework-redis/src/main/resources/META-INF/spring.factories
  9. 5 1
      base-springframework/base-springframework-redis/src/main/resources/application-redis.yml
  10. 0 1
      base-springframework/base-springframework-util/base-springframework-util.iml
  11. BIN
      wei-test-cloud/wei-test-cloud-api/target/classes/com/wei/cloud/api/ApiService.class
  12. BIN
      wei-user/target/classes/com/wei/cloud/api/ApiService.class
  13. 10 2
      wei-user/wei-user-app/src/main/java/com/wei/user/controller/UserController.java
  14. 5 4
      wei-user/wei-user-app/src/main/java/com/wei/user/service/impl/UserServiceImpl.java
  15. BIN
      wei-user/wei-user-app/target/classes/com/wei/cloud/api/impl/ApiServiceImpl.class

+ 1 - 1
base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/redis/config/MybatisPlusConfig.java → base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/config/MybatisPlusConfig.java

@@ -1,4 +1,4 @@
-package com.wei.base.springframework.redis.config;
+package com.wei.base.springframework.mysql.config;
 
 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
 import org.mybatis.spring.annotation.MapperScan;

+ 1 - 1
base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/redis/constants/GeneratorParam.java → base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/constants/GeneratorParam.java

@@ -1,4 +1,4 @@
-package com.wei.base.springframework.redis.constants;
+package com.wei.base.springframework.mysql.constants;
 
 import lombok.Data;
 

+ 1 - 1
base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/redis/env/InitializeCustomConfiguration.java → base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/env/InitializeCustomConfiguration.java

@@ -1,4 +1,4 @@
-package com.wei.base.springframework.redis.env;
+package com.wei.base.springframework.mysql.env;
 
 import com.google.common.collect.Lists;
 import org.apache.commons.lang3.StringUtils;

+ 2 - 2
base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/redis/generator/Generator.java → base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/generator/Generator.java

@@ -1,4 +1,4 @@
-package com.wei.base.springframework.redis.generator;
+package com.wei.base.springframework.mysql.generator;
 
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.generator.AutoGenerator;
@@ -10,7 +10,7 @@ import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-import com.wei.base.springframework.redis.constants.GeneratorParam;
+import com.wei.base.springframework.mysql.constants.GeneratorParam;
 import org.apache.commons.lang3.StringUtils;
 
 import java.util.List;

+ 2 - 2
base-springframework/base-springframework-mysql/src/main/resources/META-INF/spring.factories

@@ -1,4 +1,4 @@
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-  com.wei.base.springframework.redis.config.MybatisPlusConfig
+  com.wei.base.springframework.mysql.config.MybatisPlusConfig
 org.springframework.boot.env.EnvironmentPostProcessor=\
-  com.wei.base.springframework.core.env.InitializeCustomConfiguration
+  com.wei.base.springframework.mysql.env.InitializeCustomConfiguration

+ 24 - 0
base-springframework/base-springframework-redis/src/main/java/com/wei/base/springframework/redis/config/RedisProperties.java

@@ -0,0 +1,24 @@
+package com.wei.base.springframework.redis.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+import java.io.Serializable;
+
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "spring.redis")
+public class RedisProperties implements Serializable {
+
+    private static final long serialVersionUID = -4633090811506861386L;
+    /**
+     * redis锁key值
+     */
+    String lockKey;
+
+    /**
+     * redis锁默认过期时间
+     */
+    Long expirationTime;
+}

+ 40 - 36
base-springframework/base-springframework-redis/src/main/java/com/wei/base/springframework/redis/utils/RedisLock.java

@@ -1,57 +1,61 @@
 package com.wei.base.springframework.redis.utils;
 
+import com.wei.base.springframework.redis.config.RedisProperties;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
+import java.util.concurrent.TimeUnit;
+
 @Component
-public class RedisLock {
+public class RedisLock<T> {
+
+    @Autowired
+    private RedisTemplate<String, T> redisTemplate;
 
     @Autowired
-    private RedisTemplate<String, Object> redisTemplate;
+    private RedisProperties redisProperties;
+
+    /**
+     * 锁
+     *
+     * @param key            锁key值
+     * @param expirationTime 过期时间
+     * @param timeUnit       时间单位
+     * @return true枷锁成功, false枷锁失败
+     */
+    public Boolean lock(String key, Long expirationTime, TimeUnit timeUnit) {
+        return redisTemplate.opsForValue().setIfAbsent(redisProperties.getLockKey() + key, (T) "", expirationTime, timeUnit);
+    }
+
+    /**
+     * 锁(过期时间30秒)
+     *
+     * @param key 锁key值
+     * @return true枷锁成功, false枷锁失败
+     */
+    public Boolean lock(String key) {
+        return lock(key, redisProperties.getExpirationTime(), TimeUnit.SECONDS);
+    }
 
     /**
-     * 加锁
+     * 锁
      *
-     * @param key
-     * @param value 当前时间+超时时间
-     * @return
+     * @param key            锁key值
+     * @param expirationTime 过期时间(单位:秒)
+     * @return true枷锁成功, false枷锁失败
      */
-    public Boolean lock(String key, String value) {
-        redisTemplate.opsForValue();
-
-//        if (redisTemplate.opsForValue().setIfAbsent(key, value)) {//相当于SETNX,setIfAbsent方法设置了为true,没有设置为false
-//            return Boolean.TRUE;
-//        }
-//        redisTemplate.opsForValue().setIf
-//        //假设currentValue=A   接下来并发进来的两个线程的value都是B  其中一个线程拿到锁,除非从始至终所有都是在并发(实际上这中情况是不存在的),只要开始时有数据有先后顺序,则分布式锁就不会出现“多卖”的现象
-//        String currentValue = String.valueOf(redisTemplate.opsForValue().get(key));
-//        //如果锁过期  解决死锁
-//        if (!StringUtils.isEmpty(currentValue) && Long.parseLong(currentValue) < System.currentTimeMillis()) {
-//            //获取上一个锁的时间,锁过期后,GETSET将原来的锁替换成新锁
-//            String oldValue = String.valueOf(redisTemplate.opsForValue().getAndSet(key, value));
-//            if (!StringUtils.isEmpty(oldValue) && oldValue.equals(currentValue)) {
-//                return Boolean.TRUE;
-//            }
-//        }
-
-        return Boolean.FALSE;//拿到锁的就有执行权力,拿不到的只有重新再来,重新再来只得是让用户手动继续抢单
+    public Boolean lock(String key, Long expirationTime) {
+        return lock(key, expirationTime, TimeUnit.SECONDS);
     }
 
     /**
      * 解锁
      *
-     * @param key
-     * @param value
+     * @param key 锁key值
+     * @return true:成功,false:失败
      */
-    public void unlock(String key, String value) {
-//        try {
-//            String currentValue = String.valueOf(redisTemplate.opsForValue().get(key));
-//            if (!StringUtils.isEmpty(currentValue) && currentValue.equals(value)) {
-//                redisTemplate.opsForValue().getOperations().delete(key);
-//            }
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
+    public Boolean unlock(String key) {
+        return redisTemplate.opsForValue().getOperations().delete(redisProperties.getLockKey() + key);
     }
 }

+ 3 - 2
base-springframework/base-springframework-redis/src/main/resources/META-INF/spring.factories

@@ -1,5 +1,6 @@
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
   com.wei.base.springframework.redis.config.RedisConfig,\
-  com.wei.base.springframework.redis.utils.RedisLock
+  com.wei.base.springframework.redis.utils.RedisLock,\
+  com.wei.base.springframework.redis.config.RedisProperties
 org.springframework.boot.env.EnvironmentPostProcessor=\
-  com.wei.base.springframework.core.env.InitializeCustomConfiguration
+  com.wei.base.springframework.redis.env.InitializeCustomConfiguration

+ 5 - 1
base-springframework/base-springframework-redis/src/main/resources/application-redis.yml

@@ -13,4 +13,8 @@ spring:
         # 连接池中的最大空闲连接
         max-idle: 10
     # 连接超时时间(毫秒)
-    timeout: 1000
+    timeout: 3000
+    # redis锁key值
+    lockKey: "REDIS_LOCK:"
+    # redis锁默认过期时间
+    expirationTime: 30

+ 0 - 1
base-springframework/base-springframework-util/base-springframework-util.iml

@@ -53,7 +53,6 @@
     <orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.7.RELEASE" level="project" />
     <orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.7.RELEASE" level="project" />
     <orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.12" level="project" />
-    <orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.72" level="project" />
     <orderEntry type="library" name="Maven: com.google.guava:guava:29.0-jre" level="project" />
     <orderEntry type="library" name="Maven: com.google.guava:failureaccess:1.0.1" level="project" />
     <orderEntry type="library" name="Maven: com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava" level="project" />

BIN
wei-test-cloud/wei-test-cloud-api/target/classes/com/wei/cloud/api/ApiService.class


BIN
wei-user/target/classes/com/wei/cloud/api/ApiService.class


+ 10 - 2
wei-user/wei-user-app/src/main/java/com/wei/user/controller/UserController.java

@@ -1,5 +1,6 @@
 package com.wei.user.controller;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.wei.base.springframework.constant.vo.RestfulVO;
 import com.wei.base.springframework.redis.utils.RedisLock;
 import com.wei.user.entity.User;
@@ -50,9 +51,16 @@ public class UserController {
     @ApiOperation("分页查询所有数据")
     @PostMapping("/test3")
     public void test1(@RequestBody UserVO user) {
-        redisLock.lock("test", "1111");
+        System.err.println(redisLock.lock("test"));
         System.err.println(11111);
-//        redisLock.unlock("test", "1111");
+        System.err.println(redisLock.unlock("test"));
+        return;
+    }
+
+    @ApiOperation("分页查询所有数据")
+    @GetMapping("/test4")
+    public void test1(Object object) throws JsonProcessingException {
+        System.err.println(object.toString());
         return;
     }
 }

+ 5 - 4
wei-user/wei-user-app/src/main/java/com/wei/user/service/impl/UserServiceImpl.java

@@ -1,9 +1,9 @@
 package com.wei.user.service.impl;
 
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.wei.user.entity.User;
 import com.wei.user.mapper.UserMapper;
 import com.wei.user.service.UserService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
 @Service
 public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
 
-	@Autowired
-	private UserMapper userMapper;
-}
+    @Autowired
+    private UserMapper userMapper;
+    
+}

BIN
wei-user/wei-user-app/target/classes/com/wei/cloud/api/impl/ApiServiceImpl.class