Authored by LUOXC

refactor

... ... @@ -5,6 +5,7 @@ import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
... ... @@ -19,31 +20,39 @@ public class RedisLock {
@Redis("gwNoSyncRedis")
private YHValueOperations valueOperations;
public boolean acquire(RedisKeyBuilder key, final long timeout, final TimeUnit unit) {
public boolean acquire(RedisKeyBuilder key, String value, final long timeout, final TimeUnit unit) {
try {
String redisLockValue = "Y";
if (redisLockValue.equals(valueOperations.get(key))) {
String redisLockValue = valueOperations.get(key);
if (value.equals(redisLockValue)) {
return true;
} else if (StringUtils.isNoneBlank(redisLockValue)) {
return false;
}
valueOperations.set(key, redisLockValue, timeout, unit);
valueOperations.set(key, value, timeout, unit);
return true;
} catch (Exception e) {
release(key);
return false;
}
}
public void release(RedisKeyBuilder key) {
public void release(RedisKeyBuilder key, String value) {
try {
redis.delete(key);
deleteKeyIfValueEquals(key, value);
} catch (Exception e) {
try {
redis.delete(key);
deleteKeyIfValueEquals(key, value);
} catch (Exception e1) {
log.warn("release lock {} fail", key, e);
}
}
}
private void deleteKeyIfValueEquals(RedisKeyBuilder key, String value) {
String redisLockValue = valueOperations.get(key);
if (value.equals(redisLockValue)) {
redis.delete(key);
}
}
}
... ...
package com.yohoufo.order.controller;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.common.annotation.IgnoreSession;
import com.yohoufo.common.annotation.IgnoreSignature;
import com.yohoufo.common.annotation.InnerApi;
import com.yohoufo.common.cache.RedisLock;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.common.utils.RandomUtil;
import com.yohoufo.order.model.QuickDeliverOrderContext;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.service.impl.TransferService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/erp/order/help")
public class OrderHelpController {
... ... @@ -18,6 +27,9 @@ public class OrderHelpController {
@Autowired
private TransferService transferService;
@Autowired
private RedisLock redisLock;
/**
* 转账
*/
... ... @@ -32,5 +44,36 @@ public class OrderHelpController {
.build();
}
/**
* 转账
*/
@IgnoreSession
@IgnoreSignature
@InnerApi
@RequestMapping(value = "/lock")
public ApiResponse lock(String key) {
RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance()
.appendFixed("ufo:order:lock:test:")
.appendVar(key);
String value = UUID.randomUUID().toString();
String message;
if (redisLock.acquire(redisLockKey, value, 5, TimeUnit.SECONDS)) {
try {
Thread.sleep(RandomUtils.nextInt(10, 50));
message = "ok";
} catch (InterruptedException e) {
message = "ko doing";
} finally {
redisLock.release(redisLockKey, value);
}
} else {
message = "ko acquire";
}
return new ApiResponse.ApiResponseBuilder()
.code(200)
.message(message)
.build();
}
}
... ...
... ... @@ -18,6 +18,7 @@ import com.yohoufo.common.alarm.EventBusPublisher;
import com.yohoufo.common.alarm.SmsAlarmEvent;
import com.yohoufo.common.cache.RedisLock;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.common.utils.StringUtil;
import com.yohoufo.dal.order.SellerOrderGoodsViewMapper;
import com.yohoufo.dal.order.model.SellerOrderGoods;
import com.yohoufo.order.model.QuickDeliverOrderContext;
... ... @@ -33,6 +34,8 @@ import com.yohoufo.order.service.seller.orderMeta.SellerOrderMetaService;
import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
import com.yohoufo.order.utils.LoggerUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
... ... @@ -40,6 +43,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
... ... @@ -87,12 +91,13 @@ public class QuickDeliverGoodsService {
RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance()
.appendFixed("ufo:order:lock:publishQuickDeliverGoods:")
.appendVar(req.getUid() + "-" + req.getStorageId());
if (redisLock.acquire(redisLockKey, 5, TimeUnit.SECONDS)) {
String value = UUID.randomUUID().toString();
if (redisLock.acquire(redisLockKey, value,5, TimeUnit.SECONDS)) {
try {
QuickDeliverOrderContext qdoc = quickDeliverPublishProcessor.buildPublishCtx(req);
return doPublish(qdoc);
} finally {
redisLock.release(redisLockKey);
redisLock.release(redisLockKey,value);
}
} else {
logger.warn("storage has publishing , {} ", redisLockKey);
... ...