SpringBoot——redis分布式锁实现(二十三)
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| redis: cluster: nodes: - 192.168.43.32:7001 - 192.168.43.32:7002 - 192.168.43.32:7003 - 192.168.43.32:7004 - 192.168.43.32:7005 - 192.168.43.32:7006 max-redirects: 3 lettuce: pool: max-idle: 8 max-wait: -1 max-active: 8 min-idle: 0
|
RedissionConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| import cn.rojao.util.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
@Value("${spring.redis.clusters}")
private String cluster;
@Value("${spring.redis.password}")
private String password;
@Bean
public RedissonClient getRedisson(){
String[] nodes = cluster.split(",");
for(int i=0;i<nodes.length;i++){
nodes[i] = "redis://"+nodes[i];
}
RedissonClient redisson = null;
Config config = new Config();
config.useClusterServers()
.setScanInterval(2000)
.addNodeAddress(nodes).setRetryAttempts(5).setTimeout(10000);
if(StringUtils.isNotEmpty(password)){
config.useClusterServers().setPassword(password);
}
redisson = Redisson.create(config);
return redisson;
} }
|
DistributedRedisLock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package com.example.springboot_redis.redis;
import lombok.extern.slf4j.Slf4j; 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;
@Slf4j @Component public class DistributedRedisLock {
@Autowired private RedissonClient redissonClient;
public Boolean lock(String lockName) { try { if (redissonClient == null) { log.info("DistributedRedisLock redissonClient is null"); return false; }
RLock lock = redissonClient.getLock(lockName); lock.lock(10, TimeUnit.SECONDS);
log.info("Thread [{}] DistributedRedisLock lock [{}] success", Thread.currentThread().getName(), lockName); return true; } catch (Exception e) { log.error("DistributedRedisLock lock [{}] Exception:", lockName, e); return false; } }
public Boolean unlock(String lockName) { try { if (redissonClient == null) { log.info("DistributedRedisLock redissonClient is null"); return false; }
RLock lock = redissonClient.getLock(lockName); lock.unlock(); log.info("Thread [{}] DistributedRedisLock unlock [{}] success", Thread.currentThread().getName(), lockName); return true; } catch (Exception e) { log.error("DistributedRedisLock unlock [{}] Exception:", lockName, e); return false; } }
}
|
LockTestController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.example.springboot_redis.contoller;
import com.example.springboot_redis.redis.DistributedRedisLock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/lock") public class LockTestController {
@Autowired private DistributedRedisLock distributedRedisLock;
@GetMapping("/testLock") public void testLock() { for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { Boolean lockFlag = distributedRedisLock.lock("LOCK"); } }).start(); } }
}
|