redisson实现分布式锁实战

  1. 概述
https://github.com/redisson/redisson/wiki/1.-Overview

Redisson is a Redis Java client with features of In-Memory Data Grid. It provides more convenient and easiest way to work with Redis. Redisson objects provides a separation of concern, which allows you to keep focus on the data modeling and application logic.
Each Redis server instance can handle up to 1TB RAM.

Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。它不仅提供了一系列的分布式的Java常用对象,还提供了许多分布式服务。其中包括(BitSet, Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service, Scheduler service) Redisson提供了使用Redis的最简单和最便捷的方法。Redisson的宗旨是促进使用者对Redis的关注分离(Separation of Concern),从而让使用者能够将精力更集中地放在处理业务逻辑上。

  1. Redisson使用
  • 引入maven依赖
   	<dependency>
   		<groupId>org.redisson</groupId>
   		<artifactId>redisson</artifactId>
   		<version>3.8.2</version>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-data-redis</artifactId>
   	</dependency>
   	<dependency>
   		<groupId>redis.clients</groupId>
   		<artifactId>jedis</artifactId>
   	</dependency>
  • 配置redisson
    在这里插入图片描述

  • controller应用
    在这里插入图片描述
    下面这种方式一定要特别注意,解锁一定要放到加锁成功后的操作里,否则就报错。
    在这里插入图片描述
    unlock()方法特别说明:
    在这里插入图片描述

  • application.yml文件
    在这里插入图片描述

spring:
  redis:
    #single单机 sentinel哨兵
    mode: sentinel
    host: 192.168.1.106
    port: 6380
    password: 123456
    database: 5
    timeout: 3000
    jedis:
      pool:
        #资源池中的最大连接数
        maxTotal: 20
        #资源池允许的最大空闲连接数
        maxIdle: 20
        #资源池确保的最少空闲连接数
        minIdle: 5
        #当资源池用尽后,调用者是否要等待。只有当值为true时,下面的maxWaitMillis才会生效。
        blockWhenExhausted: true
        #最大阻塞等待时间(负数表示没限制)
        maxWaitMillis: 5000
        #向资源池借用连接时是否做连接有效性检测
        testOnBorrow: false
        #向资源池归还连接时是否做连接有效性检测
        testOnReturn: false
        #是否开启JMX监控
        jmxEnabled: true
        #是否开启空闲资源检测
        testWhileIdle: true
        #空闲资源的检测周期(单位为毫秒)
        timeBetweenEvictionRunsMillis: 30000
        #资源池中资源的最小空闲时间(单位为毫秒),达到此值后空闲资源将被移除
        minEvictableIdleTimeMillis: 60000
        #做空闲资源检测时,每次检测资源的个数
        numTestsPerEvictionRun: 20
    sentinel:
      master: mymaster
      nodes: 192.168.1.106:6380,192.168.1.107:6380
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿在京

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值