feature 1.更换redis锁

release
zhenghaiyang@ele-cloud.com 2 years ago
parent cde377881e
commit ac87ae0232
  1. 2
      order-management-consumer/pom.xml
  2. 7
      order-management-invoice/pom.xml
  3. 40
      order-management-invoice/src/main/java/com/dxhy/order/invoice/config/RedissonConfig.java
  4. 16
      order-management-invoice/src/main/java/com/dxhy/order/invoice/module/invoice/service/impl/OrderInvoiceInfoServiceImpl.java

@ -16,8 +16,6 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- Spring Boot dependencies --> <!-- Spring Boot dependencies -->
<!--Spring Boot 初始化引用--> <!--Spring Boot 初始化引用-->
<dependency> <dependency>

@ -17,8 +17,11 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.23.1</version>
</dependency>
<!-- Spring Boot dependencies --> <!-- Spring Boot dependencies -->
<!--Spring Boot 初始化引用--> <!--Spring Boot 初始化引用-->
<dependency> <dependency>

@ -0,0 +1,40 @@
//package com.dxhy.order.invoice.config;
//
//import org.apache.commons.lang3.StringUtils;
//import org.redisson.Redisson;
//import org.redisson.api.RedissonClient;
//import org.redisson.config.Config;
//import org.redisson.config.ReadMode;
//import org.redisson.config.SentinelServersConfig;
//import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
//import org.springframework.context.annotation.Bean;
//
//import javax.annotation.Resource;
//import java.util.List;
//
//public class RedissonConfig {
//
// @Resource
// private RedisProperties redisProperties;
//
// //哨兵模式配置
// @Bean
// RedissonClient redissonSentinel() {
// Config config = new Config();
// RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
// List<String> newNodes = sentinel.getNodes();
// newNodes.stream().forEach((index) -> newNodes.add(
// index.startsWith("redis://") ? index : "redis://" + index));
//
// SentinelServersConfig serverConfig = config.useSentinelServers()
// .addSentinelAddress(newNodes.toArray(new String[0]))
// .setMasterName(sentinel.getMaster())
// .setReadMode(ReadMode.SLAVE);
//
// if (StringUtils.isNotBlank(redisProperties.getPassword())) {
// serverConfig.setPassword(redisProperties.getPassword());
// }
// return Redisson.create(config);
// }
//
//}

@ -66,9 +66,10 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.xmlbeans.impl.jam.xml.TunnelledException;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.Duration; import org.joda.time.Duration;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
@ -1704,6 +1705,9 @@ public class OrderInvoiceInfoServiceImpl implements OrderInvoiceInfoService {
return redInvoiceRespPo; return redInvoiceRespPo;
} }
@Resource
private RedissonClient redisson;
@SneakyThrows @SneakyThrows
@Override @Override
public R stageRedInvoiceData(RedInvoiceStageReqPo redInvoiceSaveReqPo){ public R stageRedInvoiceData(RedInvoiceStageReqPo redInvoiceSaveReqPo){
@ -1714,10 +1718,12 @@ public class OrderInvoiceInfoServiceImpl implements OrderInvoiceInfoService {
//组装发票报文 //组装发票报文
//金额、数量、税额跟跟换为负数,折扣行要与被折扣行合并 //金额、数量、税额跟跟换为负数,折扣行要与被折扣行合并
//校验重复提交数据覆盖问题 //校验重复提交数据覆盖问题
boolean redRedisLock = redisService.setNx(redInvoiceSaveReqPo.getFpqqlsh(), key, RedisConstant.REDIS_EXPIRE_TIME_2HOUR); RLock lock = redisson.getLock(redInvoiceSaveReqPo.getFpqqlsh());
// boolean redRedisLock = redisService.setNx(redInvoiceSaveReqPo.getFpqqlsh(), key, RedisConstant.REDIS_EXPIRE_TIME_2HOUR);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
try { try {
if(redRedisLock){ if(!lock.isLocked()){
lock.lock();
Map<String, String> paramMap = redisService.pullAllHashMap(key); Map<String, String> paramMap = redisService.pullAllHashMap(key);
if(paramMap != null){ if(paramMap != null){
String value = redisService.pullHashMap(key, innerKey); String value = redisService.pullHashMap(key, innerKey);
@ -1744,14 +1750,14 @@ public class OrderInvoiceInfoServiceImpl implements OrderInvoiceInfoService {
redisService.set(redSyjeKey,syfpje); redisService.set(redSyjeKey,syfpje);
hashMap.put(redItemList.get(0).getId(),syfpje); hashMap.put(redItemList.get(0).getId(),syfpje);
} }
redisService.del(redInvoiceSaveReqPo.getFpqqlsh()); lock.unlock();
return R.ok().put(OrderManagementConstant.DATA,hashMap); return R.ok().put(OrderManagementConstant.DATA,hashMap);
} else { } else {
Thread.sleep(2000); Thread.sleep(2000);
log.info("获取redis锁请等待,三次之后还是获取锁失败则返回。。。"); log.info("获取redis锁请等待,三次之后还是获取锁失败则返回。。。");
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
redisService.del(redInvoiceSaveReqPo.getFpqqlsh()); lock.unlock();
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

Loading…
Cancel
Save