Pārlūkot izejas kodu

高并发投票

孙永军 1 gadu atpakaļ
vecāks
revīzija
8cbd8eb1d7

+ 13 - 0
src/main/java/com/smcic/api/operate/entity/KeysConst.java

@@ -0,0 +1,13 @@
+package com.smcic.api.operate.entity;
+
+public class KeysConst {
+    public static final String TARGET = "OPERATE_TARGET_";
+
+    public static final String SOURCE = "OPERATE_DT_SOURCE_";
+
+    public static final String VOTE_LOCK = "OPERATE_SOURCE_LOCK_";
+
+    public static final String KAFKA_QUEUE = "operate_vote_queue";
+
+    public static final String KAFKA_LOCK = "OPERATE_KAFKA_LOCK";
+}

+ 4 - 3
src/main/java/com/smcic/api/operate/job/Kafka2Db.java

@@ -3,6 +3,7 @@ package com.smcic.api.operate.job;
 import cn.hutool.core.lang.generator.SnowflakeGenerator;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.smcic.api.operate.entity.KeysConst;
 import com.smcic.api.operate.entity.VoteInfo;
 import com.smcic.api.operate.service.impl.VoteInfoServiceImpl;
 import lombok.extern.log4j.Log4j;
@@ -41,11 +42,11 @@ public class Kafka2Db {
 
     @Async("taskExecutor")
     public void run() {
-        consumer.subscribe(Collections.singleton("operate_vote_queue"));
-        String lockKey = "OPERATE_KAFKA_LOCK";
+        consumer.subscribe(Collections.singleton(KeysConst.KAFKA_QUEUE));
+        String lockKey = KeysConst.KAFKA_LOCK;
         long id = snowflakeGenerator.next();
         while (true) {
-            Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, String.valueOf(id), Duration.ofSeconds(3));
+            Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, String.valueOf(id), Duration.ofSeconds(20));
             if (Boolean.FALSE.equals(success)){
                 break;
             }

+ 8 - 7
src/main/java/com/smcic/api/operate/service/impl/VoteInfoServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.smcic.api.operate.entity.EnrollInfo;
+import com.smcic.api.operate.entity.KeysConst;
 import com.smcic.api.operate.entity.VoteInfo;
 import com.smcic.api.operate.mapper.VoteInfoMapper;
 import com.smcic.api.operate.service.IVoteInfoService;
@@ -68,13 +69,13 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
         if(null == one){
             throw new APIException(400, "不存在的目标");
         }*/
-        String one = redisTemplate.opsForValue().get("OPERATE_TARGET_" + operateId + "_" + target);
+        String one = redisTemplate.opsForValue().get(KeysConst.TARGET + operateId + "_" + target);
 
         if (null == one){
             throw new APIException(400, "不存在的目标");
         }
 
-        String vds = redisTemplate.opsForValue().get("OPERATE_DT_SOURCE_" + operateId + "_"+ dt + "_" + source);
+        String vds = redisTemplate.opsForValue().get(KeysConst.SOURCE + operateId + "_"+ dt + "_" + source);
         long vd;
         if(TextUtils.isEmpty(vds)){
             vd = 0L;
@@ -94,12 +95,12 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
 
         long id = snowflakeGenerator.next();
 
-        String lockKey = "OPERATE_SOURCE_LOCK_" + operateId + "_" + source;
+        String lockKey = KeysConst.VOTE_LOCK + operateId + "_" + source;
         while (true){
             Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, String.valueOf(id), Duration.ofSeconds(3));
 
             if (Boolean.TRUE.equals(success)){
-                vds = redisTemplate.opsForValue().get("OPERATE_DT_SOURCE_" + operateId + "_"+ dt + "_" + source);
+                vds = redisTemplate.opsForValue().get(KeysConst.SOURCE + operateId + "_"+ dt + "_" + source);
                 if(TextUtils.isEmpty(vds)){
                     vd = 0L;
                 }else{
@@ -116,15 +117,15 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
 
 
                 try {
-                    kafkaTemplate.send("operate_vote_queue",
+                    kafkaTemplate.send(KeysConst.KAFKA_QUEUE,
                             objectMapper.writeValueAsString(
                                     new VoteInfo(target, source, client, LocalDateTime.now(), LocalDate.now())
                     ));
                 } catch (JsonProcessingException e) {
                     throw new RuntimeException(e);
                 }
-                redisTemplate.opsForValue().increment("OPERATE_DT_SOURCE_" + operateId + "_"+ dt + "_" + source);
-                redisTemplate.expire("OPERATE_DT_SOURCE_" + operateId + "_"+ dt + "_" + source, Duration.ofDays(1));
+                redisTemplate.opsForValue().increment(KeysConst.SOURCE + operateId + "_"+ dt + "_" + source);
+                redisTemplate.expire(KeysConst.SOURCE + operateId + "_"+ dt + "_" + source, Duration.ofDays(1));
                 redisTemplate.delete(lockKey);
                 break;
             }