Ver código fonte

已部署生产环境, 增加队列排队

wuweihao 4 anos atrás
pai
commit
bc478cdd58

+ 35 - 5
gis_service/src/main/java/com/gis/service/impl/UploadServiceImpl.java

@@ -11,6 +11,7 @@ import com.gis.common.util.AliyunOssUtil;
 import com.gis.common.util.CmdUtils;
 import com.gis.common.util.Result;
 import com.gis.service.UploadService;
+import com.gis.service.queue.Variable;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +19,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.util.Date;
+import java.util.concurrent.atomic.AtomicInteger;
 
 
 /**
@@ -33,6 +35,12 @@ public class UploadServiceImpl implements UploadService {
     @Autowired
     AliyunOssUtil aliyunOssUtil;
 
+
+    /**
+     * 保证线程安全的
+     */
+    private static AtomicInteger count = new AtomicInteger();
+
     @Override
     public Result uploadFaceSwap(MultipartFile file, String type) {
         String originalFilename = file.getOriginalFilename();
@@ -45,10 +53,30 @@ public class UploadServiceImpl implements UploadService {
         String faceSwapName = time + "/faceSwap." + suffix;
         log.info("faceSwapName: " + faceSwapName);
         String ossUrl = null;
+
+        // 参数,用了区分调用不同的方法
+        String data = count.incrementAndGet()+"";
+        log.info("data: {}", data);
+        // 将数据存入队列中
+        boolean offer = Variable.queue.offer(data);
+
+        int size = Variable.queue.size();
+        log.info("当前队列数量: {}", size);
+
+        if (size > 8) {
+            // 消费消息
+            Variable.queue.poll();
+            return Result.failure(5006, "当前使用人数较多,请稍后再试~");
+        }
+
         try {
+
             FileUtil.writeFromStream(file.getInputStream(), filePath);
             log.info("文件写入完成");
 
+
+
+
             // 换装并上传
              ossUrl = cmdReloadAndUploadOss(filePath, basePath, type, time);
             return Result.success(ossUrl);
@@ -86,6 +114,8 @@ public class UploadServiceImpl implements UploadService {
         String uploadJsonPath = basePath + "/upload.json";
         if (!FileUtil.exist(uploadJsonPath)) {
             log.error("算法合成失败,upload.json不存在");
+            // 消费消息
+            Variable.queue.poll();
             throw new BaseRuntimeException(MsgCode.e5005, "算法合成失败,upload.json不存在");
         }
         log.info("换装完成: {}", basePath);
@@ -97,14 +127,14 @@ public class UploadServiceImpl implements UploadService {
         if (status != 0) {
             String msg = uploadJson.getString("msg");
             log.error("算法合成失败: " + msg);
+            // 消费消息
+            Variable.queue.poll();
             throw new BaseRuntimeException(MsgCode.e5005, "算法合成失败: " + msg);
         }
 
-        // 合成图片上传oss
-//        String ossDir = configConstant.ossBasePath + dir;
-//        aliyunOssUtil.uploadDir(basePath, ossDir);
-//        String ossUrl = configConstant.ossDomain + ossDir;
-//        log.info("oss上传完成: {}", ossUrl);
+        // 消费消息
+        Variable.queue.poll();
+
         // 更改为读取本地服务器静态资源, 前端自己拼接域名+文件名
         return dir;
 

+ 18 - 0
gis_service/src/main/java/com/gis/service/queue/Variable.java

@@ -0,0 +1,18 @@
+package com.gis.service.queue;
+
+
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+/**
+ * 多线程参数类
+ */
+public class Variable {
+
+	// 队列
+	public static ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
+	
+
+//	//计算模型的线程的最大数量
+//	public static ComputerModelThread[] thread_computers = null;
+
+}

+ 37 - 4
gis_web/src/main/java/com/gis/web/controller/UploadController.java

@@ -2,16 +2,16 @@ package com.gis.web.controller;
 
 import com.gis.common.util.Result;
 import com.gis.service.UploadService;
+import com.gis.service.queue.Variable;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 /**
  * Created by owen on 2021/4/30 0013 14:45
  */
@@ -21,6 +21,12 @@ import org.springframework.web.multipart.MultipartFile;
 @RequestMapping("api")
 public class UploadController {
 
+
+    /**
+     * 保证线程安全的
+     */
+    private static AtomicInteger count = new AtomicInteger();
+
     @Autowired
     UploadService uploadService;
 
@@ -31,6 +37,33 @@ public class UploadController {
         return uploadService.uploadFaceSwap(file, type);
     }
 
+    @ApiOperation(value = "测试队列")
+    @GetMapping("testQueue")
+    public Result testQueue(){
+
+        // 参数,用了区分调用不同的方法
+        String data = count.incrementAndGet()+"";
+        log.info("data: {}", data);
+        // 将数据存入队列中
+        boolean offer = Variable.queue.offer(data);
+
+        int size = Variable.queue.size();
+        log.info("当前队列数量: {}", size);
+
+        if (size > 8) {
+            return Result.failure(5006, "当前使用人数较多,请稍后再试~");
+        }
+
+        // 消费队列
+
+            Variable.queue.poll();
+//            Thread.sleep(1000);
+
+
+        return Result.success(size);
+    }
+
+