From d1a85c7653084f59afc7ac2903bec203f7533d03 Mon Sep 17 00:00:00 2001 From: chenbowen Date: Fri, 5 Dec 2025 15:33:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AE=9A=E6=97=B6=E8=B0=83?= =?UTF-8?q?=E5=BA=A6=E6=90=BA=E5=B8=A6=E5=85=B7=E4=BD=93=20IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../databus/TemplateDatabusRequestLogDO.java | 6 ++++++ .../databus/TemplateDatabusInvokeService.java | 15 +++++++++++++++ .../sql/template_databus_request_log.sql | 2 ++ 3 files changed, 23 insertions(+) diff --git a/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/dal/dataobject/databus/TemplateDatabusRequestLogDO.java b/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/dal/dataobject/databus/TemplateDatabusRequestLogDO.java index 14427fe7..3da29e68 100644 --- a/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/dal/dataobject/databus/TemplateDatabusRequestLogDO.java +++ b/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/dal/dataobject/databus/TemplateDatabusRequestLogDO.java @@ -45,6 +45,12 @@ public class TemplateDatabusRequestLogDO extends BaseDO { @TableField("HTTP_METHOD") private String httpMethod; + /** + * 发起请求的 IP。 + */ + @TableField("REQUEST_IP") + private String requestIp; + /** * Query 参数 JSON。 */ diff --git a/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/service/databus/TemplateDatabusInvokeService.java b/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/service/databus/TemplateDatabusInvokeService.java index d0be0ddd..85a4e978 100644 --- a/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/service/databus/TemplateDatabusInvokeService.java +++ b/zt-module-template/zt-module-template-server/src/main/java/com/zt/plat/module/template/service/databus/TemplateDatabusInvokeService.java @@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import java.net.InetAddress; import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; @@ -51,6 +52,7 @@ public class TemplateDatabusInvokeService { private static final String ENCRYPTION_TYPE = CryptoSignatureUtils.ENCRYPT_TYPE_AES; private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(5); private static final Duration READ_TIMEOUT = Duration.ofSeconds(10); + private static final String UNKNOWN_IP = "UNKNOWN"; private static final Map BASE_QUERY_PARAMS = Map.of( "businessCode", "11", "fileId", "11" @@ -73,6 +75,7 @@ public class TemplateDatabusInvokeService { .requestId(generateRequestId()) .httpMethod(DEFAULT_HTTP_METHOD) .targetUrl(TARGET_URL) + .requestIp(resolveRequestIp()) .success(Boolean.FALSE) .build(); Instant start = Instant.now(); @@ -301,4 +304,16 @@ public class TemplateDatabusInvokeService { } return value.substring(0, maxLength); } + + private String resolveRequestIp() { + try { + InetAddress localHost = InetAddress.getLocalHost(); + if (localHost != null) { + return localHost.getHostAddress(); + } + } catch (Exception ex) { + log.debug("无法获取本地请求 IP", ex); + } + return UNKNOWN_IP; + } } diff --git a/zt-module-template/zt-module-template-server/src/main/resources/sql/template_databus_request_log.sql b/zt-module-template/zt-module-template-server/src/main/resources/sql/template_databus_request_log.sql index 5112f7b2..57b44a93 100644 --- a/zt-module-template/zt-module-template-server/src/main/resources/sql/template_databus_request_log.sql +++ b/zt-module-template/zt-module-template-server/src/main/resources/sql/template_databus_request_log.sql @@ -3,6 +3,7 @@ CREATE TABLE template_databus_request_log ( REQUEST_ID VARCHAR(64) NOT NULL, TARGET_URL VARCHAR(512) NOT NULL, HTTP_METHOD VARCHAR(16) NOT NULL, + REQUEST_IP VARCHAR(64), QUERY_PARAMS TEXT, REQUEST_HEADERS TEXT, REQUEST_BODY TEXT, @@ -29,6 +30,7 @@ COMMENT ON COLUMN template_databus_request_log.ID IS '主键'; COMMENT ON COLUMN template_databus_request_log.REQUEST_ID IS '请求唯一标识'; COMMENT ON COLUMN template_databus_request_log.TARGET_URL IS '目标地址(含 Query)'; COMMENT ON COLUMN template_databus_request_log.HTTP_METHOD IS 'HTTP 方法'; +COMMENT ON COLUMN template_databus_request_log.REQUEST_IP IS '发起请求的 IP'; COMMENT ON COLUMN template_databus_request_log.QUERY_PARAMS IS 'Query 参数 JSON'; COMMENT ON COLUMN template_databus_request_log.REQUEST_HEADERS IS '请求头 JSON'; COMMENT ON COLUMN template_databus_request_log.REQUEST_BODY IS '原始请求体';