這篇文章將為大家詳細講解有關(guān)怎么用java使用redlock,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
專注于為中小企業(yè)提供網(wǎng)站建設、成都網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)凌云免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
一、在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 處理完具體的業(yè)務邏輯要返回的數(shù)據(jù) * @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連接類 */ @Component public class RedissonConnector { RedissonClient redisson; @PostConstruct public void init(){ redisson = Redisson.create(); } public RedissonClient getClient(){ return redisson; } }
四、RedisLocker 類,實現(xiàn)了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. */ @Component public class RedisLocker implements DistributedLocker{ private final static String LOCKER_PREFIX = "lock:"; @Autowired RedissonConnector redissonConnector; @Override publicT 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官方推薦的一種方案,可靠性比較高。
關(guān)于怎么用java使用redlock就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。