Redis 实现同步锁案例

1、技术方案

1.1、redis的基本命令

1)setnx命令(set if not exists)

语法:setnx key value

功能:当且仅当 key 不存在,将 key 的值设为 value ,并返回1;若给定的 key 已经存在,则 setnx 不做任何动作,并返回0。

2)expire命令

语法:expire key seconds

功能:设置key的过期时间。如果key已过期,将会被自动删除。

3)del命令

语法:del key [key …]

功能:删除给定的一个或多个 key ,不存在的 key 会被忽略。

1.2、实现同步锁原理

1)加锁:“锁”就是一个存储在redis里的key-value对,key是把一组投资操作用字符串来形成唯一标识,value其实并不重要,因为只要这个唯一的key-value存在,就表示这个操作已经上锁。

2)解锁:既然key-value对存在就表示上锁,那么释放锁就自然是在redis里删除key-value对。

3)阻塞、非阻塞:阻塞式的实现,若线程发现已经上锁,会在特定时间内轮询锁。非阻塞式的实现,若发现线程已经上锁,则直接返回。

4)处理异常情况:假设当投资操作调用其他平台接口出现等待时,自然没有释放锁,这种情况下加入锁超时机制,用redis的expire命令为key设置超时时长,过了超时时间redis就会将这个key自动删除,即强制释放锁

(此步骤需在java内部设置同样的超时机制,内部超时时长应小于或等于redis超时时长)。

1.3、处理流程图  

2、代码实现

2.1、同步锁工具类

package com.mic.synchrolock.util;
import java.util.arraylist;
import java.util.list;
import java.util.uuid;
import javax.annotation.postconstruct;
import javax.annotation.predestroy;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.springframework.beans.factory.annotation.autowired;
import com.mic.constants.constants;
import com.mic.constants.investtype;
/**
 * 分布式同步锁工具类
 * @author administrator
 *
 */
public class synchrolockutil {
 private final log logger = logfactory.getlog(getclass());
 @autowired
 private redisclienttemplate redisclienttemplate;
 public final string retrytype_wait = "1";  //加锁方法当对象已加锁时,设置为等待并轮询
 public final string retrytype_nowait = "0";  //加锁方法当对象已加锁时,设置为直接返回
 private string requesttimeoutname = "";  //投资同步锁请求超时时间
 private string retryintervalname = "";   //投资同步锁轮询间隔
 private string keytimeoutname = "";  //缓存中key的失效时间
 private string investproductsn = "";   //产品sn
 private string uuid;    //对象唯一标识
 private long starttime = system.currenttimemillis(); //首次调用时间
 public long getstarttime() {
  return starttime;
 }
 list<string> keylist = new arraylist<string>(); //缓存key的保存集合
 public list<string> getkeylist() {
  return keylist;
 }
 public void setkeylist(list<string> keylist) {
  this.keylist = keylist;
 }
 @postconstruct
 public void init() {
  uuid = uuid.randomuuid().tostring();
 }
 @predestroy
 public void destroy() {
  this.unlock();
 }
 /**
  * 根据传入key值,判断缓存中是否存在该key
  * 存在-已上锁:判断retrytype,轮询超时,或直接返回,返回ture
  * 不存在-未上锁:将该放入缓存,返回false
  * @param key
  * @param retrytype 当遇到上锁情况时 1:轮询;0:直接返回
  * @return
  */
 public boolean islocked(string key,string retrytype){
  boolean flag = true;
  logger.info("====投资同步锁设置轮询间隔、请求超时时长、缓存key失效时长====");
  //投资同步锁轮询间隔 毫秒
  long retryinterval = long.parselong(constants.getproperty(retryintervalname));
  //投资同步锁请求超时时间 毫秒
  long requesttimeout = long.parselong(constants.getproperty(requesttimeoutname));
  //缓存中key的失效时间 秒
  integer keytimeout = integer.parseint(constants.getproperty(keytimeoutname));
  //调用缓存获取当前产品锁
  logger.info("====当前产品key为:"+key+"====");
  if(islockedinredis(key,keytimeout)){
   if("1".equals(retrytype)){
    //采用轮询方式等待
    while (true) {
     logger.info("====产品已被占用,开始轮询====");
     try {
      thread.sleep(retryinterval);
     } catch (interruptedexception e) {
      logger.error("线程睡眠异常:"+e.getmessage(), e);
      return flag;
     }
     logger.info("====判断请求是否超时====");
     long currenttime = system.currenttimemillis(); //当前调用时间
     long interval = currenttime - starttime;
     if (interval > requesttimeout) {
      logger.info("====请求超时====");
      return flag;
     }
     if(!islockedinredis(key,keytimeout)){
      logger.info("====轮询结束,添加同步锁====");
      flag = false;
      keylist.add(key);
      break;
     }
    }
   }else{
    //不等待,直接返回
    logger.info("====产品已被占用,直接返回====");
    return flag;
   }
  }else{
   logger.info("====产品未被占用,添加同步锁====");
   flag = false;
   keylist.add(key);
  }
  return flag;
 }
 /**
  * 在缓存中查询key是否存在
  * 若存在则返回true;
  * 若不存在则将key放入缓存,设置过期时间,返回false
  * @param key
  * @param keytimeout key超时时间单位是秒
  * @return
  */
 boolean islockedinredis(string key,int keytimeout){
  logger.info("====在缓存中查询key是否存在====");
  boolean isexist = false;
  //与redis交互,查询对象是否上锁
  long result = this.redisclienttemplate.setnx(key, uuid);
  logger.info("====上锁 result = "+result+"====");
  if(null != result && 1 == integer.parseint(result.tostring())){
   logger.info("====设置缓存失效时长 = "+keytimeout+"秒====");
   this.redisclienttemplate.expire(key, keytimeout);
   logger.info("====上锁成功====");
   isexist = false;
  }else{
   logger.info("====上锁失败====");
   isexist = true;
  }
  return isexist;
 }
 /**
  * 根据传入key,对该产品进行解锁
  * @param key
  * @return
  */
 public void unlock(){
  //与redis交互,对产品解锁
  if(keylist.size()>0){
   for(string key : this.keylist){
    string value = this.redisclienttemplate.get(key);
    if(null != value && !"".equals(value)){
     if(uuid.equals(value)){
      logger.info("====解锁key:"+key+" value="+value+"====");
      this.redisclienttemplate.del(key);
     }else{
      logger.info("====待解锁集合中key:"+key+" value="+value+"与uuid不匹配====");
     }
    }else{
     logger.info("====待解锁集合中key="+key+"的value为空====");
    }
   }
  }else{
   logger.info("====待解锁集合为空====");
  }
 }
}

2.2、业务调用模拟样例

//获取同步锁工具类
  synchrolockutil synchrolockutil = springutils.getbean("synchrolockutil");
  //获取需上锁资源的key
  string key = "abc";
  //查询是否上锁,上锁轮询,未上锁加锁
  boolean islocked = synchrolockutil.islocked(key,synchrolockutil.retrytype_wait);
  //判断上锁结果
  if(islocked){
   logger.error("同步锁请求超时并返回 key ="+key);
  }else{
   logger.info("====同步锁加锁陈功====");
  }
  try {
   //执行业务处理
  } catch (exception e) {
   logger.error("业务异常:"+e.getmessage(), e);
  }finally{
   //解锁
    synchrolockutil.unlock();
  }

2.3、如果业务处理内部,还有嵌套加锁需求,只需将对象传入方法内部,加锁成功后将key值追加到集合中即可

ps:实际实现中还需要jedis工具类,需额外添加调用

补充:使用redis锁还是出现同步问题

一种可能是,2台机器同时访问,一台访问,还没有把锁设置过去的时候,另一台也查不到就会出现这个问题。

解决方法

这我跟写代码的方式有关。先查,如果不存在就set,这种方式有极微小的可能存在时间差,导致锁set了2次。

推荐使用setifabsent 这样在redis set的时候是单线程的。不会存在重复的问题。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐