Commit faa5cc0e by guanchen

添加分布式锁

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