fuyuchuan 9 годин тому
батько
коміт
e67e218ae0

+ 2 - 1
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/DeviceDataQuery.java

@@ -112,7 +112,7 @@ public class DeviceDataQuery {
 
             log.debug("请求设备数据接口,设备数量:{}", deviceUuids.size());
             String response = HttpClientUtils.doPostJson(baseUrl, requestBody.toJSONString());
-            log.warn("接口返回数据:{}", response);
+            log.info("接口返回数据:{}", response);
 
             List<JSONObject> resultList = parseResponseData(response, transferVO.getDeviceType(), transferVO.getDevices());
             boolean useXiangyuMock = false;
@@ -549,6 +549,7 @@ public class DeviceDataQuery {
             double newValue = original + noise;
 
             System.out.println("加噪: " + key + " | 原值=" + original + " | 噪声=" + noise + " | 新值=" + newValue);
+            log.info("加噪: {} | 原值={} | 噪声={} | 新值={}", key, original, noise, newValue);
 
             // 保持原类型:原来是 String 就格式化回 String,原来是 Number 就放 Double
             if (value instanceof String) {

+ 1 - 1
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/DeviceDataSyncService.java

@@ -26,7 +26,7 @@ public class DeviceDataSyncService {
      * fixedDelay:任务执行完成后固定延迟29分钟执行下一次
      * initialDelay:初始化后立即执行第一次任务
      */
-    // @Scheduled(fixedDelay = 5 * 60 * 1000, initialDelay = 0)
+    // @Scheduled(fixedDelay = 10 * 60 * 1000, initialDelay = 0)
     // public void scheduledDeviceDataSync() {
     //     Integer tenantId = 1222;
     //     Long engineeringId = 3101100021L;

+ 32 - 4
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/HttpClientUtils.java

@@ -20,6 +20,7 @@ import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 
 @Slf4j
 public class HttpClientUtils {
@@ -111,7 +112,9 @@ public class HttpClientUtils {
     }
 
     public static String doPostJson(String url, String json) {
-        log.info(">>> 请求URL={}, 请求体={}", url, json);
+        final String requestId = UUID.randomUUID().toString();
+        final long start = System.currentTimeMillis();
+        log.info("[{}] >>> POST JSON 请求开始 URL={}, body={}", requestId, url, json);
 
         CloseableHttpResponse response = null;
         String resultString = "";
@@ -128,19 +131,44 @@ public class HttpClientUtils {
             httpPost.setEntity(entity);
             // 执行http请求
             response = HTTP_CLIENT.execute(httpPost);
-            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
+            int statusCode = response.getStatusLine() != null ? response.getStatusLine().getStatusCode() : -1;
+            resultString = response.getEntity() == null ? "" : EntityUtils.toString(response.getEntity(), "utf-8");
+
+            long costMs = System.currentTimeMillis() - start;
+            String preview = resultString == null ? "null" : (resultString.length() > 500 ? resultString.substring(0, 500) + "...(truncated)" : resultString);
+
+            if (statusCode != 200) {
+                log.warn("[{}] <<< POST JSON 响应异常 status={} costMs={} preview={}", requestId, statusCode, costMs, preview);
+            } else if (resultString == null || resultString.trim().isEmpty()) {
+                log.warn("[{}] <<< POST JSON 响应为空 status=200 costMs={}", requestId, costMs);
+            } else {
+                log.info("[{}] <<< POST JSON 响应成功 status=200 costMs={} preview={}", requestId, costMs, preview);
+            }
         } catch (Exception e) {
-            e.printStackTrace();
+            long costMs = System.currentTimeMillis() - start;
+            String root = buildRootCause(e);
+            log.error("[{}] !!! POST JSON 请求异常 costMs={} URL={} body={} exType={} exMsg={} rootCause={}",
+                    requestId, costMs, url, json, e.getClass().getName(), e.getMessage(), root, e);
         } finally {
             try {
                 if (response != null) {
                     response.close();
                 }
             } catch (IOException e) {
-                e.printStackTrace();
+                log.warn("[{}] 关闭HTTP响应失败: {}", requestId, e.getMessage(), e);
             }
         }
 
         return resultString;
     }
+
+    private static String buildRootCause(Throwable t) {
+        if (t == null) return "null";
+        Throwable cur = t;
+        while (cur.getCause() != null && cur.getCause() != cur) {
+            cur = cur.getCause();
+        }
+        String msg = cur.getMessage();
+        return cur.getClass().getName() + (msg == null ? "" : (": " + msg));
+    }
 }