WANG 5 лет назад
Родитель
Сommit
aa166bca58

+ 13 - 1
src/main/java/cn/com/qmth/examcloud/web/cloud/CloudClientSupport.java

@@ -20,6 +20,7 @@ import cn.com.qmth.examcloud.api.commons.exchange.BaseRequest;
 import cn.com.qmth.examcloud.api.commons.exchange.FormFilePart;
 import cn.com.qmth.examcloud.api.commons.exchange.FormRequest;
 import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
+import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
@@ -29,6 +30,7 @@ import cn.com.qmth.examcloud.commons.util.SHA256;
 import cn.com.qmth.examcloud.commons.util.StringUtil;
 import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
 import cn.com.qmth.examcloud.web.config.LogProperties;
+import cn.com.qmth.examcloud.web.exception.ApiFlowLimitedException;
 import cn.com.qmth.examcloud.web.support.SpringContextHolder;
 import cn.com.qmth.examcloud.web.support.StatusResponse;
 
@@ -131,9 +133,14 @@ public abstract class CloudClientSupport {
 			}
 			T t = JsonUtil.fromJson(body, responseType);
 			return t;
-		} else {
+		} else if (HttpStatus.INTERNAL_SERVER_ERROR == respEntity.getStatusCode()) {
 			StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
 			throw new StatusException(sr.getCode(), sr.getDesc());
+		} else if (HttpStatus.SERVICE_UNAVAILABLE == respEntity.getStatusCode()) {
+			StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
+			throw new ApiFlowLimitedException(sr.getCode(), sr.getDesc());
+		} else {
+			throw new ExamCloudRuntimeException("unexpected http status code");
 		}
 	}
 
@@ -219,6 +226,11 @@ public abstract class CloudClientSupport {
 				INTERFACE_LOG.info(StringUtil.join("[CALL-OK]. url=" + url,
 						" ; cost " + (System.currentTimeMillis() - startTime), " ms."));
 			}
+		} catch (ApiFlowLimitedException e) {
+			INTERFACE_LOG.error(StringUtil.join("[CALL-FAIL]. url=" + url,
+					" ; cost " + (System.currentTimeMillis() - startTime), " ms."));
+			INTERFACE_LOG.error("[CALL-RESP]. response=" + e.toJson());
+			throw e;
 		} catch (StatusException e) {
 			INTERFACE_LOG.error(StringUtil.join("[CALL-FAIL]. url=" + url,
 					" ; cost " + (System.currentTimeMillis() - startTime), " ms."));

+ 24 - 0
src/main/java/cn/com/qmth/examcloud/web/exception/ApiFlowLimitedException.java

@@ -0,0 +1,24 @@
+package cn.com.qmth.examcloud.web.exception;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+
+/**
+ * 接口限流异常
+ *
+ * @author WANGWEI
+ * @date 2019年8月6日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public class ApiFlowLimitedException extends StatusException {
+
+	private static final long serialVersionUID = -3088431872660870243L;
+
+	public ApiFlowLimitedException(String code, String desc) {
+		super(code, desc);
+	}
+
+	public ApiFlowLimitedException(String code, String desc, Throwable cause) {
+		super(code, desc, cause);
+	}
+
+}

+ 11 - 2
src/main/java/cn/com/qmth/examcloud/web/support/CustomExceptionHandler.java

@@ -25,6 +25,7 @@ import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
 import cn.com.qmth.examcloud.web.cloud.AppSelfHolder;
 import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
+import cn.com.qmth.examcloud.web.exception.ApiFlowLimitedException;
 import cn.com.qmth.examcloud.web.exception.SequenceLockException;
 import cn.com.qmth.examcloud.web.jpa.DataIntegrityViolationTransverter;
 
@@ -219,7 +220,11 @@ public class CustomExceptionHandler {
 			} else {
 				INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body));
 			}
-			return new ResponseEntity<StatusResponse>(body, HttpStatus.OK);
+			if (t instanceof ApiFlowLimitedException) {
+				return new ResponseEntity<StatusResponse>(body, HttpStatus.SERVICE_UNAVAILABLE);
+			} else {
+				return new ResponseEntity<StatusResponse>(body, HttpStatus.OK);
+			}
 		} else {
 			INTERFACE_LOG.error("[HTTP-RESP]. status=" + HttpStatus.INTERNAL_SERVER_ERROR.value());
 			if (printStackTrace) {
@@ -227,7 +232,11 @@ public class CustomExceptionHandler {
 			} else {
 				INTERFACE_LOG.error("[HTTP-RESP]. response=" + JsonUtil.toJson(body));
 			}
-			return new ResponseEntity<StatusResponse>(body, HttpStatus.INTERNAL_SERVER_ERROR);
+			if (t instanceof ApiFlowLimitedException) {
+				return new ResponseEntity<StatusResponse>(body, HttpStatus.SERVICE_UNAVAILABLE);
+			} else {
+				return new ResponseEntity<StatusResponse>(body, HttpStatus.INTERNAL_SERVER_ERROR);
+			}
 		}
 	}