经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
SpringBoot整合Redisson实现分布式锁
来源:jb51  时间:2021/11/8 19:51:51  对本文有异议

Redisson是架设在redis基础上的一个Java驻内存数据网格(In-Memory Data Grid)。充分的利用了Redis键值数据库提供的一系列优势,基于Java实用工具包中常用接口,为使用者提供了一系列具有分布式特性的常用工具类。使得原本作为协调单机多线程并发程序的工具包获得了协调分布式多机多线程并发系统的能力,大大降低了设计和研发大规模分布式系统的难度。同时结合各富特色的分布式服务,更进一步简化了分布式环境中程序相互之间的协作。

Github地址:https://github.com/redisson/redisson

一、添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- springboot整合redis -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-data-redis</artifactId>
  10. </dependency>
  11. <!-- springboot整合redisson -->
  12. <dependency>
  13. <groupId>org.redisson</groupId>
  14. <artifactId>redisson-spring-boot-starter</artifactId>
  15. <version>3.13.6</version>
  16. </dependency>
  17. </dependencies>

二、redis配置文件

  1. server:
  2. port: 8000
  3. spring:
  4. redis:
  5. host: localhost
  6. port: 6379
  7. password: null
  8. database: 1
  9. timeout: 30000

三、新建配置类

  1. @Configuration
  2. public class MyRedissonConfig {
  3. @Value("${spring.redis.host}")
  4. String redisHost;
  5. @Value("${spring.redis.port}")
  6. String redisPort;
  7. @Value("${spring.redis.password}")
  8. String redisPassword;
  9. @Value("${spring.redis.timeout}")
  10. Integer redisTimeout;
  11. /**
  12. * Redisson配置
  13. * @return
  14. */
  15. @Bean
  16. RedissonClient redissonClient() {
  17. //1、创建配置
  18. Config config = new Config();
  19. redisHost = redisHost.startsWith("redis://") ? redisHost : "redis://" + redisHost;
  20. SingleServerConfig serverConfig = config.useSingleServer()
  21. .setAddress(redisHost + ":" + redisPort)
  22. .setTimeout(redisTimeout);
  23. if (StringUtils.isNotBlank(redisPassword)) {
  24. serverConfig.setPassword(redisPassword);
  25. }
  26. return Redisson.create(config);
  27. }
  28. }
  1. //单机
  2. RedissonClient redisson = Redisson.create();
  3. Config config = new Config();
  4. config.useSingleServer().setAddress("myredisserver:6379");
  5. RedissonClient redisson = Redisson.create(config);
  6. //主从
  7. Config config = new Config();
  8. config.useMasterSlaveServers()
  9. .setMasterAddress("127.0.0.1:6379")
  10. .addSlaveAddress("127.0.0.1:6389", "127.0.0.1:6332", "127.0.0.1:6419")
  11. .addSlaveAddress("127.0.0.1:6399");
  12. RedissonClient redisson = Redisson.create(config);
  13. //哨兵
  14. Config config = new Config();
  15. config.useSentinelServers()
  16. .setMasterName("mymaster")
  17. .addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379")
  18. .addSentinelAddress("127.0.0.1:26319");
  19. RedissonClient redisson = Redisson.create(config);
  20. //集群
  21. Config config = new Config();
  22. config.useClusterServers()
  23. .setScanInterval(2000) // cluster state scan interval in milliseconds
  24. .addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001")
  25. .addNodeAddress("127.0.0.1:7002");
  26. RedissonClient redisson = Redisson.create(config);

四、使用分布式锁

可重入锁

基于Redis的Redisson分布式可重入锁RLock对象实现了java.util.concurrent.locks.Lock接口。

  1. @RequestMapping("/redisson")
  2. public String testRedisson(){
  3. //获取分布式锁,只要锁的名字一样,就是同一把锁
  4. RLock lock = redissonClient.getLock("lock");
  5. //加锁(阻塞等待),默认过期时间是无限期
  6. lock.lock();
  7. try{
  8. //如果业务执行过长,Redisson会自动给锁续期
  9. Thread.sleep(1000);
  10. System.out.println("加锁成功,执行业务逻辑");
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. } finally {
  14. //解锁,如果业务执行完成,就不会继续续期
  15. lock.unlock();
  16. }
  17. return "Hello Redisson!";
  18. }

如果拿到分布式锁的节点宕机,且这个锁正好处于锁住的状态时,会出现锁死的状态,为了避免这种情况的发生,锁都会设置一个过期时间。这样也存在一个问题,一个线程拿到了锁设置了30s超时,在30s后这个线程还没有执行完毕,锁超时释放了,就会导致问题,Redisson给出了自己的答案,就是 watch dog 自动延期机制。
Redisson提供了一个监控锁的看门狗,它的作用是在Redisson实例被关闭前,不断的延长锁的有效期,也就是说,如果一个拿到锁的线程一直没有完成逻辑,那么看门狗会帮助线程不断的延长锁超时时间,锁不会因为超时而被释放。
默认情况下,看门狗的续期时间是30s,也可以通过修改Config.lockWatchdogTimeout来另行指定。
另外Redisson 还提供了可以指定leaseTime参数的加锁方法来指定加锁的时间。超过这个时间后锁便自动解开了,不会延长锁的有效期。

在RedissonLock类的renewExpiration()方法中,会启动一个定时任务每隔30/3=10秒给锁续期。如果业务执行期间,应用挂了,那么不会自动续期,到过期时间之后,锁会自动释放。

  1. private void renewExpiration() {
  2. ExpirationEntry ee = EXPIRATION_RENEWAL_MAP.get(getEntryName());
  3. if (ee == null) {
  4. return;
  5. }
  6. Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
  7. @Override
  8. public void run(Timeout timeout) throws Exception {
  9. ExpirationEntry ent = EXPIRATION_RENEWAL_MAP.get(getEntryName());
  10. if (ent == null) {
  11. return;
  12. }
  13. Long threadId = ent.getFirstThreadId();
  14. if (threadId == null) {
  15. return;
  16. }
  17. RFuture<Boolean> future = renewExpirationAsync(threadId);
  18. future.onComplete((res, e) -> {
  19. if (e != null) {
  20. log.error("Can't update lock " + getName() + " expiration", e);
  21. return;
  22. }
  23. if (res) {
  24. // reschedule itself
  25. renewExpiration();
  26. }
  27. });
  28. }
  29. }, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);
  30. ee.setTimeout(task);
  31. }

另外Redisson还提供了leaseTime的参数来指定加锁的时间。超过这个时间后锁便自动解开了。

  1. // 加锁以后10秒钟自动解锁
  2. // 无需调用unlock方法手动解锁
  3. lock.lock(10, TimeUnit.SECONDS);
  4. // 尝试加锁,最多等待100秒,上锁以后10秒自动解锁
  5. boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);

如果指定了锁的超时时间,底层直接调用lua脚本,进行占锁。如果超过leaseTime,业务逻辑还没有执行完成,则直接释放锁,所以在指定leaseTime时,要让leaseTime大于业务执行时间。RedissonLock类的tryLockInnerAsync()方法

  1. <T> RFuture<T> tryLockInnerAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) {
  2. internalLockLeaseTime = unit.toMillis(leaseTime);
  3. return evalWriteAsync(getName(), LongCodec.INSTANCE, command,
  4. "if (redis.call('exists', KEYS[1]) == 0) then " +
  5. "redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
  6. "redis.call('pexpire', KEYS[1], ARGV[1]); " +
  7. "return nil; " +
  8. "end; " +
  9. "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +
  10. "redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
  11. "redis.call('pexpire', KEYS[1], ARGV[1]); " +
  12. "return nil; " +
  13. "end; " +
  14. "return redis.call('pttl', KEYS[1]);",
  15. Collections.singletonList(getName()), internalLockLeaseTime, getLockName(threadId));
  16. }

读写锁

分布式可重入读写锁允许同时有多个读锁和一个写锁处于加锁状态。在读写锁中,读读共享、读写互斥、写写互斥。

  1. RReadWriteLock rwlock = redisson.getReadWriteLock("anyRWLock");
  2. // 最常见的使用方法
  3. rwlock.readLock().lock();
  4. // 或
  5. rwlock.writeLock().lock();

读写锁测试类,当访问write接口时,read接口会被阻塞住。

  1. @RestController
  2. public class TestController {
  3. @Autowired
  4. RedissonClient redissonClient;
  5. @Autowired
  6. StringRedisTemplate redisTemplate;
  7. @RequestMapping("/write")
  8. public String write(){
  9. RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("wr-lock");
  10. RLock writeLock = readWriteLock.writeLock();
  11. String s = UUID.randomUUID().toString();
  12. writeLock.lock();
  13. try {
  14. redisTemplate.opsForValue().set("wr-lock-key", s);
  15. Thread.sleep(10000);
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }finally {
  19. writeLock.unlock();
  20. }
  21. return s;
  22. }
  23. @RequestMapping("/read")
  24. public String read(){
  25. RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("wr-lock");
  26. RLock readLock = readWriteLock.readLock();
  27. String s = "";
  28. readLock.lock();
  29. try {
  30. s = redisTemplate.opsForValue().get("wr-lock-key");
  31. } finally {
  32. readLock.unlock();
  33. }
  34. return s;
  35. }
  36. }

信号量(Semaphore)

基于Redis的Redisson的分布式信号量(Semaphore)Java对象RSemaphore采用了与java.util.concurrent.Semaphore类似的接口和用法

关于信号量的使用你们能够想象一下这个场景,有三个停车位,当三个停车位满了后,其余车就不停了。能够把车位比做信号,如今有三个信号,停一次车,用掉一个信号,车离开就是释放一个信号。

咱们用 Redisson 来演示上述停车位的场景。

先定义一个占用停车位的方法:

  1. /**
  2. * 停车,占用停车位
  3. * 总共 3 个车位
  4. */
  5. @ResponseBody
  6. @RequestMapping("park")
  7. public String park() throws InterruptedException {
  8. // 获取信号量(停车场)
  9. RSemaphore park = redisson.getSemaphore("park");
  10. // 获取一个信号(停车位)
  11. park.acquire();
  12. return "OK";
  13. }

再定义一个离开车位的方法:

  1. /**
  2. * 释放车位
  3. * 总共 3 个车位
  4. */
  5. @ResponseBody
  6. @RequestMapping("leave")
  7. public String leave() throws InterruptedException {
  8. // 获取信号量(停车场)
  9. RSemaphore park = redisson.getSemaphore("park");
  10. // 释放一个信号(停车位)
  11. park.release();
  12. return "OK";
  13. }

为了简便,我用 Redis 客户端添加了一个 key:“park”,值等于 3,表明信号量为 park,总共有三个值。

 而后用 postman 发送 park 请求占用一个停车位。

而后在 redis 客户端查看 park 的值,发现已经改成 2 了。继续调用两次,发现 park 的等于 0,当调用第四次的时候,会发现请求一直处于等待中,说明车位不够了。若是想要不阻塞,能够用 tryAcquire 或 tryAcquireAsync。

咱们再调用离开车位的方法,park 的值变为了 1,表明车位剩余 1 个。

注意:屡次执行释放信号量操做,剩余信号量会一直增长,而不是到 3 后就封顶了。

闭锁(CountDownLatch)

CountDownLatch作用:某一线程,等待其他线程执行完毕之后,自己再继续执行。

  1. RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
  2. latch.trySetCount(1);
  3. latch.await();
  4. // 在其他线程或其他JVM里
  5. RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
  6. latch.countDown();

在TestController中添加测试方法,访问close接口时,调用await()方法进入阻塞状态,直到有三次访问release接口时,close接口才会返回。

  1. @RequestMapping("/close")
  2. public String close() throws InterruptedException {
  3. RCountDownLatch close = redissonClient.getCountDownLatch("close");
  4. close.trySetCount(3);
  5. close.await();
  6. return "close";
  7. }
  8. @RequestMapping("/release")
  9. public String release(){
  10. RCountDownLatch close = redissonClient.getCountDownLatch("close");
  11. close.countDown();
  12. return "release";
  13. }

到此这篇关于SpringBoot整合Redisson实现分布式锁的文章就介绍到这了,更多相关SpringBoot Redisson分布式锁内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号