java-spring基于redis单机版(redisTemplate)实现的分布式锁+redis消息队列,可用于秒杀,定时器,高并发,抢购

此教程不涉及整合 spring 整合 redis,可另行查阅资料教程。

代码:

RedisLock

package com.cashloan.analytics.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@Component
public class RedisLock {
private static Logger logger = LoggerFactory.getLogger(RedisLock.class);
private static final int DEFAULT_ACQUIRY_RESOLUTION_MILLIS = 100;
public static final String LOCK_PREFIX = "redis_lock_";

@Autowired
</span><span style="color: rgba(0, 0, 255, 1)">private</span> RedisTemplate&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)"> redisTemplate;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 锁超时时间,防止线程在入锁以后,无限的执行等待
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> expireMsecs = 60 * 1000<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 锁等待时间,防止线程饥饿
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span> timeoutMsecs = 10 * 1000<span style="color: rgba(0, 0, 0, 1)">;


</span><span style="color: rgba(0, 0, 255, 1)">public</span> String get(<span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> String key) {
    Object obj </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        obj </span>= redisTemplate.execute((RedisCallback&lt;Object&gt;) connection -&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            StringRedisSerializer serializer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringRedisSerializer();
            </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] data =<span style="color: rgba(0, 0, 0, 1)"> connection.get(serializer.serialize(key));
            connection.close();
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (data == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> serializer.deserialize(data);
        });
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
        logger.error(</span>"get redis error, key : {}"<span style="color: rgba(0, 0, 0, 1)">, key);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> obj != <span style="color: rgba(0, 0, 255, 1)">null</span> ? obj.toString() : <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> setNX(<span style="color: rgba(0, 0, 255, 1)">final</span> String key, <span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> String value) {
    Object obj </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        obj </span>= redisTemplate.execute((RedisCallback&lt;Object&gt;) connection -&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            StringRedisSerializer serializer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringRedisSerializer();
            Boolean success </span>=<span style="color: rgba(0, 0, 0, 1)"> connection.setNX(serializer.serialize(key), serializer.serialize(value));
            connection.close();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> success;
        });
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
        logger.error(</span>"setNX redis error, key : {}"<span style="color: rgba(0, 0, 0, 1)">, key);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> obj != <span style="color: rgba(0, 0, 255, 1)">null</span> ? (Boolean) obj : <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">private</span> String getSet(<span style="color: rgba(0, 0, 255, 1)">final</span> String key, <span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> String value) {
    Object obj </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        obj </span>= redisTemplate.execute((RedisCallback&lt;Object&gt;) connection -&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            StringRedisSerializer serializer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringRedisSerializer();
            </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] ret =<span style="color: rgba(0, 0, 0, 1)"> connection.getSet(serializer.serialize(key), serializer.serialize(value));
            connection.close();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> serializer.deserialize(ret);
        });
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
        logger.error(</span>"setNX redis error, key : {}"<span style="color: rgba(0, 0, 0, 1)">, key);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> obj != <span style="color: rgba(0, 0, 255, 1)">null</span> ? (String) obj : <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获得 lock. 实现思路: 主要是使用了redis 的setnx命令,缓存了锁. reids缓存的key是锁的key,所有的共享,
 * value是锁的到期时间(注意:这里把过期时间放在value了,没有时间上设置其超时时间) 执行过程:
 * 1.通过setnx尝试设置某个key的值,成功(当前没有这个锁)则返回,成功获得锁
 * 2.锁已经存在则获取锁的到期时间,和当前时间比较,超时的话,则设置新的值
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> true if lock is acquired, false acquire timeouted
 * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> InterruptedException
 *             in case of thread interruption
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> lock(String lockKey) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    lockKey </span>= LOCK_PREFIX +<span style="color: rgba(0, 0, 0, 1)"> lockKey;
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> timeout =<span style="color: rgba(0, 0, 0, 1)"> timeoutMsecs;
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> (timeout &gt;= 0<span style="color: rgba(0, 0, 0, 1)">) {
        </span><span style="color: rgba(0, 0, 255, 1)">long</span> expires = System.currentTimeMillis() + expireMsecs + 1<span style="color: rgba(0, 0, 0, 1)">;
        String expiresStr </span>= String.valueOf(expires); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 锁到期时间</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setNX(lockKey, expiresStr)) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
        }

        String currentValueStr </span>= <span style="color: rgba(0, 0, 255, 1)">this</span>.get(lockKey); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> redis里的时间</span>
        <span style="color: rgba(0, 0, 255, 1)">if</span> (currentValueStr != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp; Long.parseLong(currentValueStr) &lt;<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis()) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断是否为空,不为空的情况下,如果被其他线程设置了值,则第二个条件判断是过不去的
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> lock is expired</span>
String oldValueStr = this.getSet(lockKey, expiresStr); // 获取上一个锁到期时间,并设置现在的锁到期时间, // 只有一个线程才能获取上一个线上的设置时间,因为 jedis.getSet 是同步的 if (oldValueStr != null && oldValueStr.equals(currentValueStr)) { // 防止误删(覆盖,因为 key 是相同的)了他人的锁——这里达不到效果,这里值会被覆盖,但是因为什么相差了很少的时间,所以可以接受
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [分布式的情况下]:如过这个时候,多个线程恰好都到了这里,但是只有一个线程的设置值和当前值相同,他才有权利获取锁</span>
                <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            }
        }
        timeout </span>-=<span style="color: rgba(0, 0, 0, 1)"> DEFAULT_ACQUIRY_RESOLUTION_MILLIS;

        </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
         * 延迟100 毫秒, 这里使用随机时间可能会好一点,可以防止饥饿进程的出现,即,当同时到达多个进程,
         * 只会有一个进程获得锁,其他的都用同样的频率进行尝试,后面有来了一些进行,也以同样的频率申请锁,这将可能导致前面来的锁得不到满足.
         * 使用随机的等待时间可以一定程度上保证公平性
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
        Thread.sleep(DEFAULT_ACQUIRY_RESOLUTION_MILLIS);

    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * Acqurired lock release.
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> unlock(String lockKey) {
    lockKey </span>= LOCK_PREFIX +<span style="color: rgba(0, 0, 0, 1)"> lockKey;
    redisTemplate.delete(lockKey);
}

}

redis 消息队列:RedisQueue

package com.cashloan.analytics.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**

  • redis 消息队列
    */
    @Component
    public class RedisQueue {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    / ---------------------------------- redis 消息队列 ---------------------------------- */
    /

    • 存值
    • @param key 键
    • @param value 值
    • @return
      */
      public boolean lpush(String key, Object value) {
      try {
      redisTemplate.opsForList().leftPush(key, value);
      return true;
      }
      catch (Exception e) {
      e.printStackTrace();
      return false;
      }
      }

    /**

    • 取值 - <rpop:非阻塞式 >
    • @param key 键
    • @return
      */
      public Object rpop(String key) {
      try {
      return redisTemplate.opsForList().rightPop(key);
      }
      catch (Exception e) {
      e.printStackTrace();
      return null;
      }
      }

    /**

    • 取值 - <brpop:阻塞式 > - 推荐使用
    • @param key 键
    • @param timeout 超时时间
    • @param timeUnit 给定单元粒度的时间段
    •             TimeUnit.DAYS          //天
      
    •             TimeUnit.HOURS         //小时
      
    •             TimeUnit.MINUTES       //分钟
      
    •             TimeUnit.SECONDS       //秒
      
    •             TimeUnit.MILLISECONDS  //毫秒
      
    • @return
      */
      public Object brpop(String key, long timeout, TimeUnit timeUnit) {
      try {
      return redisTemplate.opsForList().rightPop(key, timeout, timeUnit);
      }
      catch (Exception e) {
      e.printStackTrace();
      return null;
      }
      }

    /**

    • 查看值
    • @param key 键
    • @param start 开始
    • @param end 结束 0 到 -1 代表所有值
    • @return
      */
      public List<Object> lrange(String key, long start, long end) {
      try {
      return redisTemplate.opsForList().range(key, start, end);
      }
      catch (Exception e) {
      e.printStackTrace();
      return null;
      }
      }

}

测试类 controller:Test

package com.cashloan.analytics.controller;

import com.cashloan.analytics.utils.RedisLock;
import com.cashloan.analytics.utils.RedisQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/test")
public class Test {
private final static String MESSAGE = "testmq";
@Autowired
private RedisQueue redisQueue;
@Autowired
private RedisLock redisLock;

@GetMapping(</span>"/add"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String add() {
    String uuid </span>= UUID.randomUUID().toString().replaceAll("-", ""<span style="color: rgba(0, 0, 0, 1)">);
    Map map </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> HashMap();
    map.put(</span>"id"<span style="color: rgba(0, 0, 0, 1)">, uuid);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加入redis消息队列</span>

redisQueue.lpush(MESSAGE, map);
addBatch();
return "success";
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addBatch() {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (redisLock.lock(MESSAGE)) {
            List</span>&lt;Object&gt; lrange = redisQueue.lrange(MESSAGE, 0, -1<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> size =<span style="color: rgba(0, 0, 0, 1)"> lrange.size();
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (size &gt;= 10<span style="color: rgba(0, 0, 0, 1)">) {
                List</span>&lt;Map&gt; maps = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; size; i++<span style="color: rgba(0, 0, 0, 1)">) {
                    Object brpop </span>=<span style="color: rgba(0, 0, 0, 1)"> redisQueue.rpop(MESSAGE);
                    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (brpop != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                        maps.add((Map) brpop);
                    }
                }
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 记录数据</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">maps.isEmpty()) {
                    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; maps.size(); i++<span style="color: rgba(0, 0, 0, 1)">) {
                        System.out.println(maps.get(i).get(</span>"id"<span style="color: rgba(0, 0, 0, 1)">));
                        Thread.sleep(</span>100<span style="color: rgba(0, 0, 0, 1)">);
                    }
                }
            }
        }
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) {
        e.printStackTrace();
    } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
        redisLock.unlock(MESSAGE);
    }
}

}

 

另有一份模拟高并发多线程请求的工具 (python3):

# -*- coding: utf-8 -*-
import requests
import threading

class postrequests():
def init(self):
self.url
= 'http://localhost:9090/test/add'
def post(self):
try:
r
= requests.get(self.url)
print(r.text)
except Exception as e:
print(e)

def test():
test
= postrequests()
return test.post()
try:
i
= 0
# 开启线程数目
tasks_number = 105
print('测试启动')
while i < tasks_number:
t
= threading.Thread(target=test)
t.start()
i
+= 1
except Exception as e:
print(e)