Commit faa5cc0e by guanchen

添加分布式锁

parent 7c77db91
......@@ -132,6 +132,11 @@
<artifactId>snakeyaml</artifactId>
<version>1.24</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.7.5</version>
</dependency>
</dependencies>
<profiles>
......
......@@ -33,6 +33,8 @@ public class PartnerApplication extends SpringBootServletInitializer {
logger.error(e.getMessage(), e);
}
}
} catch (Exception e) {
logger.error("PartnerApplication>>服务启动失败", e);
e.printStackTrace();
......
......@@ -24,6 +24,7 @@ public class Constants {
public static final int AGENT_LEVEL_3 = 3;
public static final int AGENT_LEVEL_4 = 4;
public static final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
public static final String DISTRIBUTE_REDIS_LOCK_KEY = "huhu:partner:distribute:redis:key";
static {
AGENT_RATE_MAP.put(AGENT_REWARD_TYPE_YEAR_VIP, new HashMap<Integer, BigDecimal>());
......
......@@ -11,6 +11,7 @@ import com.lanren.huhu.partner.service.AgentRewardService;
import com.lanren.huhu.partner.service.PartnerAccountService;
import com.lanren.huhu.partner.service.UserAgentService;
import com.lanren.huhu.partner.service.UserService;
import com.lanren.huhu.partner.util.LockUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -52,7 +53,7 @@ public class AgentRewardQueueTask {
@Autowired
PartnerAccountService partnerAccountService;
@Scheduled(fixedDelay = 1000L)
@Scheduled(fixedDelay = 5000L)
public void runScheduledTask() {
logger.info("run AgentRewardQueueTask");
runConsume();
......@@ -64,7 +65,19 @@ public class AgentRewardQueueTask {
while(null != ops && null != ops.size(AGENT_REWARD_QUEUE_KEY) && ops.size(AGENT_REWARD_QUEUE_KEY) > 0L) {
String msg = "";
try {
msg = ops.rightPop(AGENT_REWARD_QUEUE_KEY);
/**
* 加分布式锁
*/
LockUtil.lock(DISTRIBUTE_REDIS_LOCK_KEY);
try {
msg = ops.rightPop(AGENT_REWARD_QUEUE_KEY);
} catch (Exception e) {
logger.error(e.getMessage(), e);
continue;
} finally{
LockUtil.unlock(DISTRIBUTE_REDIS_LOCK_KEY);
}
logger.info("msg is {}", msg);
JSONObject json = JSON.parseObject(msg);
AgentRewardMessage message = json.toJavaObject(AgentRewardMessage.class);
......@@ -179,7 +192,8 @@ public class AgentRewardQueueTask {
agentReward.setAmount(message.getAgentReward().multiply(commissionRate));
agentReward.setSourceOrder(message.getReferenceId());
agentReward.setSourceUserLevel(sourceUserLevel);
if (agentRewardService.insertOrUpdate(agentReward) > 0) {
agentReward.setCreatedAt(System.currentTimeMillis() / 1000L);
if (agentRewardService.save(agentReward)) {
return true;
} else {
return false;
......
......@@ -92,15 +92,15 @@ mybatis:
configuration:
#配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
mybatis-plus:
type-aliases-package: com.lanren.huhu.partner.domain
mapper-locations: classpath:mapper/*.xml
logging:
level:
#打印SQL信息
com.hehe.mapper: debug
com.lanren.huhu.partner.dao: debug
......
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