orderNo生成增加redis锁

yun-zuoyi
nies 4 years ago
parent ea8ec0ed18
commit faf60dd3e5

@ -12,6 +12,7 @@ import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ImppRedis; import cn.estsh.impp.framework.boot.util.ImppRedis;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.redisson.api.RLock;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
@ -67,6 +68,10 @@ public class SynchronizedService implements ISynchronizedService {
@Override @Override
public synchronized List<SysOrderNoRule> nextOrderNo(String code, int num) { public synchronized List<SysOrderNoRule> nextOrderNo(String code, int num) {
//先拿规则 //先拿规则
String lockKey = CommonConstWords.REDIS_PREFIX_LOCK_GET_ORDER_NO + ":" + "LOCK" + ":" + code;
RLock rLock = (RLock) redisRes.getLock(lockKey);
rLock.lock();
try {
SysOrderNoRule codeRole = sysOrderNoRuleService.getSysOrderNoRuleByCode(code); SysOrderNoRule codeRole = sysOrderNoRuleService.getSysOrderNoRuleByCode(code);
if (codeRole == null) { if (codeRole == null) {
throw ImppExceptionBuilder.newInstance() throw ImppExceptionBuilder.newInstance()
@ -114,6 +119,13 @@ public class SynchronizedService implements ISynchronizedService {
// 生成单号更缓存 // 生成单号更缓存
return orderNoRuleList; return orderNoRuleList;
} catch (Exception e) {
LOGGER.error("生成单号失败,单号代码:{},num:{}", code, num, e);
} finally {
if (rLock.isHeldByCurrentThread()) {
rLock.unlock();
}
}
return null;
} }
} }

@ -0,0 +1,67 @@
package test.cn.estsh.i3plus.core.apiservice.serviceimpl.busi;
import cn.estsh.i3plus.core.api.iservice.base.ISynchronizedService;
import cn.estsh.i3plus.core.apiservice.serviceimpl.base.SynchronizedService;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.platform.bean.SysOrderNoRule;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import cn.estsh.impp.framework.run.ImppGo;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.util.ObjectUtils;
import org.testng.TestRunner;
import org.testng.annotations.Test;
import javax.annotation.Resource;
import java.util.Random;
/**
* @author ns
* orderNo
* @create 2021/9/15 0015 13:46
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ImppGo.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestSynchronizedService extends AbstractTestNGSpringContextTests {
@Autowired
private ISynchronizedService synchronizedService;
@Resource(name = CommonConstWords.IMPP_REDIS_RES)
private ImppRedis resRedis;
public static String orderNoKey = CommonConstWords.REDIS_PREFIX_LOCK_GET_ORDER_NO + ":" + "LOCK" + ":" + "PO_ORDER_NO" + ":" + "ORDERNO";
public static String serialNoKey = CommonConstWords.REDIS_PREFIX_LOCK_GET_ORDER_NO + ":" + "LOCK" + ":" + "PO_ORDER_NO" + ":" + "SERIALNO";
@Test(threadPoolSize = 5)
public void getOrderNo() {
while (true) {
try {
SysOrderNoRule sysOrderNoRule = synchronizedService.nextOrderNo("PO_ORDER_NO");
System.out.println(sysOrderNoRule);
System.out.println(sysOrderNoRule.getOrderNo());
System.out.println(sysOrderNoRule.getSerialNo());
if (!ObjectUtils.isEmpty(resRedis.getHash(orderNoKey + ":ORDERNO" ,sysOrderNoRule.getOrderNo()))){
throw new Exception("订单号重复");
}else{
resRedis.putHash(orderNoKey + ":ORDERNO",sysOrderNoRule.getOrderNo(), System.currentTimeMillis());
}
if (!ObjectUtils.isEmpty(resRedis.getHash(orderNoKey + ":SERIALNO", String.valueOf(sysOrderNoRule.getSerialNo())))){
throw new Exception("订单号重复");
}else {
resRedis.putHash(orderNoKey + ":SERIALNO", String.valueOf(sysOrderNoRule.getSerialNo()), System.currentTimeMillis());
}
Thread.sleep(new Random().nextInt(20));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Loading…
Cancel
Save