Commit fffe49e0 authored by zhaobiyan's avatar zhaobiyan

会员id生成锁修改

parent e3e82a56
...@@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.redis.helper.RedisDistributedLock; ...@@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.redis.helper.RedisDistributedLock;
import cn.iocoder.yudao.framework.redis.helper.RedisHelper; import cn.iocoder.yudao.framework.redis.helper.RedisHelper;
import cn.iocoder.yudao.framework.redis.helper.RedisLockTemplate; import cn.iocoder.yudao.framework.redis.helper.RedisLockTemplate;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
......
...@@ -67,7 +67,7 @@ public class MemberUserScoreApiImpl implements MemberUserScoreApi{ ...@@ -67,7 +67,7 @@ public class MemberUserScoreApiImpl implements MemberUserScoreApi{
String lockKey = "member:operate:score:" + req.getMemberId(); String lockKey = "member:operate:score:" + req.getMemberId();
RLock lock = redissonClient.getLock(lockKey); RLock lock = redissonClient.getLock(lockKey);
try { try {
boolean lockSuccess = lock.tryLock(2, 2, TimeUnit.MINUTES); boolean lockSuccess = lock.tryLock(2, 2, TimeUnit.SECONDS);
if (!lockSuccess) { if (!lockSuccess) {
throw exception(GET_LOCK_FAILED); throw exception(GET_LOCK_FAILED);
} }
......
...@@ -4,15 +4,14 @@ import cn.hutool.core.date.DateUtil; ...@@ -4,15 +4,14 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.apollo.core.event.MemberRegEvent; import cn.iocoder.yudao.framework.apollo.core.event.MemberRegEvent;
import cn.iocoder.yudao.framework.common.util.spring.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils; import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.spring.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.i18n.core.I18nMessage; import cn.iocoder.yudao.framework.i18n.core.I18nMessage;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO; import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.framework.redis.helper.RedisDistributedLock;
import cn.iocoder.yudao.framework.redis.helper.RedisHelper; import cn.iocoder.yudao.framework.redis.helper.RedisHelper;
import cn.iocoder.yudao.module.ecw.api.customer.CustomerApi; import cn.iocoder.yudao.module.ecw.api.customer.CustomerApi;
import cn.iocoder.yudao.module.ecw.api.internalMessage.ClientInternalMessageApi; import cn.iocoder.yudao.module.ecw.api.internalMessage.ClientInternalMessageApi;
...@@ -49,6 +48,8 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; ...@@ -49,6 +48,8 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
...@@ -114,8 +115,7 @@ public class MemberUserServiceImpl implements MemberUserService { ...@@ -114,8 +115,7 @@ public class MemberUserServiceImpl implements MemberUserService {
private RedisHelper redisHelper; private RedisHelper redisHelper;
@Resource @Resource
private RedisDistributedLock redisDistributedLock; private RedissonClient redissonClient;
@Override @Override
public MemberUserDO getUserByMobile(String mobile) { public MemberUserDO getUserByMobile(String mobile) {
...@@ -581,15 +581,15 @@ public class MemberUserServiceImpl implements MemberUserService { ...@@ -581,15 +581,15 @@ public class MemberUserServiceImpl implements MemberUserService {
redisScript.setScriptText(incrIfExistScript); redisScript.setScriptText(incrIfExistScript);
redisScript.setResultType(Long.class); redisScript.setResultType(Long.class);
Long nextMemberCodeNumber = redisHelper.execute4Long(redisScript, Collections.singletonList(key)); Long nextMemberCodeNumber = redisHelper.execute4Long(redisScript, Collections.singletonList(key));
if (nextMemberCodeNumber != null) { if (nextMemberCodeNumber != null) {
return MemberUserCodeUtils.generateMemberCode(nextMemberCodeNumber); return MemberUserCodeUtils.generateMemberCode(nextMemberCodeNumber);
} }
boolean lock = redisDistributedLock.lock("next:member:code:lock", 2000, 3, 500); RLock lock = redissonClient.getLock("next:member:code:lock");
if (!lock) {
throw exception(GET_LOCK_FAILED);
}
try { try {
boolean lockSuccess = lock.tryLock(2, 2, TimeUnit.SECONDS);
if (!lockSuccess) {
throw exception(GET_LOCK_FAILED);
}
nextMemberCodeNumber = redisHelper.execute4Long(redisScript, Collections.singletonList(key)); nextMemberCodeNumber = redisHelper.execute4Long(redisScript, Collections.singletonList(key));
if (nextMemberCodeNumber != null) { if (nextMemberCodeNumber != null) {
return MemberUserCodeUtils.generateMemberCode(nextMemberCodeNumber); return MemberUserCodeUtils.generateMemberCode(nextMemberCodeNumber);
...@@ -598,8 +598,10 @@ public class MemberUserServiceImpl implements MemberUserService { ...@@ -598,8 +598,10 @@ public class MemberUserServiceImpl implements MemberUserService {
Long memberCodeMaxNumber = MemberUserCodeUtils.getMemberCodeNumber(currentMaxMemberCode); Long memberCodeMaxNumber = MemberUserCodeUtils.getMemberCodeNumber(currentMaxMemberCode);
redisHelper.set(key, String.valueOf(memberCodeMaxNumber), 5, TimeUnit.MINUTES); redisHelper.set(key, String.valueOf(memberCodeMaxNumber), 5, TimeUnit.MINUTES);
return MemberUserCodeUtils.generateMemberCode(redisHelper.incrBy(key, 1)); return MemberUserCodeUtils.generateMemberCode(redisHelper.incrBy(key, 1));
} catch (InterruptedException e) {
throw exception(GET_LOCK_FAILED);
} finally { } finally {
redisDistributedLock.releaseLock("next:member:code:lock"); lock.unlock();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment