mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 12:52:07 +08:00
1、增加组件接口调用日志;2、贴皮分解、贴皮管理、未排贴皮适配orderPage和goodsPage的相互调用模式;
This commit is contained in:
+79
-45
@@ -34,11 +34,6 @@ import static com.cf.imes.module.plan.enums.ErrorCodeConstants.PRODUCING_ORDER_S
|
||||
@Slf4j
|
||||
public class ProducingOrderClient {
|
||||
|
||||
/**
|
||||
* 外部接口报文的最大日志长度,避免批量序列号同步失败时输出过大的日志。
|
||||
*/
|
||||
private static final int MAX_LOG_PAYLOAD_LENGTH = 16_384;
|
||||
|
||||
private final ProducingOrderImportProperties properties;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final HttpClient httpClient;
|
||||
@@ -71,15 +66,19 @@ public class ProducingOrderClient {
|
||||
int maxAttempts = Math.max(1, properties.getNetworkRetryTimes() + 1);
|
||||
IOException lastNetworkError = null;
|
||||
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
long startedAt = System.nanoTime();
|
||||
HttpRequest request = HttpRequest.newBuilder(uri)
|
||||
.timeout(properties.getReadTimeout())
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.header("Accept", "application/json")
|
||||
.GET()
|
||||
.build();
|
||||
logComponentRequest("pull-order", "GET", uri, attempt, maxAttempts, null);
|
||||
try {
|
||||
HttpResponse<String> response = httpClient.send(
|
||||
request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
logComponentResponse("pull-order", "GET", uri, attempt, maxAttempts,
|
||||
response.statusCode(), elapsedMillis(startedAt), response.body());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new ServiceException(PRODUCING_ORDER_PULL_FAILED,
|
||||
"HTTP " + response.statusCode());
|
||||
@@ -96,13 +95,13 @@ public class ProducingOrderClient {
|
||||
"invalid JSON response");
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
logComponentFailure("pull-order", "GET", uri, attempt, maxAttempts,
|
||||
elapsedMillis(startedAt), "request interrupted", ex);
|
||||
throw new ServiceException(PRODUCING_ORDER_PULL_FAILED, "request interrupted");
|
||||
} catch (IOException ex) {
|
||||
lastNetworkError = ex;
|
||||
if (attempt < maxAttempts) {
|
||||
log.warn("Pull producing order {} failed on attempt {}, retrying",
|
||||
orderId, attempt, ex);
|
||||
}
|
||||
logComponentFailure("pull-order", "GET", uri, attempt, maxAttempts,
|
||||
elapsedMillis(startedAt), ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
String message = lastNetworkError == null ? "network error" : lastNetworkError.getMessage();
|
||||
@@ -123,8 +122,9 @@ public class ProducingOrderClient {
|
||||
ProducingOrderExtraSyncRequest request) {
|
||||
String encodedOrderId = URLEncoder.encode(orderId, StandardCharsets.UTF_8);
|
||||
URI uri = URI.create(properties.getOrderExtraUrl().replace("{id}", encodedOrderId));
|
||||
String requestBody = serializeSyncRequest(request, uri);
|
||||
HttpResponse<String> response = sendPut(
|
||||
uri, token, serializeSyncRequest(request), "order-extra");
|
||||
uri, token, requestBody, "order-extra");
|
||||
if (!StringUtils.hasText(response.body())) {
|
||||
throw new ServiceException(PRODUCING_ORDER_SYNC_FAILED,
|
||||
"order-extra: empty response");
|
||||
@@ -147,10 +147,13 @@ public class ProducingOrderClient {
|
||||
/**
|
||||
* 序列化反向同步请求。
|
||||
*/
|
||||
private String serializeSyncRequest(Object request) {
|
||||
private String serializeSyncRequest(Object request, URI uri) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(request);
|
||||
} catch (JsonProcessingException ex) {
|
||||
log.error("[producing-order-http-result] operation=order-extra method=PUT uri={} "
|
||||
+ "status=SERIALIZE_ERROR requestBody={} result={}",
|
||||
uri, request, ex.getMessage(), ex);
|
||||
throw new ServiceException(PRODUCING_ORDER_SYNC_FAILED,
|
||||
"cannot serialize request");
|
||||
}
|
||||
@@ -176,49 +179,30 @@ public class ProducingOrderClient {
|
||||
IOException lastNetworkError = null;
|
||||
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
long startedAt = System.nanoTime();
|
||||
log.info("[producing-order-http] operation={} method=PUT uri={} attempt={}/{} requestBytes={}",
|
||||
operation, uri, attempt, maxAttempts, utf8Length(body));
|
||||
log.debug("[producing-order-http-request] operation={} uri={} body={}",
|
||||
operation, uri, logPayload(body));
|
||||
logComponentRequest(operation, "PUT", uri, attempt, maxAttempts, body);
|
||||
try {
|
||||
HttpResponse<String> response = httpClient.send(
|
||||
request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
long elapsedMs = elapsedMillis(startedAt);
|
||||
String responseBody = response.body();
|
||||
logComponentResponse(operation, "PUT", uri, attempt, maxAttempts,
|
||||
response.statusCode(), elapsedMs, responseBody);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
log.error("[producing-order-http-error] operation={} method=PUT uri={} "
|
||||
+ "attempt={}/{} status={} elapsedMs={} requestBody={} responseBody={}",
|
||||
operation, uri, attempt, maxAttempts, response.statusCode(), elapsedMs,
|
||||
logPayload(body), logPayload(responseBody));
|
||||
throw new ServiceException(PRODUCING_ORDER_SYNC_FAILED,
|
||||
operation + ": HTTP " + response.statusCode()
|
||||
+ ", response=" + logPayload(responseBody));
|
||||
+ ", response=" + payload(responseBody));
|
||||
}
|
||||
log.info("[producing-order-http] operation={} method=PUT uri={} attempt={}/{} "
|
||||
+ "status={} elapsedMs={} responseBytes={}",
|
||||
operation, uri, attempt, maxAttempts, response.statusCode(), elapsedMs,
|
||||
utf8Length(responseBody));
|
||||
log.debug("[producing-order-http-response] operation={} uri={} body={}",
|
||||
operation, uri, logPayload(responseBody));
|
||||
return response;
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
log.error("[producing-order-http-error] operation={} method=PUT uri={} "
|
||||
+ "attempt={}/{} elapsedMs={} error=request-interrupted",
|
||||
operation, uri, attempt, maxAttempts, elapsedMillis(startedAt));
|
||||
logComponentFailure(operation, "PUT", uri, attempt, maxAttempts,
|
||||
elapsedMillis(startedAt), "request interrupted", ex);
|
||||
throw new ServiceException(PRODUCING_ORDER_SYNC_FAILED,
|
||||
operation + ": request interrupted");
|
||||
} catch (IOException ex) {
|
||||
lastNetworkError = ex;
|
||||
if (attempt < maxAttempts) {
|
||||
log.warn("[producing-order-http-error] operation={} method=PUT uri={} "
|
||||
+ "attempt={}/{} elapsedMs={} retry=true",
|
||||
operation, uri, attempt, maxAttempts, elapsedMillis(startedAt), ex);
|
||||
} else {
|
||||
log.error("[producing-order-http-error] operation={} method=PUT uri={} "
|
||||
+ "attempt={}/{} elapsedMs={} retry=false",
|
||||
operation, uri, attempt, maxAttempts, elapsedMillis(startedAt), ex);
|
||||
}
|
||||
logComponentFailure(operation, "PUT", uri, attempt, maxAttempts,
|
||||
elapsedMillis(startedAt), ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
String message = lastNetworkError == null
|
||||
@@ -228,17 +212,67 @@ public class ProducingOrderClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将外部接口报文限制在可控的日志长度内;空响应使用明确占位符表示。
|
||||
* 输出组件接口的完整请求信息,不记录 Authorization 请求头。
|
||||
*/
|
||||
private String logPayload(String payload) {
|
||||
private void logComponentRequest(String operation,
|
||||
String method,
|
||||
URI uri,
|
||||
int attempt,
|
||||
int maxAttempts,
|
||||
String requestBody) {
|
||||
log.info("[producing-order-http-request] operation={} method={} uri={} attempt={}/{} "
|
||||
+ "requestBytes={} requestBody={}",
|
||||
operation, method, uri, attempt, maxAttempts,
|
||||
utf8Length(requestBody), payload(requestBody));
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出组件接口的完整 HTTP 响应,非 2xx 响应使用 ERROR 级别。
|
||||
*/
|
||||
private void logComponentResponse(String operation,
|
||||
String method,
|
||||
URI uri,
|
||||
int attempt,
|
||||
int maxAttempts,
|
||||
int status,
|
||||
long elapsedMs,
|
||||
String responseBody) {
|
||||
if (status >= 200 && status < 300) {
|
||||
log.info("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
|
||||
+ "status={} elapsedMs={} responseBytes={} result={}",
|
||||
operation, method, uri, attempt, maxAttempts, status, elapsedMs,
|
||||
utf8Length(responseBody), payload(responseBody));
|
||||
return;
|
||||
}
|
||||
log.error("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
|
||||
+ "status={} elapsedMs={} responseBytes={} result={}",
|
||||
operation, method, uri, attempt, maxAttempts, status, elapsedMs,
|
||||
utf8Length(responseBody), payload(responseBody));
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出没有取得 HTTP 响应时的完整失败结果。
|
||||
*/
|
||||
private void logComponentFailure(String operation,
|
||||
String method,
|
||||
URI uri,
|
||||
int attempt,
|
||||
int maxAttempts,
|
||||
long elapsedMs,
|
||||
String result,
|
||||
Exception exception) {
|
||||
log.error("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
|
||||
+ "status=NETWORK_ERROR elapsedMs={} result={}",
|
||||
operation, method, uri, attempt, maxAttempts, elapsedMs,
|
||||
payload(result), exception);
|
||||
}
|
||||
|
||||
/** 空报文使用明确占位符,非空报文完整输出。 */
|
||||
private String payload(String payload) {
|
||||
if (!StringUtils.hasText(payload)) {
|
||||
return "<empty>";
|
||||
}
|
||||
if (payload.length() <= MAX_LOG_PAYLOAD_LENGTH) {
|
||||
return payload;
|
||||
}
|
||||
return payload.substring(0, MAX_LOG_PAYLOAD_LENGTH)
|
||||
+ "...<truncated,totalChars=" + payload.length() + ">";
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user