怎么用java使用redlock
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇文章将为大家详细讲解有关怎么用java使用redlock,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一、在pom文件引入redis和rediss
千家信息网最后更新 2025年12月03日怎么用java使用redlock
这篇文章将为大家详细讲解有关怎么用java使用redlock,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
一、在pom文件引入redis和redisson依赖:
org.springframework.boot spring-boot-starter-data-redis org.redisson redisson 3.3.2
AquiredLockWorker接口类,,主要是用于获取锁后需要处理的逻辑:
/** * Created by fangzhipeng on 2017/4/5. * 获取锁后需要处理的逻辑 */public interface AquiredLockWorker{ T invokeAfterLockAquire() throws Exception;}
二、DistributedLocker 获取锁管理类:
/** * Created by fangzhipeng on 2017/4/5. * 获取锁管理类 */public interface DistributedLocker { /** * 获取锁 * @param resourceName 锁的名称 * @param worker 获取锁后的处理类 * @param * @return 处理完具体的业务逻辑要返回的数据 * @throws UnableToAquireLockException * @throws Exception */ T lock(String resourceName, AquiredLockWorker worker) throws UnableToAquireLockException, Exception; T lock(String resourceName, AquiredLockWorker worker, int lockTime) throws UnableToAquireLockException, Exception;} nableToAquireLockException ,不能获取锁的异常类:
/** * Created by fangzhipeng on 2017/4/5. * 异常类 */public class UnableToAquireLockException extends RuntimeException { public UnableToAquireLockException() { } public UnableToAquireLockException(String message) { super(message); } public UnableToAquireLockException(String message, Throwable cause) { super(message, cause); }}三、RedissonConnector 连接类:
/** * Created by fangzhipeng on 2017/4/5. * 获取RedissonClient连接类 */@Componentpublic class RedissonConnector { RedissonClient redisson; @PostConstruct public void init(){ redisson = Redisson.create(); } public RedissonClient getClient(){ return redisson; }}四、RedisLocker 类,实现了DistributedLocker:
import org.redisson.api.RLock;import org.redisson.api.RedissonClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;/** * Created by fangzhipeng on 2017/4/5. */@Componentpublic class RedisLocker implements DistributedLocker{ private final static String LOCKER_PREFIX = "lock:"; @Autowired RedissonConnector redissonConnector; @Override public T lock(String resourceName, AquiredLockWorker worker) throws InterruptedException, UnableToAquireLockException, Exception { return lock(resourceName, worker, 100); } @Override public T lock(String resourceName, AquiredLockWorker worker, int lockTime) throws UnableToAquireLockException, Exception { RedissonClient redisson= redissonConnector.getClient(); RLock lock = redisson.getLock(LOCKER_PREFIX + resourceName); // Wait for 100 seconds seconds and automatically unlock it after lockTime seconds boolean success = lock.tryLock(100, lockTime, TimeUnit.SECONDS); if (success) { try { return worker.invokeAfterLockAquire(); } finally { lock.unlock(); } } throw new UnableToAquireLockException(); }} 五、测试类:
@Autowired RedisLocker distributedLocker; @RequestMapping(value = "/redlock") public String testRedlock() throws Exception{ CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(5); for (int i = 0; i < 5; ++i) { // create and start threads new Thread(new Worker(startSignal, doneSignal)).start(); } startSignal.countDown(); // let all threads proceed doneSignal.await(); System.out.println("All processors done. Shutdown connection"); return "redlock"; } class Worker implements Runnable { private final CountDownLatch startSignal; private final CountDownLatch doneSignal; Worker(CountDownLatch startSignal, CountDownLatch doneSignal) { this.startSignal = startSignal; this.doneSignal = doneSignal; } public void run() { try { startSignal.await(); distributedLocker.lock("test",new AquiredLockWorker六、运行测试类:
Thread-48 start
Thread-48 sleep 99millis
Thread-48 end
Thread-49 start
Thread-49 sleep 118millis
Thread-49 end
Thread-52 start
Thread-52 sleep 141millis
Thread-52 end
Thread-50 start
Thread-50 sleep 28millis
Thread-50 end
Thread-51 start
Thread-51 sleep 145millis
Thread-51 end
不管怎么样,这是redis官方推荐的一种方案,可靠性比较高。
关于怎么用java使用redlock就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
处理
逻辑
内容
文章
更多
知识
篇文章
测试
管理
不错
业务
可靠性
名称
官方
接口
数据
文件
方案
质量
这是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全先进集体发言稿
互联网网络技术概况
杭州心溢庄园网络技术有限公司
建筑业网络安全员
网维大师服务器管理网卡
国家网络安全周直播
saas一般用啥数据库
苹果手机服务器响应
公司网络安全有哪些条件
java软件开发入门培训班
园区网络安全工作
网络安全漫画a4
万得上怎么搜数据库
服务器管理器找不到了
邯郸软件开发销售公司
厦门十七互联网科技有限公司
影城票务系统数据库设计
内蒙古新华互联网科技学校
网络安全开发前景
澡堂管理系统数据库
连接数据库的.cs文件
江西省网络安全教育公开课
网络安全由我守护
dell服务器启动
打开数据库设计器
数据库看存储过程
非计算机专业做网络安全
计票服务器
odbc怎么设计数据库
游驰网络技术 上市