@Bean(destroyMethod = “destroy”)
public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) {
return new RedisLockRegistry(redisConnectionFactory, “lock”);
}
使用示例
@GetMapping(“/redis/lock”)
public String lock(@RequestParam(“key”) String key) {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
redisLockService.lock(key); try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace(); } System.out.println(DateFormatUtils.format(new Date(), “yyyy-MM-dd HH:mm:ss”));
redisLockService.unlock(key); } ).start(); } return “OK”;
}
RedisLockService 是我封装了的一个 Redis 锁服务,代码有点多,这里就不贴了,完整的代码示例在 Github 上,大家可以 Star 一下:
https://github.com/javasta