deason 4 лет назад
Родитель
Сommit
a903197c7c

+ 3 - 7
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/bootstrap/AppBootstrap.java

@@ -2,8 +2,6 @@ package cn.com.qmth.examcloud.web.bootstrap;
 
 import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
 import cn.com.qmth.examcloud.commons.util.*;
-import cn.com.qmth.examcloud.web.cloud.AppSelf;
-import cn.com.qmth.examcloud.web.cloud.AppSelfHolder;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import okhttp3.Response;
@@ -102,7 +100,7 @@ public class AppBootstrap {
             argSet.add(arg);
         }
 
-        AppSelfHolder.set(buildAppSelf());
+        // AppSelfHolder.set(buildAppSelf());
 
         String[] newArgs = argSet.toArray(new String[argSet.size()]);
         ConfigurableApplicationContext context = null;
@@ -120,12 +118,10 @@ public class AppBootstrap {
     }
 
     /**
-     * 创建 {@link AppSelf} 实例
-     *
      * @return
      * @author WANGWEI
      */
-    private static AppSelf buildAppSelf() {
+    /*private static AppSelf buildAppSelf() {
         return new AppSelf() {
 
             @Override
@@ -143,7 +139,7 @@ public class AppBootstrap {
                 return secretKey;
             }
         };
-    }
+    }*/
 
     /**
      * 通知配置中心

+ 9 - 4
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/cloud/CloudClientSupport.java

@@ -4,6 +4,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import cn.com.qmth.examcloud.web.config.SystemProperties;
 import org.apache.commons.collections.CollectionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -177,11 +178,15 @@ public abstract class CloudClientSupport {
 
 		httpHeaders.add("Trace-Id", ThreadLocalUtil.getTraceId());
 		httpHeaders.add("timestamp", String.valueOf(startTime));
-		httpHeaders.add("App-Id", String.valueOf(AppSelfHolder.get().getAppId()));
-		httpHeaders.add("App-Code", String.valueOf(AppSelfHolder.get().getAppCode()));
+		httpHeaders.add("App-Id", String.valueOf(SystemProperties.APP_ID));
+		httpHeaders.add("App-Code", String.valueOf(SystemProperties.APP_CODE));
+
+		String joinStr = StringUtil.join(
+				SystemProperties.APP_ID,
+				SystemProperties.APP_CODE,
+				startTime,
+				SystemProperties.APP_SECRET_KEY);
 
-		String joinStr = StringUtil.join(AppSelfHolder.get().getAppId(),
-				AppSelfHolder.get().getAppCode(), startTime, AppSelfHolder.get().getSecretKey());
 		byte[] bytes = SHA256.encode(joinStr);
 		String accessToken = ByteUtil.toHexAscii(bytes);
 		httpHeaders.add("Access-Token", accessToken);

+ 30 - 0
examcloud-web/src/main/java/cn/com/qmth/examcloud/web/config/SystemProperties.java

@@ -31,6 +31,36 @@ public class SystemProperties {
     @Value("${examcloud.web.sys.tempDataDir:tempDataDir}")
     private String tempDataDir;
 
+    /**
+     * 应用ID
+     */
+    public static Integer APP_ID;
+
+    /**
+     * 应用密钥
+     */
+    public static String APP_SECRET_KEY;
+
+    /**
+     * 应用编码
+     */
+    public static String APP_CODE;
+
+    @Value("${examcloud.bootstrap.app.appId:-1}")
+    public void appId(Integer appId) {
+        APP_ID = appId;
+    }
+
+    @Value("${examcloud.bootstrap.app.secretKey}")
+    public void appSecretKey(String appSecretKey) {
+        APP_SECRET_KEY = appSecretKey;
+    }
+
+    @Value("${examcloud.startup.appCode}")
+    public void appCode(String appCode) {
+        APP_CODE = appCode;
+    }
+
     public String getProfile() {
         return profile;
     }

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

@@ -2,7 +2,7 @@ package cn.com.qmth.examcloud.web.support;
 
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.util.JsonMapper;
-import cn.com.qmth.examcloud.web.cloud.AppSelfHolder;
+import cn.com.qmth.examcloud.web.config.SystemProperties;
 import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
 import cn.com.qmth.examcloud.web.exception.ApiFlowLimitedException;
 import cn.com.qmth.examcloud.web.exception.SequenceLockException;
@@ -58,22 +58,22 @@ public class CustomExceptionHandler {
         StatusResponse body = null;
 
         if (null == cause) {
-            body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "系统异常");
+            body = new StatusResponse(SystemProperties.APP_CODE + "-500", "系统异常");
             cause = e;
         } else if (cause instanceof ApiFlowLimitedException) {
             body = new StatusResponse("503", "limited. RPC");
         } else if (cause instanceof StatusException) {
             StatusException se = (StatusException) cause;
-            body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-" + se.getCode(), se.getDesc());
+            body = new StatusResponse(SystemProperties.APP_CODE + "-" + se.getCode(), se.getDesc());
         } else if (cause instanceof DataIntegrityViolationException) {
             try {
                 DataIntegrityViolationTransverter.throwIfDuplicateEntry((DataIntegrityViolationException) cause);
-                body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "数据完整性错误");
+                body = new StatusResponse(SystemProperties.APP_CODE + "-500", "数据完整性错误");
             } catch (StatusException se) {
                 body = new StatusResponse(se.getCode(), se.getDesc());
             }
         } else if (cause instanceof IllegalStateException) {
-            body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", cause.getMessage());
+            body = new StatusResponse(SystemProperties.APP_CODE + "-500", cause.getMessage());
         } else if (cause instanceof javax.validation.ConstraintViolationException) {
             javax.validation.ConstraintViolationException cvExcp = (ConstraintViolationException) cause;
             Set<ConstraintViolation<?>> constraintViolations = cvExcp.getConstraintViolations();
@@ -88,15 +88,15 @@ public class CustomExceptionHandler {
                     errorMsg.append("; ").append(cv.getMessage());
                 }
             }
-            body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", errorMsg.toString());
+            body = new StatusResponse(SystemProperties.APP_CODE + "-500", errorMsg.toString());
         } else if (cause instanceof SequenceLockException) {
-            body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", cause.getMessage());
+            body = new StatusResponse(SystemProperties.APP_CODE + "-500", cause.getMessage());
         } else if (cause instanceof org.springframework.jdbc.CannotGetJdbcConnectionException
                 || cause instanceof org.springframework.orm.jpa.JpaSystemException
                 || cause instanceof org.springframework.transaction.CannotCreateTransactionException) {
             body = new StatusResponse("503", "limited. JDBC");
         } else {
-            body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "系统异常");
+            body = new StatusResponse(SystemProperties.APP_CODE + "-500", "系统异常");
             cause = e;
         }
 
@@ -127,7 +127,7 @@ public class CustomExceptionHandler {
             }
         }
 
-        StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", errorMsg.toString());
+        StatusResponse body = new StatusResponse(SystemProperties.APP_CODE + "-500", errorMsg.toString());
         return asResult(e, body, request);
     }
 
@@ -141,7 +141,7 @@ public class CustomExceptionHandler {
      */
     @ExceptionHandler(MissingServletRequestParameterException.class)
     public ResponseEntity<StatusResponse> handleException(MissingServletRequestParameterException e, HttpServletRequest request) {
-        StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", e.getMessage());
+        StatusResponse body = new StatusResponse(SystemProperties.APP_CODE + "-500", e.getMessage());
         return asResult(e, body, request);
     }
 
@@ -155,7 +155,7 @@ public class CustomExceptionHandler {
      */
     @ExceptionHandler(MissingServletRequestPartException.class)
     public ResponseEntity<StatusResponse> handleException(MissingServletRequestPartException e, HttpServletRequest request) {
-        StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", e.getMessage());
+        StatusResponse body = new StatusResponse(SystemProperties.APP_CODE + "-500", e.getMessage());
         return asResult(e, body, request);
     }
 
@@ -169,7 +169,7 @@ public class CustomExceptionHandler {
      */
     @ExceptionHandler(HttpMessageNotReadableException.class)
     public ResponseEntity<StatusResponse> handleException(HttpMessageNotReadableException e, HttpServletRequest request) {
-        StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500",
+        StatusResponse body = new StatusResponse(SystemProperties.APP_CODE + "-500",
                 "Required request body is missing");
         return asResult(e, body, request);
     }
@@ -184,7 +184,7 @@ public class CustomExceptionHandler {
      */
     @ExceptionHandler(Exception.class)
     public ResponseEntity<StatusResponse> handleException(Exception e, HttpServletRequest request) {
-        StatusResponse body = new StatusResponse(AppSelfHolder.get().getAppCode() + "-500", "系统错误");
+        StatusResponse body = new StatusResponse(SystemProperties.APP_CODE + "-500", "系统错误");
         return asResult(e, body, request);
     }