Authored by LUOXC

钱包提现加入分布式锁

1 package com.yohoufo.user.controller.wallet; 1 package com.yohoufo.user.controller.wallet;
2 2
  3 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
3 import com.yoho.core.rest.client.ServiceCaller; 4 import com.yoho.core.rest.client.ServiceCaller;
4 import com.yoho.service.model.uic.request.UserSmsSendReqBO; 5 import com.yoho.service.model.uic.request.UserSmsSendReqBO;
5 import com.yoho.service.model.wallet.request.WalletChangeBO; 6 import com.yoho.service.model.wallet.request.WalletChangeBO;
6 import com.yoho.tools.common.beans.ApiResponse; 7 import com.yoho.tools.common.beans.ApiResponse;
7 import com.yohoufo.common.annotation.IgnoreSession; 8 import com.yohoufo.common.annotation.IgnoreSession;
  9 +import com.yohoufo.common.exception.UfoServiceException;
8 import com.yohoufo.common.interceptor.RemoteIPInterceptor; 10 import com.yohoufo.common.interceptor.RemoteIPInterceptor;
  11 +import com.yohoufo.common.lock.RedisLock;
  12 +import com.yohoufo.common.lock.RedisLockFactory;
9 import com.yohoufo.user.requestVO.UserSmsSendReqVO; 13 import com.yohoufo.user.requestVO.UserSmsSendReqVO;
10 import com.yohoufo.user.requestVO.WalletChangeVO; 14 import com.yohoufo.user.requestVO.WalletChangeVO;
11 import org.slf4j.Logger; 15 import org.slf4j.Logger;
@@ -17,6 +21,8 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -17,6 +21,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
17 import org.springframework.web.bind.annotation.ResponseBody; 21 import org.springframework.web.bind.annotation.ResponseBody;
18 import org.springframework.web.bind.annotation.RestController; 22 import org.springframework.web.bind.annotation.RestController;
19 23
  24 +import java.util.concurrent.TimeUnit;
  25 +
20 26
21 @RestController 27 @RestController
22 public class WalletChangeController { 28 public class WalletChangeController {
@@ -26,6 +32,9 @@ public class WalletChangeController { @@ -26,6 +32,9 @@ public class WalletChangeController {
26 @Autowired 32 @Autowired
27 private ServiceCaller serviceCaller; 33 private ServiceCaller serviceCaller;
28 34
  35 + @Autowired
  36 + private RedisLockFactory redisLockFactory;
  37 +
29 38
30 /** 39 /**
31 * 钱包提现 40 * 钱包提现
@@ -34,13 +43,24 @@ public class WalletChangeController { @@ -34,13 +43,24 @@ public class WalletChangeController {
34 */ 43 */
35 44
36 @RequestMapping(params = "method=ufo.wallet.assertWithdraw") 45 @RequestMapping(params = "method=ufo.wallet.assertWithdraw")
37 - public ApiResponse assetWithdraw( WalletChangeVO walletChangeVO){ 46 + public ApiResponse assetWithdraw(WalletChangeVO walletChangeVO) {
38 logger.info("enter ufo.wallet.assertWithdraw with param {}", walletChangeVO); 47 logger.info("enter ufo.wallet.assertWithdraw with param {}", walletChangeVO);
39 - WalletChangeBO reqBO = new WalletChangeBO();  
40 - BeanUtils.copyProperties(walletChangeVO, reqBO);  
41 - String ip = RemoteIPInterceptor.getRemoteIP();  
42 - reqBO.setIp(ip);  
43 - return serviceCaller.call("wallet.assetWithdraw", reqBO, ApiResponse.class); 48 + RedisLock lock = redisLockFactory.newLock(RedisKeyBuilder.newInstance()
  49 + .appendFixed("ufo:wallet:assertWithdraw:lock:")
  50 + .appendVar(walletChangeVO.getUid() + "-" + walletChangeVO.getVerifyCode()), 5, TimeUnit.SECONDS);
  51 + if (lock.tryLock()) {
  52 + try {
  53 + WalletChangeBO reqBO = new WalletChangeBO();
  54 + BeanUtils.copyProperties(walletChangeVO, reqBO);
  55 + String ip = RemoteIPInterceptor.getRemoteIP();
  56 + reqBO.setIp(ip);
  57 + return serviceCaller.call("wallet.assetWithdraw", reqBO, ApiResponse.class);
  58 + } finally {
  59 + lock.unlock();
  60 + }
  61 + } else {
  62 + throw new UfoServiceException(400, "钱包提现处理中,稍后再试");
  63 + }
44 } 64 }
45 65
46 /** 66 /**