一、Lock4j 分布式鎖工具
你是不是在使用分布式鎖的時候,還在自己用 AOP
封裝框架?那么 Lock4j
你可以考慮一下。
Lock4j
是一個分布式鎖組件,其提供了多種不同的支持以滿足不同性能和環境的需求。
立志打造一個簡單但富有內涵的分布式鎖組件。
并且支持redission,redisTemplate,zookeeper
。可混用,支持擴展。
Giee地址:https://gitee.com/baomidou/lock4j
二、使用方式
這里我以 redisson
作為分布式鎖的底層。
添加依賴:
<dependency><groupId>com.baomidou</groupId><artifactId>lock4j-redisson-spring-boot-starter</artifactId><version>2.2.5</version>
</dependency>
然后再配置中增加 redis
的配置:
spring:redis:timeout: 6000password:cluster:max-redirects:nodes:- 192.168.40.120:6379- 192.168.40.121:6379- 192.168.40.122:6379
聲明 RedissonClient
:
@Configuration
public class RedissonConfig {@Beanpublic RedissonClient getRedisson(RedisProperties redisProperties) {Config config = new Config();String[] nodes = redisProperties.getCluster().getNodes().stream().filter(StringUtils::isNotBlank).map(node -> "redis://" + node).collect(Collectors.toList()).toArray(new String[]{});ClusterServersConfig clusterServersConfig = config.useClusterServers().addNodeAddress(nodes);if (StringUtils.isNotBlank(redisProperties.getPassword())) {clusterServersConfig.setPassword(redisProperties.getPassword());}clusterServersConfig.setConnectTimeout((int) (redisProperties.getTimeout().getSeconds() * 1000));clusterServersConfig.setScanInterval(2000);return Redisson.create(config);}
}
然后只需在需要分布式鎖的地方加 @Lock4j
即可:
@RestController
@RequestMapping("/lock")
public class Lock4jController {//不指定,默認獲取鎖超時3秒,30秒鎖過期@Lock4j@GetMapping("/test")public String test() {return "success";}@Lock4j(keys = {"#id", "#name"}, expire = 60000, acquireTimeout = 10000)@GetMapping("/test1")public String test1(Long id, String name) {Thread.sleep(5000);return "success";}}
如果同時兩次訪問 /test1
,可以感覺出第二次沒有獲得鎖的請求等待的時間更長,因為要等待鎖的釋放:
獲取鎖超時時間和鎖過期時間,可以通過配置在配置文件中全局生效:
lock4j:acquire-timeout: 3000 #默認值3s,可不設置expire: 30000 #默認值30s,可不設置primary-executor: com.baomidou.lock.executor.RedisTemplateLockExecutor #默認redisson>redisTemplate>zookeeper,可不設置lock-key-prefix: lock4j #鎖key前綴, 默認值lock4j,可不設置
acquire-timeout
等待鎖的時長,超過這個時間會默認拋出 com.baomidou.lock.exception.LockFailureException
異常。
也可以自定義異常捕獲,需要實現 LockFailureStrategy
接口:
@Slf4j
@Component
public class MyLockFailureStrategy implements LockFailureStrategy {@Overridepublic void onLockFailure(String key, Method method, Object[] arguments) {log.error("key: {} , method: {} ,arguments: {} ", key, method.getName(), Arrays.asList(arguments).toString());}
}
如果獲取鎖超時,則可以看到打印的日志:
鎖的獲取邏輯也可以自定義,如果是 Redisson
依賴下,可以繼承 AbstractLockExecutor<RLock>
抽象類,例如:
@Slf4j
@Component
public class MyLockExecutor extends AbstractLockExecutor<RLock> {@ResourceRedissonClient redissonClient;/*** 嘗試獲取鎖*/@Overridepublic RLock acquire(String lockKey, String lockValue, long expire, long acquireTimeout) {log.info("key: {} 嘗試獲取鎖", lockKey);try {RLock lockInstance = this.redissonClient.getLock(lockKey);boolean locked = lockInstance.tryLock(acquireTimeout, expire, TimeUnit.MILLISECONDS);return (RLock)this.obtainLockInstance(locked, lockInstance);} catch (InterruptedException var9) {return null;}}/*** 釋放鎖*/@Overridepublic boolean releaseLock(String key, String value, RLock lockInstance) {log.info("key: {} 釋放鎖", key);if (lockInstance.isHeldByCurrentThread()) {try {return (Boolean)lockInstance.forceUnlockAsync().get();} catch (InterruptedException | ExecutionException var5) {return false;}} else {return false;}}
}
然后在使用時指定執行器:
@Lock4j(keys = {"#id", "#name"}, expire = 60000, acquireTimeout = 1000, executor = MyLockExecutor.class)
@GetMapping("/test2")
public String test2(Long id, String name) throws InterruptedException {Thread.sleep(5000);return "success";
}
請求 test2
接口可以看到打印的日志:
Key
的生成也可以自定義,只需繼承 DefaultLockKeyBuilder
抽象類,例如:
@Slf4j
@Component
public class MyLockKeyBuilder extends DefaultLockKeyBuilder {public MyLockKeyBuilder(BeanFactory beanFactory) {super(beanFactory);}@Overridepublic String buildKey(MethodInvocation invocation, String[] definitionKeys) {String key = super.buildKey(invocation, definitionKeys);log.info("生成的key:{} ", key);return key;}
}
運行后可以觀察日志:
上面都是通過注解的方式,同樣也可以手動控制鎖的獲取和釋放,只需要引入 LockTemplate
,例如:
@RestController
@RequestMapping("/lock")
public class Lock4j2Controller {@Resourceprivate LockTemplate lockTemplate;@GetMapping("/test3")public String test1(Long id, String name) {// 獲取鎖final LockInfo lockInfo = lockTemplate.lock(id + name, 30000L, 5000L, RedissonLockExecutor.class);try {Thread.sleep(5000);return "success";} catch (InterruptedException e) {throw new RuntimeException(e);} finally {//釋放鎖lockTemplate.releaseLock(lockInfo);}}
}
三、通過鎖實現限流
在注解中 autoRelease
控制著是否自動在方法執行結束后釋放鎖,如果為 false
則是在 expire
時間到的時候移除鎖,實際是通過 Redis
的過期機制,通過這個機制可以限制某個 key
的訪問頻次,例如:
@Lock4j(keys = {"#id", "#name"}, expire = 3000, acquireTimeout = 10000, autoRelease = false)
@GetMapping("/test4")
public String test4(Long id, String name) throws InterruptedException {return "success";
}
當同一個 id
和 name
第一次訪問的時候速度會很快:
如果頻繁訪問則會被限流: