|
@@ -10,8 +10,6 @@ package cn.com.qmth.examcloud.app.core.utils;
|
|
|
import cn.com.qmth.examcloud.app.model.Constants;
|
|
|
import cn.com.qmth.examcloud.app.model.ResBody;
|
|
|
import cn.com.qmth.examcloud.app.model.Result;
|
|
|
-import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLog;
|
|
|
-import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLogFactory;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import okhttp3.Request;
|
|
|
import okhttp3.RequestBody;
|
|
@@ -21,6 +19,7 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.net.ConnectException;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
import static cn.com.qmth.examcloud.app.model.Constants.*;
|
|
|
|
|
@@ -30,10 +29,14 @@ import static cn.com.qmth.examcloud.app.model.Constants.*;
|
|
|
*/
|
|
|
public class HttpUtils {
|
|
|
private static Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
|
|
- private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(HttpUtils.class);
|
|
|
+
|
|
|
+ private static String getRandomTraceId() {
|
|
|
+ //return ThreadUtils.getTraceID();
|
|
|
+ return UUID.randomUUID().toString().replace("-", "");
|
|
|
+ }
|
|
|
|
|
|
public static Result<String> doGet(String requestUrl, String key, String token) throws Exception {
|
|
|
- String traceId = ThreadUtils.getTraceID();
|
|
|
+ String traceId = getRandomTraceId();
|
|
|
|
|
|
//封装请求参数
|
|
|
Request request = new Request.Builder()
|
|
@@ -46,12 +49,12 @@ public class HttpUtils {
|
|
|
.build();
|
|
|
|
|
|
//执行请求
|
|
|
- log.info("[GET] " + requestUrl + " [TRACE_ID] " + traceId);
|
|
|
- return call(request);
|
|
|
+ log.info("[TRACE_ID] " + traceId + " [GET] " + requestUrl);
|
|
|
+ return call(request, traceId);
|
|
|
}
|
|
|
|
|
|
public static Result<String> doPost(String requestUrl, RequestBody formBody, String key, String token) throws Exception {
|
|
|
- String traceId = ThreadUtils.getTraceID();
|
|
|
+ String traceId = getRandomTraceId();
|
|
|
|
|
|
//封装请求参数
|
|
|
Request request = new Request.Builder()
|
|
@@ -62,13 +65,14 @@ public class HttpUtils {
|
|
|
.addHeader(PARAM_TRACE_ID, traceId)
|
|
|
.addHeader(PARAM_CLIENT, PARAM_CLIENT_VALUE)
|
|
|
.build();
|
|
|
+
|
|
|
//执行请求
|
|
|
- log.info("[POST] " + requestUrl + " [TRACE_ID] " + traceId);
|
|
|
- return call(request);
|
|
|
+ log.info("[TRACE_ID] " + traceId + " [POST] " + requestUrl);
|
|
|
+ return call(request, traceId);
|
|
|
}
|
|
|
|
|
|
public static Result<String> doPut(String requestUrl, RequestBody formBody, String key, String token) throws Exception {
|
|
|
- String traceId = ThreadUtils.getTraceID();
|
|
|
+ String traceId = getRandomTraceId();
|
|
|
|
|
|
//封装请求参数
|
|
|
Request request = new Request.Builder()
|
|
@@ -79,26 +83,27 @@ public class HttpUtils {
|
|
|
.addHeader(PARAM_TRACE_ID, traceId)
|
|
|
.addHeader(PARAM_CLIENT, PARAM_CLIENT_VALUE)
|
|
|
.build();
|
|
|
+
|
|
|
//执行请求
|
|
|
- log.info("[PUT] " + requestUrl + " [TRACE_ID] " + traceId);
|
|
|
- return call(request);
|
|
|
+ log.info("[TRACE_ID] " + traceId + " [PUT] " + requestUrl);
|
|
|
+ return call(request, traceId);
|
|
|
}
|
|
|
|
|
|
- public static Result<String> call(Request request) throws Exception {
|
|
|
+ public static Result<String> call(Request request, String traceId) throws Exception {
|
|
|
Response response;
|
|
|
try {
|
|
|
response = HttpClientBuilder.getClient().newCall(request).execute();
|
|
|
} catch (ConnectException e) {
|
|
|
- log.error("HttpConnectException:" + e.getMessage());
|
|
|
+ log.error("[TRACE_ID] " + traceId + " [ConnectException] " + e.getMessage());
|
|
|
return new Result<>().error("服务访问失败!");
|
|
|
}
|
|
|
|
|
|
String bodyStr = response.body().string();
|
|
|
if (response.isSuccessful()) {
|
|
|
- log.info("[Response] is success!");
|
|
|
+ log.info("[TRACE_ID] " + traceId + "[Response] is success!");
|
|
|
return new Result().success(bodyStr);
|
|
|
} else {
|
|
|
- log.warn("[Response] is fail! body:" + bodyStr);
|
|
|
+ log.warn("[TRACE_ID] " + traceId + "[Response] is fail! body:" + bodyStr);
|
|
|
ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
|
|
|
if (body != null && body.getCode() != null) {
|
|
|
if (Constants.CODE_403.equals(body.getCode())) {
|