SpringBoot 集成Redisson

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 集成Redisson相关的知识,希望对你有一定的参考价值。

什么是Redisson

基于Java Redis 和Netty 实现的高性能异步无锁框架

温馨提示:JDK 大于等于1.8

Redisson 功能实现

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分布式锁

SpringBoot集成redisson操作redis

SpringBoot集成redisson分布式锁

[转帖]SpringBoot集成redisson分布式锁

SpringBoot 集成Redisson 提示:java.lang.ClassNotFoundException: **.redis.connection.ReactiveRedisConnec

SpringBoot集成redisson分布式锁