原始碼位址
org.redisson
redisson
單節點redis
@configuration
public class redissonconfig
}
多節點redis集群
//建立配置
config config = new config();
//指定使用集群部署方式
config.useclusterservers()
// 集群狀態掃瞄間隔時間,單位是毫秒
.setscaninterval(2000)
//cluster方式至少6個節點(3主3從,3主做sharding,3從用來保證主宕機後可以高可用)
.addnodeaddress("redis:" )
.addnodeaddress("redis:")
.addnodeaddress("redis:")
.addnodeaddress("redis:")
.addnodeaddress("redis:")
.addnodeaddress("redis:");
//建立客戶端(發現這一非常耗時,基本在2秒-4秒左右)
redissonclient redisson = redisson.create(config);
/**
* @author energy
* @date 2020/12/7
*/@inherited
@documented
@target()
@retention(retentionpolicy.runtime)
public @inte***ce distributedlock
/**
* @author energy
* @date 2020/12/7
*/@component
@aspect
public class distributedlockadvice
@pointcut("@annotation(com.example.redis.annotation.distributedlock)")
private void aspectjmethod()
@before("aspectjmethod()")
public void beforeadvice(joinpoint joinpoint)
@after("aspectjmethod()")
public void afteradvice(joinpoint joinpoint)
@around("aspectjmethod()")
public object aroundadvice(proceedingjoinpoint pjp) throws throwable ,引數:{}", methodname, arguments);
object args = new object[arguments.length];
for (int i = 0; i < arguments.length; i++)
args[i] = arguments[i];
}distributedlock annotation = method.getannotation(distributedlock.class);
string prefix = annotation.prefix();
long maxsleeptime = annotation.maxsleeptime();
object result;
string lockparamkey = stringutils.arraytodelimitedstring(args, ".");
string lockkey = this.getmsilockkey(prefix, targetclass.get******name(), method.getname(), lockparamkey);
rlock rlock = redissonclient.getlock(lockkey);
if(rlock.trylock(maxsleeptime, time_unit))", lockkey);
try finally ]", lockkey);
}}else ,引數:{}", methodname, arguments);
return result;
}return result;
}private string getmsilockkey(string prefix, string classname, string invokedmethod, string lockparamkey)
}
/**
* @author energy
* @date 2020/12/7
*/@restcontroller
public class distributedlockcontroller catch (interruptedexception e)
return new responsevo(1, "請求成功", null);
}}
原始碼位址 Redisson實現分布式鎖
引入包 org.redissongroupid redissonartifactid 3.10.0version dependency redissonconfig類 package com.xiepanpan.locks.lockstest.config import org.redisson.r...
RedisSon實現分布式鎖
主要步驟 1 引入redisson的依賴 2 配置redisson的配置類 3 使用redisson構建分布式鎖,在需要使用分布式鎖的地方注入redissonclient這個類來獲取鎖 第一步 引入依賴 org.springframework.boot spring boot starter par...
redisson實現分布式鎖
redisson官方文件 1.匯入相關依賴 這裡我只匯入redisson,其他還需要redis的依賴 org.redisson groupid redisson artifactid 3.12 5 version dependency 2.新增redisson核心配置 description red...