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>

其他维持不变

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值