- 概述
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),从而让使用者能够将精力更集中地放在处理业务逻辑上。
- 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