spring cloud微服務雲架構-用java使用 redlock
一、 在pom檔案引入redis和redisson依賴:
<!-- redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- redisson--> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.3.2</version> </dependency>
AquiredLockWorker介面類,,主要是用於獲取鎖後需要處理的邏輯:
/** * Created by fangzhipeng on 2017/4/5. * 獲取鎖後需要處理的邏輯 */ public interface AquiredLockWorker<T> { T invokeAfterLockAquire() throws Exception; }
二、DistributedLocker 獲取鎖管理類:
/** * Created by fangzhipeng on 2017/4/5. * 獲取鎖管理類 */ public interface DistributedLocker { /** * 獲取鎖 * @param resourceName 鎖的名稱 * @param worker 獲取鎖後的處理類 * @param <T> * @return 處理完具體的業務邏輯要返回的資料 * @throws UnableToAquireLockException * @throws Exception */ <T> T lock(String resourceName, AquiredLockWorker<T> worker) throws UnableToAquireLockException, Exception; <T> T lock(String resourceName, AquiredLockWorker<T> 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 類,實現了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 public <T> T lock(String resourceName, AquiredLockWorker<T> worker) throws InterruptedException, UnableToAquireLockException, Exception { return lock(resourceName, worker, 100); } @Override public <T> T lock(String resourceName, AquiredLockWorker<T> 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<Object>() { @Override public Object invokeAfterLockAquire() { doTask(); return null; } }); }catch (Exception e){ } } void doTask() { System.out.println(Thread.currentThread().getName() + " start"); Random random = new Random(); int _int = random.nextInt(200); System.out.println(Thread.currentThread().getName() + " sleep " + _int + "millis"); try { Thread.sleep(_int); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " end"); doneSignal.countDown(); } }
六、執行測試類:
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官方推薦的一種方案,可靠性比較高。
歡迎大家和我一起學習spring cloud構建微服務雲架構,我這邊會將近期研發的spring cloud微服務雲架構的搭建過程和精髓記錄下來,幫助更多有興趣研發spring cloud框架的朋友,大家來一起探討spring cloud架構的搭建過程及如何運用於企業專案。Spring Cloud大型企業分散式微服務雲構建的B2B2C電子商務平臺原始碼請加企鵝求求:三五三六二四七二五九
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69952307/viewspace-2676256/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java架構-(一)spring cloud微服務分散式雲架構 - Spring Cloud簡介Java架構SpringCloud微服務分散式
- spring cloud微服務分散式雲架構Spring Cloud ZuulSpringCloud微服務分散式架構Zuul
- spring cloud微服務分散式雲架構-Spring Cloud NetflixSpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構-Spring Cloud BusSpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構--hystrix的使用SpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構 - Spring Cloud簡介SpringCloud微服務分散式架構
- Spring Cloud分散式微服務雲架構構建SpringCloud分散式微服務架構
- (一)spring cloud微服務分散式雲架構 - Spring Cloud簡介SpringCloud微服務分散式架構
- (一)spring cloud微服務分散式雲架構-Spring Cloud簡介SpringCloud微服務分散式架構
- spring cloud + spring boot + springmvc+mybatis微服務雲架構CloudSpring BootSpringMVCMyBatis微服務架構
- Spring Cloud Spring Boot mybatis分散式微服務雲架構CloudSpring BootMyBatis分散式微服務架構
- Spring Cloud雲服務架構 - 企業分散式微服務雲架構構建SpringCloud架構分散式微服務
- spring cloud微服務分散式雲架構-Spring Cloud Config環境庫SpringCloud微服務分散式架構
- 整合spring cloud雲服務架構 - 企業分散式微服務雲架構構建SpringCloud架構分散式微服務
- Spring Cloud 微服務架構進階SpringCloud微服務架構
- 微服務架構:Dubbo VS Spring Cloud微服務架構SpringCloud
- spring cloud微服務架構設計SpringCloud微服務架構
- Spring Cloud構建微服務架構-spring cloud服務監控中心SpringCloud微服務架構
- spring cloud微服務分散式雲架構 - Spring Cloud整合專案簡介SpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構-Gateway入門SpringCloud微服務分散式架構Gateway
- spring cloud微服務分散式雲架構-Commons 普通抽象SpringCloud微服務分散式架構抽象
- (四)整合spring cloud雲服務架構 - 企業分散式微服務雲架構構建SpringCloud架構分散式微服務
- spring cloud + spring boot + springmvc+mybatis分散式微服務雲架構CloudSpring BootSpringMVCMyBatis分散式微服務架構
- (三)spring cloud微服務分散式雲架構 - Spring Cloud整合專案簡介SpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構--服務註冊(consul)SpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構(一)-spring cloud 服務註冊與發現SpringCloud微服務分散式架構
- Spring Cloud 微服務架構解決方案SpringCloud微服務架構
- spring cloud微服務分散式雲架構- Config 快速開始SpringCloud微服務分散式架構
- (十七)spring cloud微服務分散式雲架構-eureka 基礎SpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構-服務消費者FeignSpringCloud微服務分散式架構
- Spring Cloud系列(一):微服務架構簡介SpringCloud微服務架構
- spring cloud微服務分散式雲架構(四)-斷路器(Hystrix)SpringCloud微服務分散式架構
- spring cloud微服務分散式雲架構-單點登入(SSO)SpringCloud微服務分散式架構
- Spring Cloud微服務分散式雲架構—整合專案簡介SpringCloud微服務分散式架構
- Spring Cloud構建微服務架構-服務閘道器SpringCloud微服務架構
- Spring Cloud構建微服務架構-Hystrix服務降級SpringCloud微服務架構
- spring cloud微服務分散式雲架構-Spring Cloud 分散式的五大重點SpringCloud微服務分散式架構
- Spring Cloud微服務分散式雲架構 - 整合企業架構的技術點SpringCloud微服務分散式架構