SpringBoot 集成Redisson
Posted 在奋斗的大道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 集成Redisson相关的知识,希望对你有一定的参考价值。
什么是Redisson
基于Java Redis 和Netty 实现的高性能异步无锁框架
温馨提示:JDK 大于等于1.8
Redisson 功能实现
- Redis Replicated setup (including support of AWS ElastiCache and Azure Redis Cache)
- Redis Cluster setup (including support of AWS ElastiCache Cluster and Azure Redis Cache)
- Redis Sentinel setup
- Redis with Master with Slave only
- Redis single (including support of Azure Redis Cache and Google Cloud Memorystore for Redis)
- Thread-safe implementation
- Reactive Streams API
- RxJava3 API
- Asynchronous API
- Asynchronous connection pool
- Lua scripting
- Local cache support including Caffeine-based implementation
- Distributed Java objects
Object holder, Binary stream holder, Geospatial holder, BitSet, AtomicLong, AtomicDouble, PublishSubscribe, Bloom filter, HyperLogLog - Distributed Java collections
Map, Multimap, Set, List, SortedSet, ScoredSortedSet, LexSortedSet, Queue, Deque, Blocking Queue, Bounded Blocking Queue, Blocking Deque, Delayed Queue, Priority Queue, Priority Deque - Distributed Java locks and synchronizers
Lock, FairLock, MultiLock, RedLock, ReadWriteLock, Semaphore, PermitExpirableSemaphore, CountDownLatch - Distributed services
Remote service, Live Object service, Executor service, Scheduler service, MapReduce service - Helidon integration
- Micronaut integration
- Quarkus integration
- Spring Cache implementation
- Spring Transaction API implementation
- Spring Data Redis integration
- Spring Boot Starter implementation
- Hibernate Cache implementation
- MyBatis Cache implementation
- Transactions API
- JCache API (JSR-107) implementation
- Tomcat Session Manager implementation
- Spring Session implementation
- Redis pipelining (command batches)
- Supports android platform
- Supports auto-reconnection
- Supports failed to send command auto-retry
- Supports OSGi
- Supports SSL
- Supports many popular codecs (JBoss Marshalling, Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, Amazon Ion, LZ4, Snappy and JDK Serialization)
- With over 2000 unit tests
SpringBoot 集成Redisson(SpringBoot 1.x版本)
pom.xml 集成Redisson依赖
<!-- 集成并发锁模块 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.13.1</version>
<exclusions>
<exclusion>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-22</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-18</artifactId>
<version>3.13.1</version>
</dependency>
Redisson配置对象定义
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:redis/redis.properties")
@ImportResource("classpath:redisson.xml" )
public class RedissonConfig
redis.properties
# redis host å°å
redis.host = 127.0.0.1
# redis port 端å£
redis.port = 6379
# redis password å¯ç
redis.password = abc123
# redis åå¨æ°æ®åº
redis.database = 0
redisson.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:redisson="http://redisson.org/schema/redisson"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://redisson.org/schema/redisson
http://redisson.org/schema/redisson/redisson.xsd">
<redisson:client id="redissonClient">
<redisson:single-server address="redis://$redis.host:$redis.port" connection-pool-size="30" password="$redis.password"/>
</redisson:client>
</beans>
SpringBoot 集成Redisson 实列截图
功能说明:生成调档单号使用分布式锁,防止并发产生重复调档单号。
@Autowired
private GlobalLogMessage globalLogMessage;
@Autowired
private RedissonClient redissonClient;
// 定义常量锁对象
public static final String APPLICATION_CODE = "APPLICATION_CODE";
@ApiOperation(httpMethod = "POST", value = "利用信息保存")
@RequestMapping(value = "/insert", method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
@ResponseBody
public Result insert(HttpServletRequest request,
@RequestBody @ApiParam(name = "利用对象", value = "json格式对象", required = true) UcasArchiveUseListWrapper entity)
String sid = null;
RLock rlock = redissonClient.getLock(APPLICATION_CODE);//设置锁超时时间,防止异常造成死锁
rlock.lock(10, TimeUnit.SECONDS);
try
Map<String, Object> paramter = new HashMap<String, Object>();
paramter.put("currentDt", DateUtils.dateTimeNow("yyyy-MM-dd"));
Integer total = ucasArchiveUseListService.getSerialNumber(paramter);// 更新对象的状态
String useNo = null;
if (total > 0)
total++;
useNo = StringFormatUtil.addZeroForNum(String.valueOf(total), 4);
else
total = 1;
useNo = StringFormatUtil.addZeroForNum(String.valueOf(total), 4);
LocalDateTime currentDate = LocalDateTime.now();
String dt = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
entity.setUseNo(dt + useNo);
// 捕获异常,必要时恢复到原来的不变约束
// 如果有return语句,放在这里
// status=0申请调阅
entity.setStatus(ArchiveUseListConstants.APPLY_READING);
entity.setCreatedDt(new Date());
UserTool tool = new UserTool(ucasAuthUserInfoService);
String userPin = tool.getUserPin(request);
if (!StringUtils.isEmpty(userPin))
entity.setCreatedBy(userPin);
entity.setInfoSource(ArchiveUseListConstants.ACCESS_MUSEUM_TYPE);
sid = ucasArchiveUseListService.insertSelective(entity);
catch(Exception e)
log.error("生成调档单号异常:", e.getMessage(), e);
finally
rlock.unlock();
SpringBoot 集成Redisson(SpringBoot 2.x版本)
pom.xml 集成Redisson依赖
<!-- 集成redisson-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.13.6</version>
</dependency>
其他维持不变
以上是关于SpringBoot 集成Redisson的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot 集成Redisson 提示:java.lang.ClassNotFoundException: **.redis.connection.ReactiveRedisConnec