当前位置:   article > 正文

SpringBoot 集成Redisson_redisson-spring-data-18

redisson-spring-data-18

什么是Redisson

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

温馨提示:JDK 大于等于1.8

Redisson 功能实现

SpringBoot 集成Redisson(SpringBoot 1.x版本)

pom.xml 集成Redisson依赖

  1. <!-- 集成并发锁模块 -->
  2. <dependency>
  3. <groupId>org.redisson</groupId>
  4. <artifactId>redisson-spring-boot-starter</artifactId>
  5. <version>3.13.1</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.redisson</groupId>
  9. <artifactId>redisson-spring-data-22</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.redisson</groupId>
  15. <artifactId>redisson-spring-data-18</artifactId>
  16. <version>3.13.1</version>
  17. </dependency>

Redisson配置对象定义

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.context.annotation.ImportResource;
  3. import org.springframework.context.annotation.PropertySource;
  4. @Configuration
  5. @PropertySource("classpath:redis/redis.properties")
  6. @ImportResource({"classpath:redisson.xml" })
  7. public class RedissonConfig {
  8. }

redis.properties

  1. # redis host å°å
  2. redis.host = 127.0.0.1
  3. # redis port 端å£
  4. redis.port = 6379
  5. # redis password å¯ç 
  6. redis.password = abc123
  7. # redis å­å¨æ°æ®åº
  8. redis.database = 0

redisson.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:redisson="http://redisson.org/schema/redisson"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://redisson.org/schema/redisson
  11. http://redisson.org/schema/redisson/redisson.xsd">
  12. <redisson:client id="redissonClient">
  13. <redisson:single-server address="redis://${redis.host}:${redis.port}" connection-pool-size="30" password="${redis.password}"/>
  14. </redisson:client>
  15. </beans>

SpringBoot 集成Redisson 实列截图

功能说明:生成调档单号使用分布式锁,防止并发产生重复调档单号。

  1. @Autowired
  2. private GlobalLogMessage globalLogMessage;
  3. @Autowired
  4. private RedissonClient redissonClient;
  5. // 定义常量锁对象
  6. public static final String APPLICATION_CODE = "APPLICATION_CODE";
  7. @ApiOperation(httpMethod = "POST", value = "利用信息保存")
  8. @RequestMapping(value = "/insert", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
  9. @ResponseBody
  10. public Result insert(HttpServletRequest request,
  11. @RequestBody @ApiParam(name = "利用对象", value = "json格式对象", required = true) UcasArchiveUseListWrapper entity) {
  12. String sid = null;
  13. RLock rlock = redissonClient.getLock(APPLICATION_CODE);//设置锁超时时间,防止异常造成死锁
  14. rlock.lock(10, TimeUnit.SECONDS);
  15. try{
  16. Map<String, Object> paramter = new HashMap<String, Object>();
  17. paramter.put("currentDt", DateUtils.dateTimeNow("yyyy-MM-dd"));
  18. Integer total = ucasArchiveUseListService.getSerialNumber(paramter);// 更新对象的状态
  19. String useNo = null;
  20. if (total > 0) {
  21. total++;
  22. useNo = StringFormatUtil.addZeroForNum(String.valueOf(total), 4);
  23. } else {
  24. total = 1;
  25. useNo = StringFormatUtil.addZeroForNum(String.valueOf(total), 4);
  26. }
  27. LocalDateTime currentDate = LocalDateTime.now();
  28. String dt = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
  29. entity.setUseNo(dt + useNo);
  30. // 捕获异常,必要时恢复到原来的不变约束
  31. // 如果有return语句,放在这里
  32. // status=0申请调阅
  33. entity.setStatus(ArchiveUseListConstants.APPLY_READING);
  34. entity.setCreatedDt(new Date());
  35. UserTool tool = new UserTool(ucasAuthUserInfoService);
  36. String userPin = tool.getUserPin(request);
  37. if (!StringUtils.isEmpty(userPin)) {
  38. entity.setCreatedBy(userPin);
  39. }
  40. entity.setInfoSource(ArchiveUseListConstants.ACCESS_MUSEUM_TYPE);
  41. sid = ucasArchiveUseListService.insertSelective(entity);
  42. } catch(Exception e){
  43. log.error("生成调档单号异常:", e.getMessage(), e);
  44. }finally{
  45. rlock.unlock();
  46. }

SpringBoot 集成Redisson(SpringBoot 2.x版本)

pom.xml 集成Redisson依赖

  1. <!-- 集成redisson-->
  2. <dependency>
  3. <groupId>org.redisson</groupId>
  4. <artifactId>redisson-spring-boot-starter</artifactId>
  5. <version>3.13.6</version>
  6. </dependency>

其他维持不变

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/815237
推荐阅读
相关标签
  

闽ICP备14008679号