4 Commits

Author SHA1 Message Date
yangyun b372d673db fix: 绕过DM分区表rule_version列异常 2026-09-06 23:30:32 +08:00
王绍宏 b61ec2e37c - 修复 Kafka Listener 并发配置包含尾部空格时触发 NumberFormatException 的问题
- 移除 SysLogProcessor 注解中对并发配置的直接字符串解析
- 改由 Kafka 容器工厂统一处理并发度配置
- 统一两个 consumer 模块各环境的并发配置格式
- 清理 application.properties 中可能影响类型转换的行尾空格
- 更新 Kafka 监听器拓扑回归测试,防止问题再次引入
2026-09-02 13:54:13 +08:00
王绍宏 f53661931f feat(upgrade): 增加 升级脚本和部署说明
- 新增达梦数据库结构、幂等索引、统计基线及校验脚本
- 新增 Docker Kafka Retry、DLT、Replay Topic 升级脚本
- 更新生产 Compose,启用 DIRECT 并关闭旧增量捕获和投影
- 补充数据库、Kafka、应用启动及验证说明
- 增加日志和本地生成文件忽略规则
2026-08-24 15:09:36 +08:00
王绍宏 29445c6825 feat(syslog): 完成高吞吐可靠链路与统计存储优化
- 新增接入 WAL、检查点、背压恢复及可靠分发机制,完善 Kafka 幂等发布、分区、重试、DLT 和补偿流程
- 引入规则预编译、批内快照及 JetCache/Caffeine 两级缓存,支持缓存失效广播、影子对账和执行指标
- 升级 InfluxDB V2 原始日志模型,统一接收时间与 Point 时间,支持按日 measurement、UUID 分桶及 V1/V2 双读
- 重构达梦标准化批量写入与统计链路,支持事务内直写、增量投影、幂等控制、窗口水位和日期切换
- 完善告警处理、配置校验、运行监控及测试覆盖
2026-08-20 00:18:33 +08:00
355 changed files with 21149 additions and 10429 deletions
-1
View File
@@ -1 +0,0 @@
security-xdr
-17
View File
@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="syslog-client" />
<module name="syslog-consumer" />
<module name="syslog-consumer-rule" />
<module name="syslog-serve" />
</profile>
</annotationProcessing>
</component>
</project>
-13
View File
@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/syslog-client" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/syslog-consumer" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/syslog-consumer-rule" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/syslog-consumer/docker_run.txt" charset="GBK" />
<file url="file://$PROJECT_DIR$/syslog-serve" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/syslog-serve/docker_run.txt" charset="GBK" />
<file url="PROJECT" charset="GBK" />
</component>
</project>
-14
View File
@@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
-6
View File
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
File diff suppressed because it is too large Load Diff
+1
View File
@@ -0,0 +1 @@
java=8.0.392-zulu
@@ -1,9 +1,9 @@
2026-05-14 08:53:15.386 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.haobang.syslog.SysjavacollectApplicationTests], using SpringBootContextLoader
2026-05-14 08:53:15.400 [main] INFO o.s.t.c.s.AbstractContextLoader - Could not detect default resource locations for test class [com.haobang.syslog.SysjavacollectApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
2026-05-14 08:53:15.401 [main] INFO o.s.t.c.s.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.haobang.syslog.SysjavacollectApplicationTests]: SysjavacollectApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2026-05-14 08:53:15.555 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.haobang.syslog.syslogClientApplication for test class com.haobang.syslog.SysjavacollectApplicationTests
2026-05-14 08:53:15.765 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2026-05-14 08:53:15.782 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@748741cb, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@3e44f2a5, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@295cf707, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1130520d, org.springframework.test.context.support.DirtiesContextTestExecutionListener@5f77d0f9, org.springframework.test.context.event.EventPublishingTestExecutionListener@463fd068, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@895e367, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@1b266842, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@7a3793c7, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@42b3b079, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@651aed93, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@4dd6fd0a]
2026-05-14 08:53:16.222 [main] INFO c.h.s.SysjavacollectApplicationTests - Starting SysjavacollectApplicationTests using Java 1.8.0_121 on LAPTOP-ARDUR3N0 with PID 20672 (started by chenc in E:\GIT_GOSAME\ai-security-xdr\haobang-security-xdr\syslog-client)
2026-05-14 08:53:16.223 [main] INFO c.h.s.SysjavacollectApplicationTests - No active profile set, falling back to 1 default profile: "default"
2026-05-14 08:53:17.046 [main] INFO c.h.s.SysjavacollectApplicationTests - Started SysjavacollectApplicationTests in 1.201 seconds (JVM running for 2.913)
2026-08-20 00:07:55.926 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.haobang.syslog.SysjavacollectApplicationTests], using SpringBootContextLoader
2026-08-20 00:07:55.931 [main] INFO o.s.t.c.s.AbstractContextLoader - Could not detect default resource locations for test class [com.haobang.syslog.SysjavacollectApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
2026-08-20 00:07:55.931 [main] INFO o.s.t.c.s.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.haobang.syslog.SysjavacollectApplicationTests]: SysjavacollectApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2026-08-20 00:07:56.000 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.haobang.syslog.syslogClientApplication for test class com.haobang.syslog.SysjavacollectApplicationTests
2026-08-20 00:07:56.089 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2026-08-20 00:07:56.098 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@7fd7a283, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@22f59fa, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@78fa769e, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@16612a51, org.springframework.test.context.support.DirtiesContextTestExecutionListener@54e041a4, org.springframework.test.context.event.EventPublishingTestExecutionListener@2c78324b, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@79defdc, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@18317edc, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@4e0ae11f, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@238d68ff, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@4b86805d, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@5852c06f]
2026-08-20 00:07:56.280 [main] INFO c.h.s.SysjavacollectApplicationTests - Starting SysjavacollectApplicationTests using Java 1.8.0_392 on wangshaohongdeMacBook-Pro.local with PID 23533 (started by wangshaohong in /Users/wangshaohong/WorkSpace/fufeng/ai-security-xdr-orig/ai-security-xdr/haobang-security-dm/syslog-client)
2026-08-20 00:07:56.280 [main] INFO c.h.s.SysjavacollectApplicationTests - No active profile set, falling back to 1 default profile: "default"
2026-08-20 00:07:56.613 [main] INFO c.h.s.SysjavacollectApplicationTests - Started SysjavacollectApplicationTests in 0.489 seconds (JVM running for 1.098)
@@ -1,15 +0,0 @@
FROM openjdk:8
MAINTAINER chenchunan@139.com
# 创建程序运行和日志目录
RUN mkdir -p /app
RUN mkdir -p /app/logs
WORKDIR /app
# 挂载日志运行目录
VOLUME /app/logs
ADD syslog-collector-api-1.0.0.jar syslog-collector-api.jar
EXPOSE 8290
CMD java ${JAVA_OPTS} -jar syslog-collector-api.jar
@@ -1,694 +0,0 @@
智能安全运营平台
设备API接口数据采集功能模块
开发设计说明
文档版本:V1.0.0
文档密级:内部
2026年08月24日
文档修订记录
[TABLE]
| 版本号 | 修订日期 | 修订内容 | 修订人 |
| V1.0.0 | 2026-08-24 | 初始版本,创建文档 | |
| | | | |
| | | | |
[/TABLE]
# 1 概述
1.1 编写目的
本文档描述智能安全运营平台"设备API接口数据采集功能模块"的需求与开发设计,涵盖从奇安信天眼分析平台、微步在线威胁感知平台、深信服XDR平台三类安全设备通过API接口定时采集安全数据(Web访问日志、告警日志、攻击流量、日志流量)的功能需求、非功能性需求、接口规范及详细设计方案。
本文档的预期读者为:项目开发人员、系统测试人员、项目管理人员及运维人员。
1.2 系统架构
设备API接口数据采集功能模块部署于智能安全运营平台侧,采用"定时拉取—字段映射—标准化数据格式—写入Kafka"的处理模式,整体数据流如下:
┌─────────┐ ┌─────────┐ ┌─────────┐
│ 天眼平台 │ │ 微步平台 │ │ 深信服XDR│
└────┬────┘ └────┬────┘ └─── ─┬────┘
│ HTTPS API │ │
└──────┬───────┴──────┬──────┘
▼ ▼
┌────────────────────────────┐
│ 设备API数据采集模块 │
│ (定时调度/认证/分页/映射) │
└────────────┬───────────────┘
▼
┌────────────┐ ┌──────┐
│ Kafka │───▶│ Redis │(断点/Token缓存)
└─────┬──────┘ └──────┘
▼
syslog-consumer-rule 消费处理(泛化规则)
采集源汇总:共3个设备平台、4种数据类型、2种认证机制(免密登录/HMAC签名)、3种API协议。
1.3 适用范围
本文档适用于智能安全运营平台设备API接口数据采集功能模块的设计、开发、测试与验收工作。
1.4 术语与缩略语
[TABLE]
| 术语/缩略语 | 全称 | 说明 |
| 天眼 | 奇安信威胁监测与分析系统 | 奇安信天眼分析平台,提供日志检索与告警API |
| 微步 | 微步在线威胁感知平台(TDP) | 提供攻击流量与日志流量查询API |
| XDR | Extended Detection and Response | 深信服扩展检测与响应平台,提供开放API |
| csrf_token | Cross-Site Request Forgery Token | 天眼免密登录后获取的业务请求令牌 |
| AK/SK | Access Key / Secret Key | XDR联动码解码获得的访问密钥对 |
| HMAC-SHA256 | Hash-based Message Authentication Code | 微步与XDR采用的请求签名算法 |
| IOC | Indicators of Compromise | 失陷威胁指标 |
| ATT&CK | Adversarial Tactics, Techniques & Common Knowledge | 攻击战术技术知识库 |
[/TABLE]
1.5 参考文档
[TABLE]
| 序号 | 文档名称 | 版本 |
| 1 | 奇安信网神威胁监测与分析系统(天眼-分析平台)V4.0.12.0-API调用手册V1.0 | V1.0.0 |
| 2 | 微步在线API配置文档 | 最新 |
| 3 | 深信服XDR开放API认证方案及接口开放列表 | 最新 |
| 4 | 智能安全运营平台系统架构设计文档 | V1.0.0 |
[/TABLE]
# 2 功能需求
2.1 数据采集
设备API接口数据采集模块需要从以下安全设备平台定时拉取安全数据,采集的数据类型主要包括Web访问日志、告警日志、攻击流量和日志流量。
2.1.1 天眼分析平台API接口
#### 2.1.1.1 功能需求
通过天眼分析平台API接口,定时采集以下两类数据:
• Web访问日志(skyeye-weblog):记录外部对内网的HTTP/HTTPS访问行为,包括源/目的IP、端口、URI、Host、Method、状态码、Cookie、Referer等。
• 告警日志(alarm_collection等):记录各类安全告警事件,包括规则告警、威胁情报告警、蜜罐告警、WebShell检测、Web攻击检测、IDS入侵检测等。
#### 2.1.1.2 认证机制
天眼分析平台采用免密登录(OAuth式)认证,流程如下:
1. 获取login_key(系统管理-账号管理)
2. 生成client_id = SHA256("mNSLP9UJCtBHtegjDPJnK3v" + "|" + login_key)
3. 生成client_secret = SHA256("3460681205014671737" + "|" + login_key)
4. 生成timestamp(10位Unix时间戳,与平台时差不能超过10分钟)
5. 生成X-Authorization = SHA256(JSON({"client_id":..,"username":"tapadmin"}) + timestamp + client_secret)
6. POST /skyeye/v1/admin/auth 获取access_token
7. GET /skyeye/v1/admin/auth?token=... 提取csrf_token和cookies
8. 携带csrf_token + cookies请求业务数据接口
注意:csrf_token有效期约30分钟,需在每次采集周期前重新认证;cookies需在后续请求Header中携带,格式为 key1=value1;key2=value2;。
#### 2.1.1.3 相关API接口说明
(1)Web访问日志查询接口
[TABLE]
| 属性 | 说明 |
| 接口URL | /skyeye/v1/analysis/log-search/list |
| 请求协议 | HTTPS |
| 请求方法 | GET |
| 认证方式 | csrf_token + cookies(免密登录获取) |
| 数据格式 | JSON |
[/TABLE]
请求参数:
[TABLE]
| 参数名 | 类型 | 必填 | 说明 | 示例 |
| branch_id | string | 是 | 级联单位ID | QbJK/sLHi |
| start_time | number | 是 | 开始时间(毫秒时间戳) | 1787534456117 |
| end_time | number | 是 | 结束时间(毫秒时间戳) | 1787534756117 |
| index | string | 是 | 索引名称(web日志固定为skyeye-weblog) | skyeye-weblog |
| category | string | 是 | 索引类别 | skyeye |
| offset | int | 是 | 页码,从1开始 | 1 |
| limit | int | 是 | 每页条数 | 100 |
| mode | string | 否 | 查询模式(fast_model快速查询) | fast_model |
| keyword | string | 否 | 关键词检索 | |
| csrf_token | string | 是 | 认证令牌(免密登录获取) | 8696f4a3f08fe4c48b310f269bd4adad |
| r | string | 否 | 随机数(防缓存) | 0.8929389461785662 |
[/TABLE]
响应字段(Web访问日志核心数据字段):
[TABLE]
| 字段名 | 说明 |
| serial_num | 设备序列号 |
| access_time | 访问时间 |
| sip / dip | 源IP / 目的IP |
| sport / dport | 源端口 / 目的端口 |
| uri / uri.raw | 请求URI / 原始URI |
| host / host_raw | 主机域名 |
| method | HTTP方法(GET/POST等) |
| status | HTTP状态码 |
| agent | User-Agent |
| xff | X-Forwarded-For |
| data | 请求体数据 |
| content_type | Content-Type |
| setcookie / cookie / referer | Set-Cookie / Cookie / Referer |
| authorization | Authorization头 |
| device_ip | 设备IP |
| src_mac / dst_mac | 源MAC / 目的MAC |
| vlan_id | VLAN ID |
| sess_key | 会话Key |
| priv_info | 私有信息(产品类型等) |
| geo_sip.* / geo_dip.* | 源/目的IP地理位置(城市、国家、经纬度) |
| @timestamp | ES时间戳 |
| sipv6 / dipv6 | IPv6源/目的地址 |
| origin / accept_language | Origin / Accept-Language |
[/TABLE]
(2)告警日志查询接口
[TABLE]
| 属性 | 说明 |
| 接口URL | /skyeye/v1/analysis/log-search/list |
| 请求方法 | GET |
| 索引名称 | alarm_collection, skyeye-sandbox, sandbox_mail, alarm_honeypot, alarm_yunsuo, webids-webshell_dolog, webids-webattack_dolog, webids-ids_dolog, webids-ioc_dolog |
| 查询模式 | advance_model(高级模式) |
[/TABLE]
告警日志数据字段(核心字段):
[TABLE]
| 字段名 | 说明 |
| rule_id / rule_name | 规则ID / 规则名称 |
| dolog_count | 告警次数 |
| severity | 严重级别 |
| rule_version | 规则版本 |
| sip / dip / sport / dport | 源/目的IP、端口 |
| method / host / uri | HTTP方法 / 主机 / URI |
| file_name / referer / agent / cookie / parameter | 文件名 / Referer / UA / Cookie / 参数 |
| req_header / req_body | 请求头 / 请求体 |
| rsp_status / rsp_content_length / rsp_content_type / rsp_header / rsp_body / rsp_body_len | 响应状态/长度/类型/头/体 |
| victim_type / attack_flag | 受害类型 / 攻击标识 |
| attacker / victim | 攻击者 / 受害者 |
| attack_type / attack_result | 攻击类型 / 攻击结果 |
| confidence | 置信度 |
| detail_info / solution | 详细信息 / 解决方案 |
| vuln_desc / vuln_harm / vuln_name / vuln_type | 漏洞描述/危害/名称/类型 |
| kill_chain / att_ck_all | 杀伤链 / ATT&CK |
| cve_id / bid_id / cwe_id / msb_id | CVE/BID/CWE/MSB编号 |
| webrules_tag / rule_labels | Web规则标签 |
| code_language / site_app | 代码语言 / 站点应用 |
| device_ip / vendor_id | 设备IP / 厂商ID |
[/TABLE]
(3)告警列表查询接口
[TABLE]
| 属性 | 说明 |
| 接口URL | /alarm/alarm/list |
| 请求方法 | GET |
| 认证方式 | csrf_token + cookies |
[/TABLE]
核心请求参数:start_time、end_time、threat_type、hazard_level(0低危/1中危/2高危/3危急)、host_state(攻击结果)、status(处理状态)、offset(页码)、limit(每页条数)等。
2.1.2 微步API接口
#### 2.1.2.1 功能需求
通过微步在线威胁感知平台API接口,定时采集以下两类数据:
• 攻击流量(net_data_type: ["attack"]):检测到的攻击行为日志,包括威胁名称、攻击结果、严重级别、攻击阶段、源/目的IP端口、HTTP详情、IOC等。
• 日志流量(net_data_type: ["info"]):网络流量日志记录,用于行为分析和审计。
#### 2.1.2.2 认证机制
微步平台采用HMAC-SHA256签名认证,流程如下:
1. 获取当前时间戳 Timestamp(10位Unix秒级时间戳)
2. 拼接签名数据:SignData = ApiKey + Timestamp
3. 计算签名:Sign = Base64.encodeBase64URLSafeString(HmacSHA256(SignData, Secret))
4. 构造请求URL:
POST https://domain/api/v1/log/searchBySql?api_key=<ApiKey>&auth_timestamp=<Timestamp>&sign=<Sign>
说明:Base64 URL Safe编码将"+"替换为"-"、"/"替换为"_"并去除末尾"=",防止URL参数解析不完整;请求方时间与服务端时间差须在10分钟以内,否则签名过期。
#### 2.1.2.3 相关API接口说明
[TABLE]
| 属性 | 说明 |
| 接口URL | /api/v1/log/searchBySql |
| 请求协议 | HTTPS |
| 请求方法 | POST |
| 频率限制 | 50次/秒 |
| 认证方式 | api_key + auth_timestamp + sign(HMAC-SHA256) |
| Content-Type | application/json |
[/TABLE]
请求Body参数:
[TABLE]
| 参数名 | 类型 | 必填 | 说明 | 示例 |
| net_data_type | Array | 是 | 数据类型:attack-攻击流量,info-日志流量 | ["attack"] |
| time_from | Integer | 是 | 开始时间戳(秒级) | 1787500800 |
| time_to | Integer | 是 | 截止时间戳(秒级) | 1787587199 |
| assets_group | Array | 否 | 业务组ID列表 | [] |
| search_connection | Boolean | 否 | 是否搜索关联连接 | false |
| sql | String | 否 | 查询SQL条件 | (threat.level IN ('attack','action')) |
| columns | Array | 否 | 查询字段列表(label+value对) | [{"label":"告警时间","value":"time"}] |
| asc | Boolean | 否 | 是否升序排列 | false |
| page | Object | 否 | 分页参数:cur_page, page_size, sort | {"cur_page":1,"page_size":100} |
[/TABLE]
查询字段列表(columns):
[TABLE]
| 字段(value) | 中文名(label) | 说明 |
| time | 告警时间 | 事件发生时间(秒级时间戳) |
| threat.name | 威胁名称 | 如"路径穿越攻击"、"疑似冰蝎Webshell" |
| threat.result | 攻击结果 | success/failed/unknown |
| threat.severity | 严重级别 | 0:信息, 1:低, 2:中, 3:高, 4:严重 |
| threat.phase | 攻击阶段 | 如exploit, post_exploit |
| threat.type | 威胁类型 | 如exploit, virus, c2 |
| threat.level | 威胁分类 | attack-攻击, action-事件, risk-风险 |
| threat.ioc | IOC | 威胁指标 |
| threat.is_apt | 是否APT | 0:否, 1:是 |
| threat.tag | 威胁标签 | Mitre ATT&CK标签等 |
| threat.status | 处理状态 | 处理状态标识 |
| machine | 告警主机 | 受影响主机IP |
| net.src_ip / net.src_port | 源IP / 源端口 | 攻击源地址与端口 |
| net.dest_ip / net.dest_port | 目的IP / 目的端口 | 目标地址与端口 |
| net.type | 协议 | 应用层协议(http等) |
| net.http.method / status | HTTP方法 / 状态码 | GET/POST等 / 响应状态码 |
| net.http.reqs_header / reqs_body | 请求头 / 请求体 | HTTP请求信息 |
| net.http.resp_header / resp_body | 响应头 / 响应体 | HTTP响应信息 |
| data | URL/域名 | 告警主体数据 |
| assets.name / assets.group_name | 资产名称 / 业务组 | 受影响资产信息 |
| direction | 告警方向 | in:外部攻击, out:失陷破坏, lateral:内网渗透 |
| src_tag / dest_tag | 来源/目的标签 | 内/外 |
| ip_reputation / ip_reputation_zh | IP信誉 | IP信誉标签(英文/中文) |
| hit_whitelist | 命中白名单 | 0:未命中, 1:命中 |
[/TABLE]
响应数据结构(示例):
{
"data": {
"data": [
{
"data": "URL或域名",
"index": "tdp-2026-08-24",
"src_tag": "外", "dest_tag": "内",
"ip_reputation_zh": ["网关"],
"assets": {"group_id":1, "group_name":"互联网应用", "name":["蜜罐系统"], "id":36},
"machine": "172.20.34.33",
"time": 1787545680,
"threat": {
"severity": 1, "result": "unknown", "phase": "exploit",
"level": "attack", "name": "路径穿越攻击",
"tag": ["Mitre: T1190..."], "type": "exploit",
"is_apt": 0, "status": 0
},
"id": "J7UGMqABp3egYciwfG0Z",
"net": {
"src_ip": "119.132.173.119", "src_port": 55105,
"dest_ip": "172.20.34.33", "dest_port": 80,
"type": "http",
"http": {"method":"POST", "reqs_header":"Host: ...",
"reqs_body":"...", "resp_header":"...", "resp_body":"..."}
},
"hit_whitelist": 0, "direction": "in"
}
]
},
"response_code": 0,
"verbose_msg": "OK"
}
2.1.3 XDR OpenAPI接口(深信服)
#### 2.1.3.1 功能需求
通过深信服XDR平台开放API接口,采集告警事件等安全数据,作为天眼和微步数据的补充。
#### 2.1.3.2 认证机制
深信服XDR平台采用AK/SK HMAC-SHA256签名认证,流程如下:
• 从XDR平台"配置管理-系统设置-开放性-联动码管理"页面获取联动码(authCode)。
• 联动码解码获取AK/SK(AES-CBC解密)。
• 构造请求,生成签名头;签名后直接发送请求,不可修改任何参数。
签名算法说明:
[TABLE]
| 步骤 | 说明 |
| 联动码解码 | Hex解码 -> 按"|"分割为14段 -> 通过SHA256计算AES密钥 -> AES-CBC解密第10段得到AK,第11段得到SK |
| 添加公共头 | sdk-content-type: application/json; sdk-host: {host}; sign-date: {UTC时间(yyyyMMdd'T'HHmmssZZ)} |
| 构造规范串 | METHOD + "\n" + URI(转义+末尾加/) + "\n" + QueryString(排序) + "\n" + Headers(排序key:value\n) + SignedHeaders(;) + "\n" + PayloadHash(SHA256(payload去空格+排序)) |
| 计算签名 | stringToSign = "HMAC-SHA256\n" + signDate + "\n" + SHA256(规范串);signature = HMAC-SHA256(stringToSign, SK) -> Hex编码 |
| 构造Authorization | algorithm=HMAC-SHA256, Access={AK}, SignedHeaders={排序后的header keys}, Signature={signature} |
[/TABLE]
XDR平台信息:
[TABLE]
| 属性 | 值 |
| 平台地址 | https://10.56.171.157/api/openapi/ |
| API Token | dTTQ8esIvaZOETpT4NWX |
| 接口示例 | /api/xdr/v1/assets/list(资产列表)、/api/xdr/v1/assets/department(部门资产) |
| 签名依赖库 | commons-codec(Hex/HmacUtils/DigestUtils)、Spring HttpHeaders |
[/TABLE]
# 3 非功能性需求
[TABLE]
| 需求项 | 指标 | 说明 |
| 性能 | 采集延迟 | 数据采集周期为5分钟,从设备API获取到数据写入Kafka的延迟不超过30秒 |
| 吞吐量 | 处理能力 | 单次采集批量处理1000条记录,支持多设备并行采集 |
| 可靠性 | 容错机制 | API调用失败自动重试3次(间隔递增:5s/10s/15s),重试失败记录错误日志并跳过当前批次 |
| 可靠性 | 断点续传 | 记录每批次最后采集时间戳,重启后从上次位置继续采集,不丢数据 |
| 可用性 | 认证缓存 | csrf_token(天眼)和AK/SK签名缓存于Redis,避免频繁认证 |
| 安全性 | 凭证管理 | login_key、ApiKey、Secret、authCode等敏感信息加密存储于配置文件,不记录在日志中 |
| 安全性 | HTTPS | 所有设备API调用使用HTTPS协议,关闭SSL证书校验(设备使用自签证书) |
| 可扩展性 | 设备适配 | 采用策略模式,新增设备类型只需实现统一的DataCollector接口,无需修改调度逻辑 |
| 可维护性 | 配置化 | 设备地址、认证信息、采集间隔、数据类型等全部通过application.yml配置,支持热刷新 |
| 采集监控 | 采集统计 | 记录每次采集的设备名、数据类型、采集条数、耗时、成功/失败状态,写入采集日志表 |
[/TABLE]
# 4 假设与约束条件
4.1 时间周期
[TABLE]
| 约束项 | 约束值 | 说明 |
| 天眼认证有效期 | 约30分钟 | csrf_token有效期约30分钟,需在每次采集周期前检查并刷新 |
| 微步签名有效期 | 10分钟 | auth_timestamp与服务端时间差须在10分钟以内 |
| 天眼时间同步 | 10分钟 | 第三方系统时间与天眼分析平台时间差不能超过10分钟 |
| 微步频率限制 | 50次/秒 | 微步API调用频率限制为50次/秒,需控制并发请求速率 |
| 采集间隔 | 5分钟 | 默认每5分钟采集一次,可配置 |
| 时间窗口重叠 | -1分钟 | 每次采集的时间窗口结束时间为当前时间减1分钟,开始时间为上次采集结束时间 |
| 分页大小 | 100条/页 | 单次请求最大100条,需循环分页拉取 |
[/TABLE]
# 5 开发设计
5.1 数据采集处理流程
数据采集处理主流程(9步):
定时触发 -> 认证管理 -> 构造请求 -> 调用API -> 解析响应
-> 字段映射(标准化数据格式) -> 写入Kafka(指定topic) -> 更新断点 -> 记录日志
5.1.1 认证管理流程
[TABLE]
| 设备 | 认证策略 |
| 天眼 | 1. 检查Redis中缓存的csrf_token是否有效(TTL=25分钟)2. 若无效,执行免密登录流程:生成client_id/secret/signature -> POST获取access_token -> GET提取csrf_token+cookies3. 缓存csrf_token和cookies到Redis(TTL=25分钟) |
| 微步 | 1. 每次请求实时生成签名(Timestamp + HmacSHA256)2. 无需缓存Token,但Api_key和Secret从配置文件读取 |
| 深信服XDR | 1. 启动时解码联动码获取AK/SK,缓存到内存2. 每次请求实时生成sign-date和签名头3. 签名后直接发送,不可修改请求 |
[/TABLE]
5.1.2 分页拉取流程
int page = 1;
int pageSize = 100;
boolean hasMore = true;
while (hasMore) {
params.put("offset", page);
params.put("limit", pageSize);
JSONObject response = httpClient.get(apiUrl, params, headers);
JSONArray items = response.getJSONObject("data").getJSONArray("items");
int total = response.getJSONObject("data").getInt("total");
for (Object item : items) {
StandardMessage msg = fieldMapper.map((JSONObject) item, deviceType);
kafkaTemplate.send(topic, JSON.toJSONString(msg));
}
hasMore = (page * pageSize) < total;
page++;
Thread.sleep(rateLimitInterval); // 限流控制(微步50/s)
}
5.1.3 断点续传机制
使用Redis记录每个设备+数据类型的最后采集时间戳,确保重启后不丢数据:
// Redis Key设计
key: device:api:last_collect_time:{deviceIp}:{deviceType}:{dataType}
value: 1787534756117 (最后成功采集的数据时间戳)
TTL: 7天 (过期自动清理)
// 采集时间窗口计算
long lastTime = redis.get(key); // 上次采集结束时间
long endTime = System.currentTimeMillis(); // 当前时间
long startTime = lastTime - 60000; // 回退1分钟,防止边界丢数据
5.2 设备API接口设计
5.2.1 统一采集器接口
/**
* 设备数据采集器统一接口
* 所有设备采集器实现此接口,由调度器统一管理
*/
public interface DeviceDataCollector {
/** 获取设备类型 */
String getDeviceType();
/** 认证/获取访问凭证 */
void authenticate() throws CollectorException;
/**
* 采集数据
* @param dataType 数据类型(weblog/alarm/attack/info等)
* @param startTime 开始时间
* @param endTime 结束时间
* @return 采集结果统计
*/
CollectResult collect(String dataType, long startTime, long endTime)
throws CollectorException;
/** 检查认证是否有效 */
boolean isAuthenticated();
}
5.2.2 天眼采集器设计
@Slf4j
@Component
public class TianyanDataCollector implements DeviceDataCollector {
@Value("${device.tianyan.host}") private String host;
@Value("${device.tianyan.loginKey}") private String loginKey;
@Value("${device.tianyan.branchId}") private String branchId;
private static final String CLIENT_ID_SEED = "mNSLP9UJCtBHtegjDPJnK3v";
private static final String CLIENT_SECRET_SEED = "3460681205014671737";
private static final String AUTH_URI = "/skyeye/v1/admin/auth";
private static final String LOG_SEARCH_URI = "/skyeye/v1/analysis/log-search/list";
private static final String CSRF_TOKEN_CACHE_KEY = "device:tianyan:csrf_token";
private static final String COOKIES_CACHE_KEY = "device:tianyan:cookies";
private static final long CSRF_TOKEN_TTL = 25 * 60; // 25分钟
@Override
public void authenticate() throws CollectorException {
// 1. 检查Redis缓存 2. 生成client_id/client_secret
// 3. 生成X-Authorization签名 4. POST获取access_token
// 5. GET提取csrf_token+cookies 6. 缓存到Redis
}
@Override
public CollectResult collect(String dataType, long startTime, long endTime) {
// weblog: index=skyeye-weblog, category=skyeye, mode=fast_model
// alarm : index=alarm_collection,... category=event, mode=advance_model
// 分页拉取并写入Kafka
}
}
5.2.3 微步采集器设计
@Slf4j
@Component
public class WeibuDataCollector implements DeviceDataCollector {
@Value("${device.weibu.host}") private String host;
@Value("${device.weibu.apiKey}") private String apiKey;
@Value("${device.weibu.secret}") private String secret;
private static final String SEARCH_BY_SQL_URI = "/api/v1/log/searchBySql";
/**
* 生成微步API签名:
* Sign = Base64.encodeBase64URLSafeString(HmacSHA256(ApiKey+Timestamp, Secret))
*/
private String generateSign(long timestamp) {
String signData = apiKey + timestamp;
byte[] hmacBytes = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secret).hmac(signData);
return Base64.encodeBase64URLSafeString(hmacBytes);
}
@Override
public CollectResult collect(String dataType, long startTime, long endTime) {
long timestamp = System.currentTimeMillis() / 1000;
String sign = generateSign(timestamp);
String url = host + SEARCH_BY_SQL_URI
+ "?api_key=" + apiKey + "&auth_timestamp=" + timestamp + "&sign=" + sign;
// 构造Body: net_data_type/time_from/time_to/sql/columns/分页参数
// 分页拉取...
}
}
5.2.4 深信服XDR采集器设计(暂缓)
@Slf4j
@Component
public class XdrDataCollector implements DeviceDataCollector {
@Value("${device.xdr.host}") private String host;
@Value("${device.xdr.authCode}") private String authCode;
private String ak; private String sk;
@PostConstruct
public void init() {
String[] akSk = decodeAuthCode(authCode);
this.ak = akSk[0]; this.sk = akSk[1];
}
/** 对请求进行HMAC-SHA256签名 */
private HttpRequest signRequest(HttpRequest request) {
String signDate = DateFormatUtils.formatUTC(new Date(), "yyyyMMdd'T'HHmmssZZ");
request.header("sdk-content-type", "application/json");
request.header("sdk-host", request.uri().getHost());
request.header("sign-date", signDate);
String canonicalStr = buildCanonicalStr(request);
String canonicalHash = Hex.encodeHexString(DigestUtils.sha256(canonicalStr), false);
String stringToSign = "HMAC-SHA256\n" + signDate + "\n" + canonicalHash;
String signature = Hex.encodeHexString(
new HmacUtils(HmacAlgorithms.HMAC_SHA_256, sk).hmac(stringToSign), false);
String authorization = String.format(
"algorithm=HMAC-SHA256, Access=%s, SignedHeaders=%s, Signature=%s",
ak, getSignedHeaders(request), signature);
request.header("Authorization", authorization);
return request;
}
}
5.3 数据表设计
5.3.1 设备API采集配置表
-- 达梦DM8 DDL
CREATE TABLE device_api_config (
id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 主键,自增
device_id INT, -- 设备ID,关联 device_device.id
device_type VARCHAR(50) NOT NULL, -- 设备类型: tianyan/weibu/xdr
device_name VARCHAR(100) NOT NULL, -- 设备名称
device_ip VARCHAR(200) NOT NULL, -- 设备IP地址
host_api VARCHAR(200) NOT NULL, -- 设备API地址
auth_config TEXT, -- 认证配置JSON
data_types VARCHAR(500) NOT NULL, -- 数据类型: weblog,alarm / attack,info
collect_interval_sec INT DEFAULT 300, -- 采集间隔(秒)
enabled INT DEFAULT 1, -- 是否启用: 0-禁用, 1-启用
remark VARCHAR(500), -- 备注
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE device_api_config IS '设备API采集配置表';
5.3.2 采集任务执行日志表
CREATE TABLE device_api_collect_log (
id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 主键,自增
device_id INT NOT NULL, -- 设备ID,关联 device_device.id
device_type VARCHAR(50) NOT NULL, -- 设备类型
data_type VARCHAR(50) NOT NULL, -- 数据类型
start_time TIMESTAMP NOT NULL, -- 采集窗口开始时间
end_time TIMESTAMP NOT NULL, -- 采集窗口结束时间
total_count INT DEFAULT 0, -- 采集记录总数
success_count INT DEFAULT 0, -- 成功写入数
fail_count INT DEFAULT 0, -- 失败数
cost_ms BIGINT DEFAULT 0, -- 耗时(毫秒)
status VARCHAR(20) NOT NULL, -- 状态: SUCCESS/FAIL
error_msg TEXT, -- 错误信息
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE device_api_collect_log IS '设备API采集执行日志';
5.3.3 标准化消息格式(Kafka消息体)
[receive_time=20260825143015235 device_id=1001 device_name=天眼01 vendor=天眼, data_type=json device_collect_id=1 eventId=20260822-f47ac10b-58cc-4372-a567-0e02b2c3d479]
{
"deviceType": "tianyan",
"dataType": "alarm",
"collectTime": 1787534756117,
"rawData": { ... 各设备原始JSON ... }
}
5.4 核心类设计
[TABLE]
| 类名 | 类型 | 职责 |
| DeviceDataCollector | 接口 | 定义统一的采集器接口规范 |
| TianyanDataCollector | 实现类 | 天眼平台数据采集,含免密登录认证、Web日志/告警日志查询 |
| WeibuDataCollector | 实现类 | 微步平台数据采集,含HMAC-SHA256签名、攻击/日志流量查询 |
| XdrDataCollector | 实现类 | 深信服XDR数据采集,含联动码解码、AK/SK签名 |
| CollectScheduler | 调度器 | 基于Spring @Scheduled定时调度,管理所有采集器的执行周期 |
| AuthManager | 认证管理器 | 统一管理各设备的Token缓存与刷新策略 |
| FieldMapper | 字段映射器 | 将各设备原始字段映射为统一标准化格式 |
| CollectResult | 结果对象 | 封装采集结果统计信息(总数、成功数、失败数、耗时) |
| CollectorException | 异常类 | 采集过程中异常的统一封装 |
| DeviceApiConfig | 实体类 | 设备API配置实体,对应device_api_config表 |
| DeviceApiCollectLog | 实体类 | 采集日志实体,对应device_api_collect_log表 |
[/TABLE]
5.5 定时任务设计
@Slf4j
@Component
public class CollectScheduler {
@Autowired private List<DeviceDataCollector> collectors;
@Autowired private DeviceApiConfigService configService;
@Autowired private RedisTemplate<String, String> redisTemplate;
/** 主采集任务 - 每5分钟执行 */
@Scheduled(fixedDelayString = "${device.collect.interval:300000}")
public void scheduleCollect() {
List<DeviceApiConfig> configs = configService.listEnabled();
for (DeviceApiConfig config : configs) {
for (String dataType : config.getDataTypes().split(",")) {
try {
collectForDevice(config, dataType.trim());
} catch (Exception e) {
log.error("采集失败: {}-{}", config.getDeviceType(), dataType, e);
}
}
}
}
private void collectForDevice(DeviceApiConfig config, String dataType) {
// 1. 查找采集器 2. 认证检查
// 3. 计算时间窗口(上次时间-1分钟 ~ 当前时间)
// 4. 采集 5. 更新断点 6. 记录日志
}
}
5.6 配置设计
# application.yml 设备API采集配置
device:
collect:
interval: 300000 # 采集间隔(毫秒), 默认5分钟
retry:
max-attempts: 3 # 最大重试次数
initial-delay: 5000 # 初始重试延迟(毫秒)
multiplier: 2.0 # 重试延迟倍数
kafka:
topic: device-api-collect
tianyan: # 天眼分析平台
host: https://10.56.171.xxx:443
login-key: ${TIANYAN_LOGIN_KEY}
branch-id: QbJK/sLHi
data-types: weblog,alarm
page-size: 100
weibu: # 微步在线
host: https://10.56.171.164
api-key: ${WEIBU_API_KEY}
secret: ${WEIBU_SECRET}
data-types: attack,info
rate-limit: 50 # 每秒最大请求数
page-size: 100
xdr: # 深信服XDR
host: https://10.56.171.157
auth-code: ${XDR_AUTH_CODE}
api-base: /api/openapi/
data-types: alarm,asset
page-size: 1000
# 6开发计划
6.1 任务计划
总工期 2周(2026-08-25至2026-9-3,共10个工作日),设置5个里程碑:
[TABLE]
| 里程碑 | 名称 | 计划完成日期 | 达成标准(出口准则) |
| M1 | 需求与设计基线确认 | 2026-08-25 | 设计说明评审通过并基线化;API账号凭证(天眼login_key、微步ApiKey/Secret、XDR联动码)全部到位 |
| M2 | 采集框架与环境就绪 | 2026-08-27 | 模块骨架搭建完成;两张DM8表建表并通过验证;Kafka/Redis连通;统一接口与调度器编译通过 |
| M3 | 天眼采集器完成 | 2026-08-28 | 免密登录认证调通;weblog与alarm两类数据可稳定拉取并写入Kafka;断点续传验证通过 |
| M4 | 微步与XDR采集器完成 | 2026-08-31 | 微步签名调通、attack/info双类型采集正常;XDR联动码解码与签名调通、数据可拉取 |
| M5 | 联调测试与上线 | 2026-09-04 | 7×24小时连续运行无丢数;重试/断点/限流场景测试通过;测试报告与部署方案归档 |
[/TABLE]
6.2任务分解与进度安排
6.2.1 阶段一:需求确认与基础环境准备开发(第1周,08-25~08-27)
[TABLE]
| 任务编号 | 任务名称 | 工作内容 | 工期(天) | 责任角色 |
| T1.1 | 设计评审与基线 | 组织设计说明评审,确认接口清单、字段映射规则、消息格式 | 1 | 开发负责人 |
| T1.2 | 凭证与环境确认 | 获取三类设备API凭证;确认测试环境网络可达(防火墙开通HTTPS策略) | 1 | 开发+运维 |
| T1.3 | 模块骨架搭建 | 新建syslog-collector-api模块;引入依赖(httpclient/commons-codec/fastjson);统一异常与日志规范 | 2 | 后端开发 |
| T2.1 | 数据表与实体层 | device_api_config、device_api_collect_log建表(DM8);entity/mapper/service代码生成 | 1 | 后端开发 |
| T2.2 | 统一采集器接口 | DeviceDataCollector接口、CollectResult、CollectorException定义 | 1 | 后端开发 |
| T2.3 | 调度器开发 | CollectScheduler定时调度、时间窗口计算、采集日志落库 | 2 | 后端开发 |
| T2.4 | 断点续传与重试 | Redis断点Key读写;指数退避重试(3次:5s/10s/15s);限流器 | 1 | 后端开发 |
[/TABLE]
6.2.3 阶段二:天眼、微步采集器开发(第1-2周8.28-8.31)
[TABLE]
| 任务编号 | 任务名称 | 工作内容 | 工期(天) | 责任角色 |
| T3.1 | 免密登录认证 | client_id/client_secret/X-Authorization生成;access_token获取;csrf_token+cookies提取与Redis缓存(TTL=25分钟) | 2 | 后端开发 |
| T3.2 | Web访问日志采集 | skyeye-weblog索引查询、分页拉取(100条/页)、fast_model模式适配 | 2 | 后端开发 |
| T3.3 | 告警日志采集 | 9个告警索引联合查询、advance_model模式适配、响应字段解析 | 3 | 后端开发 |
| T3.4 | 字段映射与联调 | 天眼字段映射;与真实环境联调,验证数据完整性 | 2 | 后端开发 |
| T4.1 | 签名与请求封装 | HmacSHA256+Base64URLSafe签名;api_key/auth_timestamp/sign URL构造 | 1 | 后端开发 |
| T4.2 | 查询接口对接 | searchBySql接口对接;columns字段列表构造;分页参数封装 | 2 | 后端开发 |
| T4.3 | 双类型采集与限流 | attack/info两种net_data_type采集;50次/秒限流控制;字段映射与联调 | 2 | 后端开发 |
[/TABLE]
6.2.5 阶段三:系统调试与上线(第2周,09.01~09-04)
[TABLE]
| 任务编号 | 任务名称 | 工作内容 | 工期(天) | 责任角色 |
| T5.1 | 集成测试 | 采集器并行运行7×24小时;采集量/延迟/丢数核对;Kafka消息抽检 | 2 | 开发+测试 |
| T5.2 | 异常场景测试 | Token过期重认证、API失败重试、服务重启断点续传、限流压测 | 1 | 开发+测试 |
| T5.3 | 部署上线(依赖现场网络策略开通) | Docker Compose编排;生产配置核对;监控告警接入;测试报告归档 | 2 | 开发+运维 |
[/TABLE]
6.2.6 人员分工与资源需求
[TABLE]
| 角色 | 人数 | 主要职责 | 投入占比 |
| 开发负责人 | 1 | 设计评审、任务分派、进度与质量把控、里程碑验收 | 30% |
| 后端开发(Java) | 1 | 采集框架、三个采集器、调度与断点、字段映射编码实现及自测 | 100% |
| 测试工程师 | 1 | 测试用例编写、集成与异常场景测试、测试报告 | 100%(1周) |
| 运维工程师 | 1 | 申请网络策略开通(3台采集服务器->安全设备)、环境准备、Docker部署、监控接入 | 20% |
| 厂商接口支持 | 2 | 天眼/微步 接口问题答疑、凭证发放(按需) | 按需 |
[/TABLE]
# 7 附录
7.1天眼接口数据样例
7.2微步口数据样例
@@ -1,99 +0,0 @@
# ============================================
# Syslog Collector API 部署配置 (生产环境)
# ============================================
# 使用方法:
# 1. 先执行 Maven 打包: mvn -pl syslog-collector-api -am clean package -DskipTests
# 2. 拷贝 jar 到模块目录: cp syslog-collector-api/target/syslog-collector-api-1.0.0.jar syslog-collector-api/
# 3. 构建并启动: docker-compose -f docker-compose-collector-api-prod.yaml up -d
# 4. 查看日志: docker logs -f collector-api
# 5. 停止: docker-compose -f docker-compose-collector-api-prod.yaml down
# ============================================
services:
# ============================================
# Collector API - 设备API数据采集服务
# 天眼(Tianyan) / 微步(Weibu) / XDR
# ============================================
collector-api:
build:
context: ./collector-api-dm
dockerfile: Dockerfile
image: syslog-collector-api-dm:v1.0.1
container_name: syslog-collector-api-dm
restart: unless-stopped
environment:
# 基础环境
- TZ=Asia/Shanghai
- SPRING_PROFILES_ACTIVE=prod
# JVM 参数
- JAVA_OPTS=-Xms4096m -Xmx8192m -XX:+UseG1GC
# 采集探针ID
- DEVICE_COLLECT_ID=1
# Kafka 推送 topic
- KAFKA_COLLECTOR_TOPIC=test-topic
# 天眼(Tianyan) 采集配置
- TIANYAN_HOST=https://10.56.170.107
- TIANYAN_LOGIN_KEY=QuJm4NwF3GMaXb2Z
- TIANYAN_USERNAME=xxadmin
- TIANYAN_BRANCH_ID=QbJK%2FsLHi
- TIANYAN_DATA_TYPES=weblog,alarm
- TIANYAN_PAGE_SIZE=100
# 微步(Weibu) 采集配置
- WEIBU_HOST=https://10.56.171.164
- WEIBU_API_KEY=57d8fb2b556a471ea36b8b4441fa1c0c
- WEIBU_SECRET=9d967876f51d4e16bbf8bde069d12a9b
- WEIBU_DATA_TYPES=attack,info
- WEIBU_RATE_LIMIT=10
- WEIBU_PAGE_SIZE=100
# XDR 采集配置 (暂缓,待联动码联调)
- XDR_HOST=https://127.0.0.1
- XDR_AUTH_CODE=
- XDR_API_BASE=/api/openapi/
- XDR_DATA_TYPES=
# 达梦数据库配置
- SPRING_DATASOURCE_URL=jdbc:dm://192.168.4.99:5237
- SPRING_DATASOURCE_USERNAME=SYSDBA
- SPRING_DATASOURCE_PASSWORD=caZ2TcmXNSW8L2Ap
- SPRING_DATASOURCE_DRIVER_CLASS_NAME=dm.jdbc.driver.DmDriver
- SPRING_DATASOURCE_HIKARI_SCHEMA=PUBLIC
# Redis 缓存配置
- SPRING_REDIS_HOST=192.168.4.99
- SPRING_REDIS_PORT=6379
- SPRING_REDIS_PASSWORD=redis_GdGWte
- SPRING_REDIS_DATABASE=0
- SPRING_REDIS_TIMEOUT=2000
- SPRING_CACHE_REDIS_TIME_TO_LIVE=600000
# Kafka 生产者配置
- SPRING_KAFKA_PRODUCER_BOOTSTRAP_SERVERS=192.168.4.99:9092
- SPRING_KAFKA_PRODUCER_PROPERTIES_RETRIES=10
- SPRING_KAFKA_PRODUCER_PROPERTIES_RETRY_BACKOFF_MS=500
- SPRING_KAFKA_PRODUCER_PROPERTIES_CONNECTIONS_MAX_IDLE_MS=600000
- SPRING_KAFKA_PRODUCER_PROPERTIES_SOCKET_KEEPALIVE_ENABLE=true
- SPRING_KAFKA_PRODUCER_PROPERTIES_REQUEST_TIMEOUT_MS=30000
- SPRING_KAFKA_PRODUCER_PROPERTIES_DELIVERY_TIMEOUT_MS=120000
# 定时任务线程池
- SPRING_TASK_SCHEDULING_POOL_SIZE=5
# Mock 服务器配置(模拟天眼/微步接口,供功能测试)
- MOCK_SERVER_ENABLED=true
- MOCK_SERVER_TOTAL_COUNT=100
ports:
- "8290:8290" # Collector API 端口
volumes:
- /home/syslog/collector-api:/app/logs
networks:
- xdr-network
# ============================================
# 网络配置
# ============================================
networks:
xdr-network:
driver: bridge
@@ -1,131 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>security-xdr</artifactId>
<groupId>com.haobang</groupId>
<version>1.0.0</version>
</parent>
<groupId>com.haobang</groupId>
<artifactId>syslog-collector-api</artifactId>
<version>1.0.0</version>
<name>syslog-collector-api</name>
<description>API data collector module (Tianyan/Weibu/XDR)</description>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis.version>3.5.10</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.40</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- 日志框架 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<!-- MyBatis核心库 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- 达梦数据库驱动 JDK1.8 -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver</artifactId>
<version>8.1.4.169</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<!-- Spring Boot 缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis 缓存实现 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<!-- Lombok (可选,简化代码) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Bouncy Castle 国密算法支持 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.78</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
@@ -1,158 +0,0 @@
-- =====================================================================
-- 设备API采集模块 建表 + 测试数据脚本
-- 适用数据库: 达梦 DM8
-- 说明:
-- 1. 先执行建表语句(表已存在时请先删除或忽略 DDL 部分)
-- 2. 再执行 INSERT 测试数据
-- 3. id 均为自增字段(IDENTITY),INSERT 无需也不允许指定 id
-- =====================================================================
-- ---------------------------------------------------------------------
-- 1. 建表: 设备API采集配置表
-- ---------------------------------------------------------------------
DROP TABLE IF EXISTS device_api_collect_log;
DROP TABLE IF EXISTS device_api_config;
CREATE TABLE device_api_config (
id BIGINT IDENTITY(1,1) PRIMARY KEY,
device_id INT, -- 设备ID,关联 device_device.id
device_type VARCHAR(50) NOT NULL, -- 设备类型: tianyan/weibu/xdr
device_name VARCHAR(100) NOT NULL, -- 设备名称
device_ip VARCHAR(200) NOT NULL, -- 设备IP地址
host_api VARCHAR(200) NOT NULL, -- 设备API URL地址
auth_config TEXT, -- 认证配置JSON
data_types VARCHAR(500) NOT NULL, -- 数据类型: weblog,alarm / attack,info
collect_interval_sec INT DEFAULT 300, -- 采集间隔(秒)
enabled INT DEFAULT 1, -- 是否启用: 0-禁用, 1-启用
remark VARCHAR(500), -- 备注
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE device_api_config IS '设备API采集配置表';
COMMENT ON COLUMN device_api_config.id IS '主键,自增';
COMMENT ON COLUMN device_api_config.device_id IS '设备ID,关联 device_device.id';
COMMENT ON COLUMN device_api_config.device_type IS '设备类型: tianyan/weibu/xdr';
COMMENT ON COLUMN device_api_config.device_name IS '设备名称';
COMMENT ON COLUMN device_api_config.device_ip IS '设备IP地址';
COMMENT ON COLUMN device_api_config.host_api IS '设备API URL地址';
COMMENT ON COLUMN device_api_config.auth_config IS '认证配置JSON';
COMMENT ON COLUMN device_api_config.data_types IS '数据类型: weblog,alarm / attack,info';
COMMENT ON COLUMN device_api_config.collect_interval_sec IS '采集间隔(秒)';
COMMENT ON COLUMN device_api_config.enabled IS '是否启用: 0-禁用, 1-启用';
COMMENT ON COLUMN device_api_config.remark IS '备注';
COMMENT ON COLUMN device_api_config.create_time IS '创建时间';
COMMENT ON COLUMN device_api_config.update_time IS '更新时间';
-- ---------------------------------------------------------------------
-- 2. 建表: 设备API采集执行日志表
-- ---------------------------------------------------------------------
CREATE TABLE device_api_collect_log (
id BIGINT IDENTITY(1,1) PRIMARY KEY,
device_id INT NOT NULL, -- 设备ID,关联 device_device.id
device_type VARCHAR(50) NOT NULL, -- 设备类型
data_type VARCHAR(50) NOT NULL, -- 数据类型
start_time TIMESTAMP NOT NULL, -- 采集窗口开始时间
end_time TIMESTAMP NOT NULL, -- 采集窗口结束时间
total_count INT DEFAULT 0, -- 采集记录总数
success_count INT DEFAULT 0, -- 成功写入数
fail_count INT DEFAULT 0, -- 失败数
cost_ms BIGINT DEFAULT 0, -- 耗时(毫秒)
status VARCHAR(20) NOT NULL, -- 状态: SUCCESS/FAIL
error_msg TEXT, -- 错误信息
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE device_api_collect_log IS '设备API采集执行日志';
COMMENT ON COLUMN device_api_collect_log.id IS '主键,自增';
COMMENT ON COLUMN device_api_collect_log.device_id IS '设备ID,关联 device_device.id';
COMMENT ON COLUMN device_api_collect_log.device_type IS '设备类型';
COMMENT ON COLUMN device_api_collect_log.data_type IS '数据类型';
COMMENT ON COLUMN device_api_collect_log.start_time IS '采集窗口开始时间';
COMMENT ON COLUMN device_api_collect_log.end_time IS '采集窗口结束时间';
COMMENT ON COLUMN device_api_collect_log.total_count IS '采集记录总数';
COMMENT ON COLUMN device_api_collect_log.success_count IS '成功写入数';
COMMENT ON COLUMN device_api_collect_log.fail_count IS '失败数';
COMMENT ON COLUMN device_api_collect_log.cost_ms IS '耗时(毫秒)';
COMMENT ON COLUMN device_api_collect_log.status IS '状态: SUCCESS/FAIL';
COMMENT ON COLUMN device_api_collect_log.error_msg IS '错误信息';
COMMENT ON COLUMN device_api_collect_log.create_time IS '创建时间';
-- =====================================================================
-- 3. 测试数据: 设备API采集配置表(id 自增,不插入)
-- 覆盖: 天眼(启用/停用)、微步(启用)、XDR(暂缓停用)
-- =====================================================================
INSERT INTO device_api_config
(device_id, device_type, device_name, device_ip, host_api, auth_config, data_types,
collect_interval_sec, enabled, remark, create_time, update_time)
VALUES
-- 天眼-主设备(启用,采集 weblog+alarm)
(35, 'tianyan', '天眼态势感知平台', '10.56.170.107',
'https://10.56.170.107',
'{"login_key":"QuJm4NwF3GMaXb2Z","username":"xxadmin","branch_id":"QbJK%2FsLHi"}',
'weblog,alarm', 300, 1, '生产环境天眼主采集器',
'2026-08-27 09:00:00', '2026-08-27 09:00:00'),
-- 天眼-备设备(停用,仅测试)
(102, 'tianyan', '天眼态势感知平台-备', '10.56.170.107',
'https://10.56.170.107',
'{"login_key":"QuJm4NwF3GMaXb2Z","username":"xxadmin","branch_id":"QbJK%2FsLHi"}',
'weblog', 600, 0, '备用采集器,暂未启用',
'2026-08-27 09:05:00', '2026-08-27 09:05:00'),
-- 微步-TDP(启用,采集 attack+info)
(36, 'weibu', '微步威胁感知平台', '10.56.171.164',
'https://10.56.171.164',
'{"api_key":"57d8fb2b556a471ea36b8b4441fa1c0c","secret":"9d967876f51d4e16bbf8bde069d12a9b"}',
'attack,info', 300, 1, '生产环境微步采集器',
'2026-08-27 09:10:00', '2026-08-27 09:10:00'),
-- XDR(暂缓,联动码待联调)
(104, 'xdr', '深信服XDR平台', '192.168.30.31',
'https://192.168.30.31',
'{"auth_code":"<待联调时填入14段联动码>"}',
'alert', 300, 0, 'XDR采集暂缓,待联动码解码联调后启用',
'2026-08-27 09:15:00', '2026-08-27 09:15:00');
-- =====================================================================
-- 4. 测试数据: 设备API采集执行日志表(id 自增,不插入)
-- 覆盖: 成功、失败、不同设备/类型
-- =====================================================================
INSERT INTO device_api_collect_log
(device_id, device_type, data_type, start_time, end_time,
total_count, success_count, fail_count, cost_ms, status, error_msg, create_time)
VALUES
-- 天眼 weblog 采集成功
(101, 'tianyan', 'weblog',
'2026-08-27 08:55:00', '2026-08-27 09:00:00',
1280, 1280, 0, 3265, 'SUCCESS', NULL,
'2026-08-27 09:00:00'),
-- 天眼 alarm 采集成功
(101, 'tianyan', 'alarm',
'2026-08-27 08:55:00', '2026-08-27 09:00:00',
36, 36, 0, 1240, 'SUCCESS', NULL,
'2026-08-27 09:00:00'),
-- 微步 attack 采集成功(含少量失败)
(103, 'weibu', 'attack',
'2026-08-27 08:55:00', '2026-08-27 09:00:00',
520, 518, 2, 1890, 'SUCCESS', '2条消息Kafka推送失败',
'2026-08-27 09:00:00'),
-- 微步 info 采集失败(模拟接口超时)
(103, 'weibu', 'info',
'2026-08-27 08:55:00', '2026-08-27 09:00:00',
0, 0, 0, 30050, 'FAIL', 'searchBySql接口请求超时: read timed out',
'2026-08-27 09:00:00'),
-- XDR(停用状态,无采集记录; 以下为历史参考记录)
(104, 'xdr', 'alert',
'2026-08-26 09:00:00', '2026-08-26 09:05:00',
0, 0, 0, 150, 'FAIL', 'XDR采集器暂缓,待联动码解码与签名接口联调后实现',
'2026-08-26 09:05:00');
-- =====================================================================
-- 5. 校验查询(可选执行)
-- =====================================================================
-- 查询启用中的采集配置
-- SELECT id, device_type, device_name, data_types, collect_interval_sec
-- FROM device_api_config
-- WHERE enabled = 1;
-- 查询最近采集日志
-- SELECT device_type, data_type, total_count, success_count, fail_count,
-- status, error_msg, create_time
-- FROM device_api_collect_log
-- ORDER BY id DESC;
@@ -1,93 +0,0 @@
-- =====================================================================
-- Mock 服务器功能测试配置脚本
-- 将天眼/微步的 host_api 指向本地 Mock 接口(复用 8290 端口,路径区分)
--
-- 前置条件:
-- 1. application.properties 中 mock.server.enabled=true
-- 2. 应用运行在 127.0.0.1:8290(context-path=/collectorapi)
--
-- 采集器 host 拼接规则: host_api + 接口路径
-- 天眼: http://127.0.0.1:8290/collectorapi/mock/tianyan + /skyeye/v1/admin/auth ...
-- 微步: http://127.0.0.1:8290/collectorapi/mock/weibu + /api/v1/log/searchBySql ...
--
-- 使用说明:
-- 执行本脚本前,请先恢复生产 host_api(执行恢复语句);
-- 测试完成后务必执行"恢复生产配置"部分,避免误连生产。
-- =====================================================================
-- =====================================================================
-- 1. 切换到本地 Mock 地址
-- =====================================================================
-- 天眼-主设备(101): weblog+alarm
UPDATE device_api_config
SET host_api = 'http://127.0.0.1:8290/collectorapi/mock/tianyan',
enabled = 1,
remark = '【功能测试】连接本地Mock,生产地址10.56.170.107',
update_time = SYSDATE
WHERE id = 101;
-- 天眼-备设备(102): weblog(启用用于并行测试)
UPDATE device_api_config
SET host_api = 'http://127.0.0.1:8290/collectorapi/mock/tianyan',
enabled = 1,
remark = '【功能测试】连接本地Mock,生产地址10.56.170.107',
update_time = SYSDATE
WHERE id = 102;
-- 微步-TDP(103): attack+info
UPDATE device_api_config
SET host_api = 'http://127.0.0.1:8290/collectorapi/mock/weibu',
enabled = 1,
remark = '【功能测试】连接本地Mock,生产地址10.56.171.164',
update_time = SYSDATE
WHERE id = 103;
-- =====================================================================
-- 2. 手动触发测试(执行后可手工调用,也可等待定时调度)
-- 天眼 weblog: curl -X POST "http://127.0.0.1:8290/collectorapi/api/collect/trigger?id=101&dataType=weblog&minutes=60"
-- 天眼 alarm: curl -X POST "http://127.0.0.1:8290/collectorapi/api/collect/trigger?id=101&dataType=alarm&minutes=60"
-- 微步 attack: curl -X POST "http://127.0.0.1:8290/collectorapi/api/collect/trigger?id=103&dataType=attack&minutes=60"
-- 微步 info: curl -X POST "http://127.0.0.1:8290/collectorapi/api/collect/trigger?id=103&dataType=info&minutes=60"
-- 全部类型: curl -X POST "http://127.0.0.1:8290/collectorapi/api/collect/trigger?id=101&minutes=60"
-- =====================================================================
-- =====================================================================
-- 3. 模拟异常场景(通过 Mock 接口场景参数,无需改库)
-- 天眼 csrf过期: GET "http://127.0.0.1:8290/collectorapi/mock/tianyan/skyeye/v1/analysis/log-search/list?index=weblog&offset=1&limit=100&start_time=xxx&end_time=xxx&mock_scenario=csrf_expired"
-- 天眼 空数据: ...&mock_scenario=empty
-- 微步 签名错误: POST "http://127.0.0.1:8290/collectorapi/mock/weibu/api/v1/log/searchBySql?api_key=xxx&auth_timestamp=xxx&sign=xxx&mock_scenario=sign_error"
-- =====================================================================
-- =====================================================================
-- 4. 恢复生产配置(测试完成后务必执行!)
-- =====================================================================
-- UPDATE device_api_config
-- SET host_api = 'https://10.56.170.107',
-- remark = '生产环境天眼主采集器',
-- update_time = SYSDATE
-- WHERE id = 101;
--
-- UPDATE device_api_config
-- SET host_api = 'https://10.56.170.107',
-- remark = '备用采集器,暂未启用',
-- update_time = SYSDATE
-- WHERE id = 102;
--
-- UPDATE device_api_config
-- SET host_api = 'https://10.56.171.164',
-- remark = '生产环境微步采集器',
-- update_time = SYSDATE
-- WHERE id = 103;
-- =====================================================================
-- 5. 校验查询
-- =====================================================================
-- SELECT id, device_type, device_name, host_api, data_types, enabled, remark
-- FROM device_api_config
-- ORDER BY id;
-- SELECT device_type, data_type, total_count, success_count, fail_count,
-- status, error_msg, create_time
-- FROM device_api_collect_log
-- ORDER BY id DESC
-- FETCH FIRST 10 ROWS ONLY;
@@ -1,17 +0,0 @@
package com;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@MapperScan("com.common.mapper")
@EnableScheduling
public class CollectorApiMainApp {
public static void main(String[] args) {
SpringApplication.run(CollectorApiMainApp.class, args);
System.out.println("============== CollectorApiMainApp 启动成功 ==============");
}
}
@@ -1,111 +0,0 @@
package com.collector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* 认证管理器
* 负责各采集器访问凭证(access_token / csrf_token / cookie)的获取与缓存。
* 凭证存储于 Redis,并带进程内缓存以减少 Redis 访问。
*/
@Component
public class AuthManager {
/** Redis key 前缀 */
private static final String TOKEN_PREFIX = "collector:auth:";
/** 默认凭证过期时间(秒),天眼 csrf_token 有效期约30分钟,按25分钟缓存 */
private static final long DEFAULT_TTL_SECONDS = 25 * 60;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/** 进程内缓存:key -> {token, expireAt} */
private final ConcurrentHashMap<String, CachedToken> localCache = new ConcurrentHashMap<>();
private static class CachedToken {
final String token;
final long expireAt;
CachedToken(String token, long ttlSeconds) {
this.token = token;
this.expireAt = System.currentTimeMillis() + ttlSeconds * 1000;
}
}
/**
* 保存凭证
* @param deviceType 设备类型
* @param key 凭证类型:csrf_token / access_token / cookie / sign
* @param value 凭证值
*/
public void saveToken(String deviceType, String key, String value) {
saveToken(deviceType, key, value, DEFAULT_TTL_SECONDS);
}
/**
* 保存凭证(自定义过期时间)
*/
public void saveToken(String deviceType, String key, String value, long ttlSeconds) {
String redisKey = TOKEN_PREFIX + deviceType + ":" + key;
try {
redisTemplate.opsForValue().set(redisKey, value, ttlSeconds, TimeUnit.SECONDS);
} catch (Exception e) {
// Redis 不可用时仅保留进程内缓存
}
localCache.put(redisKey, new CachedToken(value, ttlSeconds));
}
/**
* 获取凭证,优先本地缓存,其次 Redis
*/
public String getToken(String deviceType, String key) {
String redisKey = TOKEN_PREFIX + deviceType + ":" + key;
// 1. 本地缓存
CachedToken cached = localCache.get(redisKey);
if (cached != null && cached.expireAt > System.currentTimeMillis()) {
return cached.token;
}
if (cached != null) {
localCache.remove(redisKey);
}
// 2. Redis 缓存
try {
Object value = redisTemplate.opsForValue().get(redisKey);
if (value != null) {
String token = value.toString();
localCache.put(redisKey, new CachedToken(token, DEFAULT_TTL_SECONDS));
return token;
}
} catch (Exception e) {
// Redis 不可用
}
return null;
}
/**
* 删除凭证
*/
public void removeToken(String deviceType, String key) {
String redisKey = TOKEN_PREFIX + deviceType + ":" + key;
localCache.remove(redisKey);
try {
redisTemplate.delete(redisKey);
} catch (Exception e) {
// Redis 不可用
}
}
/**
* 判断凭证是否有效
*/
public boolean hasToken(String deviceType, String key) {
return getToken(deviceType, key) != null;
}
}
@@ -1,122 +0,0 @@
package com.collector;
import java.util.ArrayList;
import java.util.List;
/**
* 采集结果
*/
public class CollectResult {
/** 设备ID */
private Long deviceId;
/** 设备类型 */
private String deviceType;
/** 数据类型 */
private String dataType;
/** 采集开始时间戳(毫秒) */
private long startTime;
/** 采集结束时间戳(毫秒) */
private long endTime;
/** 采集总数 */
private int totalCount;
/** 成功条数 */
private int successCount;
/** 失败条数 */
private int failCount;
/** Kafka 消息列表(已格式化的完整消息) */
private List<String> messages = new ArrayList<>();
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public long getEndTime() {
return endTime;
}
public void setEndTime(long endTime) {
this.endTime = endTime;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getSuccessCount() {
return successCount;
}
public void setSuccessCount(int successCount) {
this.successCount = successCount;
}
public int getFailCount() {
return failCount;
}
public void setFailCount(int failCount) {
this.failCount = failCount;
}
public List<String> getMessages() {
return messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
public void addMessage(String message) {
this.messages.add(message);
this.totalCount++;
}
public void incrementSuccess() {
this.successCount++;
}
public void incrementFail() {
this.failCount++;
}
}
@@ -1,532 +0,0 @@
package com.collector;
import com.common.entity.DeviceApiCollectLog;
import com.common.entity.DeviceApiConfig;
import com.common.service.DeviceApiCollectLogService;
import com.common.service.DeviceApiConfigService;
import com.config.AppProperties;
import com.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* 采集调度器
* 定时遍历所有启用的设备配置,按数据类型执行采集,
* 支持断点续传(Redis记录最后采集时间戳)、失败重试、采集日志记录。
* 另提供手动全量回补:把 [startTime, endTime] 区间按固定子窗口由旧到新滚动逐段采集(同步执行)。
*/
@Component
public class CollectScheduler {
private static final Logger log = LoggerFactory.getLogger(CollectScheduler.class);
/** 断点续传 Redis Key 前缀 */
private static final String LAST_COLLECT_KEY_PREFIX = "device:api:last_collect_time:";
/** 断点续传 TTL:3天 */
private static final long LAST_COLLECT_TTL_DAYS = 3;
/** 时间窗口重叠回退:1分钟,防止边界丢数据 */
private static final long OVERLAP_BACKOFF_MS = 60000;
/** 采集窗口最大跨度:默认30分钟 */
private static final long MAX_WINDOW_MS = 30 * 60 * 1000;
/** 全量回补默认回看天数:1天 */
private static final long FULL_DEFAULT_BACK_DAYS = 1;
/** 全量回补默认子窗口跨度:30分钟(与定时增量最大窗口一致,避免单次查询跨度过大) */
private static final long FULL_DEFAULT_WINDOW_MS = 30 * 60 * 1000L;
@Autowired
private DeviceApiConfigService configService;
@Autowired
private DeviceApiCollectLogService collectLogService;
@Autowired
private AppProperties properties;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/** 采集器注册表:deviceType -> collector */
private final Map<String, DeviceDataCollector> collectorMap = new HashMap<>();
/** 采集执行线程池:各设备/数据类型并行采集,慢设备(如微步 info)不再阻塞其他设备 */
private ExecutorService collectExecutor;
/** 防重入集合:正在采集的任务 key(deviceType:deviceIp:dataType),上一轮未完成时跳过本轮提交 */
private final Set<String> runningKeys = ConcurrentHashMap.newKeySet();
@PostConstruct
public void initExecutor() {
int poolSize = Math.max(1, properties.getCollectPoolSize());
collectExecutor = Executors.newFixedThreadPool(poolSize, r -> {
Thread t = new Thread(r, "collect-worker");
t.setDaemon(true);
return t;
});
log.info("采集线程池初始化完成, poolSize={}", poolSize);
}
@PreDestroy
public void shutdownExecutor() {
if (collectExecutor != null) {
collectExecutor.shutdownNow();
}
}
@Autowired
public void registerCollectors(List<DeviceDataCollector> collectors) {
for (DeviceDataCollector collector : collectors) {
collectorMap.put(collector.getDeviceType(), collector);
}
log.info("注册采集器: {}", collectorMap.keySet());
}
/**
* 定时调度入口
*/
@Scheduled(cron = "${device.collect.cron:0 * * * * ?}")
public void scheduleCollect() {
try {
List<DeviceApiConfig> configs = configService.listEnabled();
if (configs.isEmpty()) {
log.debug("无启用的 API 采集配置");
return;
}
for (DeviceApiConfig config : configs) {
collectForDevice(config);
}
} catch (Exception e) {
log.error("采集调度异常: {}", e.getMessage(), e);
}
}
/**
* 对单个设备执行采集(遍历其所有数据类型)
*/
private void collectForDevice(DeviceApiConfig config) {
DeviceDataCollector collector = collectorMap.get(config.getDeviceType());
if (collector == null) {
log.warn("设备类型 {} 无对应采集器", config.getDeviceType());
return;
}
String dataTypes = config.getDataTypes();
if (dataTypes == null || dataTypes.isEmpty()) {
log.warn("设备 {} 未配置数据类型", config.getDeviceName());
return;
}
for (String dataType : dataTypes.split(",")) {
final String type = dataType.trim();
if (type.isEmpty()) {
continue;
}
// 防重入:上一轮该任务未结束时跳过本轮提交,避免同窗口并发重复采集
String runKey = config.getDeviceType() + ":"
+ (config.getDeviceIp() != null ? config.getDeviceIp() : "unknown") + ":" + type;
if (!runningKeys.add(runKey)) {
log.info("上一轮采集未完成,跳过本轮提交: device={}, type={}", config.getDeviceName(), type);
continue;
}
try {
collectExecutor.submit(() -> {
try {
collectWithRetry(collector, config, type);
} catch (Exception e) {
log.error("设备 {} 采集失败: type={}, error={}",
config.getDeviceName(), type, e.getMessage(), e);
} finally {
runningKeys.remove(runKey);
}
});
} catch (Exception e) {
runningKeys.remove(runKey);
log.error("提交采集任务失败: device={}, type={}, error={}",
config.getDeviceName(), type, e.getMessage());
}
}
}
/**
* 带重试的采集执行
*/
private void collectWithRetry(DeviceDataCollector collector, DeviceApiConfig config, String dataType) {
int maxAttempts = properties.getRetryMaxAttempts();
long delay = properties.getRetryInitialDelay();
double multiplier = properties.getRetryMultiplier();
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
LocalDateTime startLocal = LocalDateTime.now();
long startMs = System.currentTimeMillis();
// 本轮采集窗口,供最终失败时写采集日志(窗口未算出时才用尝试耗时兜底)
long[] window = null;
try {
// 计算采集时间窗口(断点续传)
window = calculateWindow(config, dataType);
long startTime = window[0];
long endTime = window[1];
// 执行采集
CollectResult result = collector.collect(config, dataType, startTime, endTime);
// 记录成功采集日志
DeviceApiCollectLog logEntity = new DeviceApiCollectLog();
logEntity.setDeviceId(config.getDeviceId());
logEntity.setDeviceType(config.getDeviceType());
logEntity.setDataType(dataType);
logEntity.setStartTime(TimeUtils.toLocalDateTime(startTime));
logEntity.setEndTime(TimeUtils.toLocalDateTime(endTime));
logEntity.setTotalCount(result.getTotalCount());
logEntity.setSuccessCount(result.getSuccessCount());
logEntity.setFailCount(result.getFailCount());
logEntity.setCostMs(System.currentTimeMillis() - startMs);
logEntity.setStatus("SUCCESS");
collectLogService.record(logEntity);
// 更新断点续传时间戳
updateLastCollectTime(config, dataType, endTime);
log.info("采集成功: device={}, type={}, window=[{}, {}], success={}, fail={}, cost={}ms",
config.getDeviceName(), dataType, startTime, endTime,
result.getSuccessCount(), result.getFailCount(), logEntity.getCostMs());
return;
} catch (Exception e) {
log.error("采集失败(第{}次/共{}次): device={}, type={}, error={}",
attempt, maxAttempts, config.getDeviceName(), dataType, e.getMessage());
if (attempt < maxAttempts) {
// 记录失败日志(中间重试不记录,仅在最终失败时记录)
try {
Thread.sleep(delay);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
delay = (long) (delay * multiplier);
} else {
// 最终失败,记录采集日志(窗口已算出时记录真实窗口,便于排查漏采)
DeviceApiCollectLog logEntity = new DeviceApiCollectLog();
logEntity.setDeviceId(config.getDeviceId());
logEntity.setDeviceType(config.getDeviceType());
logEntity.setDataType(dataType);
logEntity.setStartTime(window != null ? TimeUtils.toLocalDateTime(window[0]) : startLocal);
logEntity.setEndTime(window != null ? TimeUtils.toLocalDateTime(window[1]) : LocalDateTime.now());
logEntity.setTotalCount(0);
logEntity.setSuccessCount(0);
logEntity.setFailCount(0);
logEntity.setCostMs(System.currentTimeMillis() - startMs);
logEntity.setStatus("FAIL");
logEntity.setErrorMsg(e.getMessage() != null
? e.getMessage().substring(0, Math.min(e.getMessage().length(), 2000)) : "unknown");
collectLogService.record(logEntity);
}
}
}
}
/**
* 手动触发采集(功能测试用)
* 按配置ID立即执行一次采集,不走断点续传时间戳,
* 使用指定回看窗口(默认最近1小时,可指定 minutes)。
*
* @param configId 设备配置ID
* @param dataType 数据类型,为空则采集配置中的所有类型
* @param minutes 回看窗口分钟数,为空默认最近1小时
* @return 采集结果汇总
*/
public Map<String, Object> collectNow(Long configId, String dataType, Integer minutes) {
DeviceApiConfig config = configService.getById(configId);
if (config == null) {
throw new IllegalArgumentException("采集配置不存在: " + configId);
}
DeviceDataCollector collector = collectorMap.get(config.getDeviceType());
if (collector == null) {
throw new IllegalArgumentException("设备类型 " + config.getDeviceType() + " 无对应采集器");
}
long endTime = System.currentTimeMillis() - OVERLAP_BACKOFF_MS;
long backMs = minutes != null && minutes > 0 ? minutes * 60 * 1000L : 60 * 60 * 1000L;
long startTime = endTime - backMs;
String targetType = (dataType == null || dataType.trim().isEmpty())
? config.getDataTypes() : dataType.trim();
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("deviceId", config.getDeviceId());
resultMap.put("deviceName", config.getDeviceName());
resultMap.put("deviceType", config.getDeviceType());
resultMap.put("startTime", startTime);
resultMap.put("endTime", endTime);
List<String> types = java.util.Arrays.asList(targetType.split(","));
List<Map<String, Object>> results = new java.util.ArrayList<>();
int totalSuccess = 0;
int totalFail = 0;
for (String type : types) {
type = type.trim();
if (type.isEmpty()) {
continue;
}
long costStart = System.currentTimeMillis();
Map<String, Object> item = new HashMap<>();
item.put("dataType", type);
try {
CollectResult result = collector.collect(config, type, startTime, endTime);
item.put("status", "SUCCESS");
item.put("successCount", result.getSuccessCount());
item.put("failCount", result.getFailCount());
item.put("totalCount", result.getTotalCount());
totalSuccess += result.getSuccessCount();
totalFail += result.getFailCount();
} catch (Exception e) {
item.put("status", "FAIL");
item.put("error", e.getMessage());
totalFail++;
}
item.put("costMs", System.currentTimeMillis() - costStart);
results.add(item);
}
resultMap.put("results", results);
resultMap.put("totalSuccess", totalSuccess);
resultMap.put("totalFail", totalFail);
return resultMap;
}
/**
* 手动全量回补采集(同步,按时间滚动逐段拉取)
* 把 [startTime, endTime] 区间按 windowMinutes 子窗口由旧到新滚动推进逐段采集,
* 直到终点,用于历史数据回补(如天眼 weblog 全量采集)。
* 说明:1) 不读/不写 Redis 断点,与定时增量互不影响;
* 2) 消息按 Kafka key(eventId) 幂等,重复采集不会重复落库;
* 3) 默认某段失败即终止,可设置 continueOnError=true 跳过失败段继续。
*
* @param configId 设备采集配置ID
* @param dataType 数据类型,逗号分隔,如 weblog;为空则取配置中所有类型
* @param startTime 起始时间(毫秒时间戳),为空则取 endTime - backDays 天
* @param endTime 结束时间(毫秒时间戳),为空默认当前时间-1分钟
* @param backDays 回看天数,仅 startTime 为空时生效,默认30天
* @param windowMinutes 子窗口分钟数,默认30分钟
* @param continueOnError 某段失败是否继续后续段,默认 false(失败即终止)
* @return 采集结果汇总(含每段明细)
*/
public Map<String, Object> collectFull(Long configId, String dataType, Long startTime, Long endTime,
Integer backDays, Integer windowMinutes, Boolean continueOnError) {
DeviceApiConfig config = configService.getById(configId);
if (config == null) {
throw new IllegalArgumentException("采集配置不存在: " + configId);
}
DeviceDataCollector collector = collectorMap.get(config.getDeviceType());
if (collector == null) {
throw new IllegalArgumentException("设备类型 " + config.getDeviceType() + " 无对应采集器");
}
List<String> types = new ArrayList<>();
String rawTypes = (dataType == null || dataType.trim().isEmpty()) ? config.getDataTypes() : dataType;
if (rawTypes != null) {
for (String t : rawTypes.split(",")) {
t = t.trim();
if (!t.isEmpty()) {
types.add(t);
}
}
}
if (types.isEmpty()) {
throw new IllegalArgumentException("未指定数据类型");
}
long end = (endTime != null && endTime > 0) ? endTime : System.currentTimeMillis() - OVERLAP_BACKOFF_MS;
long start;
if (startTime != null && startTime > 0) {
start = startTime;
} else {
long days = (backDays != null && backDays > 0) ? backDays : FULL_DEFAULT_BACK_DAYS;
start = end - days * 24 * 60 * 60 * 1000L;
}
if (start >= end) {
throw new IllegalArgumentException("startTime 必须小于 endTime");
}
long windowMs = (windowMinutes != null && windowMinutes > 0)
? windowMinutes * 60 * 1000L : FULL_DEFAULT_WINDOW_MS;
boolean continueOnErr = Boolean.TRUE.equals(continueOnError);
long totalCostStart = System.currentTimeMillis();
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("deviceId", config.getDeviceId());
resultMap.put("deviceName", config.getDeviceName());
resultMap.put("deviceType", config.getDeviceType());
resultMap.put("dataTypes", types);
resultMap.put("startTime", start);
resultMap.put("endTime", end);
resultMap.put("windowMinutes", windowMs / 60_000L);
List<Map<String, Object>> segments = new ArrayList<>();
long totalSuccess = 0;
long totalFail = 0;
int successSegments = 0;
int failSegments = 0;
boolean interrupted = false;
for (String type : types) {
long cursor = start;
while (cursor < end) {
long segStart = cursor;
long segEnd = Math.min(segStart + windowMs, end);
long segCostStart = System.currentTimeMillis();
Map<String, Object> item = new HashMap<>();
item.put("dataType", type);
item.put("segStart", segStart);
item.put("segEnd", segEnd);
try {
CollectResult result = collector.collect(config, type, segStart, segEnd);
item.put("status", "SUCCESS");
item.put("successCount", result.getSuccessCount());
item.put("failCount", result.getFailCount());
item.put("totalCount", result.getTotalCount());
totalSuccess += result.getSuccessCount();
totalFail += result.getFailCount();
successSegments++;
} catch (Exception e) {
item.put("status", "FAIL");
item.put("error", e.getMessage());
failSegments++;
log.error("全量回补段失败: device={}, type={}, seg=[{} ~ {}], error={}",
config.getDeviceName(), type,
TimeUtils.format(segStart), TimeUtils.format(segEnd), e.getMessage());
if (!continueOnErr) {
item.put("costMs", System.currentTimeMillis() - segCostStart);
segments.add(item);
interrupted = true;
break;
}
}
item.put("costMs", System.currentTimeMillis() - segCostStart);
segments.add(item);
log.info("全量回补段完成: device={}, type={}, seg=[{} ~ {}], success={}, fail={}, cost={}ms",
config.getDeviceName(), type,
TimeUtils.format(segStart), TimeUtils.format(segEnd),
item.get("successCount") == null ? 0 : item.get("successCount"),
item.get("failCount") == null ? 0 : item.get("failCount"),
item.get("costMs"));
cursor = segEnd;
}
if (interrupted) {
break;
}
}
resultMap.put("segments", segments);
resultMap.put("successSegments", successSegments);
resultMap.put("failSegments", failSegments);
resultMap.put("totalSuccess", totalSuccess);
resultMap.put("totalFail", totalFail);
resultMap.put("interrupted", interrupted);
resultMap.put("costMs", System.currentTimeMillis() - totalCostStart);
log.info("全量回补完成: device={}, types={}, start={}, end={}, 成功段={}, 失败段={}, success={}, fail={}, cost={}ms",
config.getDeviceName(), types,
TimeUtils.format(start), TimeUtils.format(end),
successSegments, failSegments, totalSuccess, totalFail, resultMap.get("costMs"));
return resultMap;
}
/**
* 计算采集时间窗口(断点续传)
* @return [startTime, endTime]
*/
private long[] calculateWindow(DeviceApiConfig config, String dataType) {
long endTime = System.currentTimeMillis() - OVERLAP_BACKOFF_MS;
long lastTime = getLastCollectTime(config, dataType);
long startTime;
if (lastTime > 0) {
// 上次采集时间回退1分钟,防止边界丢数据
startTime = lastTime - OVERLAP_BACKOFF_MS;
} else {
// 首次采集:默认采集最近1小时的数据
startTime = endTime - 60 * 60 * 1000;
}
// 断点异常(晚于当前时间,如脏数据)时收敛为空窗口,避免窗口倒挂导致接口报错
if (startTime > endTime) {
startTime = endTime;
}
// 限制窗口最大跨度:默认30分钟;微步 info(日志流量)数据量大,单独限流为更小窗口,
// 避免单轮翻页过久阻塞线程池。截断时收窄 endTime 到实际采集终点,
// 断点只推进到真正采到的位置,剩余数据由下一轮继续采集,避免截断丢数据
long maxWindowMs = MAX_WINDOW_MS;
if ("weibu".equals(config.getDeviceType()) && "info".equals(dataType)) {
long infoMaxMs = properties.getWeibuInfoMaxWindowMinutes() * 60 * 1000L;
if (infoMaxMs > 0 && infoMaxMs < maxWindowMs) {
maxWindowMs = infoMaxMs;
}
}
if (endTime - startTime > maxWindowMs) {
endTime = startTime + maxWindowMs;
}
return new long[]{startTime, endTime};
}
/**
* 获取断点续传时间戳
*/
private long getLastCollectTime(DeviceApiConfig config, String dataType) {
String key = buildLastCollectKey(config, dataType);
try {
Object value = redisTemplate.opsForValue().get(key);
if (value != null) {
return Long.parseLong(value.toString());
}
} catch (Exception e) {
log.debug("获取断点续传时间失败: key={}, error={}", key, e.getMessage());
}
return 0;
}
/**
* 更新断点续传时间戳
*/
private void updateLastCollectTime(DeviceApiConfig config, String dataType, long timestamp) {
String key = buildLastCollectKey(config, dataType);
try {
redisTemplate.opsForValue().set(key, timestamp, LAST_COLLECT_TTL_DAYS, TimeUnit.DAYS);
} catch (Exception e) {
log.warn("更新断点续传时间失败: key={}, error={}", key, e.getMessage());
}
}
/**
* 构建断点续传 Redis Key
* device:api:last_collect_time:{deviceIp}:{deviceType}:{dataType}
*/
private String buildLastCollectKey(DeviceApiConfig config, String dataType) {
return LAST_COLLECT_KEY_PREFIX
+ (config.getDeviceIp() != null ? config.getDeviceIp() : "unknown") + ":"
+ config.getDeviceType() + ":"
+ dataType;
}
}
@@ -1,19 +0,0 @@
package com.collector;
/**
* 采集器自定义异常
*/
public class CollectorException extends RuntimeException {
public CollectorException(String message) {
super(message);
}
public CollectorException(String message, Throwable cause) {
super(message, cause);
}
public CollectorException(Throwable cause) {
super(cause);
}
}
@@ -1,38 +0,0 @@
package com.collector;
import com.common.entity.DeviceApiConfig;
/**
* 设备数据采集器统一接口
* 所有 API 类数据采集器(天眼/微步/XDR)必须实现此接口
*/
public interface DeviceDataCollector {
/**
* 获取设备类型
* @return tianyan / weibu / xdr
*/
String getDeviceType();
/**
* 认证并获取访问凭证(token / cookie 等)
* 凭证由 AuthManager 缓存,TTL 到期自动刷新
*/
void authenticate() throws CollectorException;
/**
* 当前采集器是否已认证成功
*/
boolean isAuthenticated();
/**
* 采集指定时间范围内的数据
* @param config 设备采集配置
* @param dataType 数据类型(天眼:weblog/alarm;微步:attack/info)
* @param startTime 起始时间戳(毫秒)
* @param endTime 结束时间戳(毫秒)
* @return 采集结果(包含 Kafka 消息列表)
*/
CollectResult collect(DeviceApiConfig config, String dataType, long startTime, long endTime)
throws CollectorException;
}
@@ -1,120 +0,0 @@
package com.collector;
import com.alibaba.fastjson2.JSONObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 字段映射器
* 将各厂商(天眼/微步)的原始字段映射为平台标准字段,
* 未映射字段原样保留,避免数据丢失。
*/
public class FieldMapper {
// ==================== 天眼字段映射 ====================
/**
* 天眼 weblog 访问日志字段映射:原始字段 -> 标准字段
*/
private static final Map<String, String> TIANYAN_WEBLOG_MAP = new HashMap<>();
static {
TIANYAN_WEBLOG_MAP.put("_time", "eventTime");
TIANYAN_WEBLOG_MAP.put("src_ip", "srcIp");
TIANYAN_WEBLOG_MAP.put("dst_ip", "dstIp");
TIANYAN_WEBLOG_MAP.put("src_port", "srcPort");
TIANYAN_WEBLOG_MAP.put("dst_port", "dstPort");
TIANYAN_WEBLOG_MAP.put("proto", "protocol");
TIANYAN_WEBLOG_MAP.put("method", "httpMethod");
TIANYAN_WEBLOG_MAP.put("uri", "url");
TIANYAN_WEBLOG_MAP.put("user_agent", "userAgent");
TIANYAN_WEBLOG_MAP.put("status", "httpStatus");
TIANYAN_WEBLOG_MAP.put("request_body", "requestBody");
TIANYAN_WEBLOG_MAP.put("response_body", "responseBody");
}
/**
* 天眼 alarm 告警日志字段映射:原始字段 -> 标准字段
*/
private static final Map<String, String> TIANYAN_ALARM_MAP = new HashMap<>();
static {
TIANYAN_ALARM_MAP.put("_time", "eventTime");
TIANYAN_ALARM_MAP.put("title", "eventName");
TIANYAN_ALARM_MAP.put("alarm_sev", "severity");
TIANYAN_ALARM_MAP.put("src_ip", "srcIp");
TIANYAN_ALARM_MAP.put("dst_ip", "dstIp");
TIANYAN_ALARM_MAP.put("src_port", "srcPort");
TIANYAN_ALARM_MAP.put("dst_port", "dstPort");
TIANYAN_ALARM_MAP.put("proto", "protocol");
TIANYAN_ALARM_MAP.put("rule_id", "ruleId");
TIANYAN_ALARM_MAP.put("event_id", "eventId");
TIANYAN_ALARM_MAP.put("attack_type", "attackType");
}
// ==================== 微步字段映射 ====================
/**
* 微步 attack/info 日志字段映射:原始字段 -> 标准字段
*/
private static final Map<String, String> WEIBU_MAP = new HashMap<>();
static {
WEIBU_MAP.put("event_time", "eventTime");
WEIBU_MAP.put("src_ip", "srcIp");
WEIBU_MAP.put("dst_ip", "dstIp");
WEIBU_MAP.put("src_port", "srcPort");
WEIBU_MAP.put("dst_port", "dstPort");
WEIBU_MAP.put("threat_type", "attackType");
WEIBU_MAP.put("risk_level", "severity");
WEIBU_MAP.put("domain", "domain");
WEIBU_MAP.put("url", "url");
WEIBU_MAP.put("md5", "fileHash");
WEIBU_MAP.put("sha256", "fileHash");
WEIBU_MAP.put("action", "action");
}
/**
* 根据设备类型和数据类型选择映射表
*/
private static Map<String, String> getMap(String deviceType, String dataType) {
if ("tianyan".equalsIgnoreCase(deviceType)) {
return "alarm".equalsIgnoreCase(dataType) ? TIANYAN_ALARM_MAP : TIANYAN_WEBLOG_MAP;
} else if ("weibu".equalsIgnoreCase(deviceType)) {
return WEIBU_MAP;
}
return null;
}
/**
* 映射单条原始数据
* @param deviceType 设备类型
* @param dataType 数据类型
* @param raw 原始数据
* @return 映射后的标准数据(保留原始字段)
*/
public static JSONObject map(String deviceType, String dataType, Map<String, Object> raw) {
JSONObject result = new JSONObject(new LinkedHashMap<>());
Map<String, String> fieldMap = getMap(deviceType, dataType);
for (Map.Entry<String, Object> entry : raw.entrySet()) {
String key = entry.getKey();
if (fieldMap != null && fieldMap.containsKey(key)) {
// 已映射字段:同时保留标准名和原始名
result.put(fieldMap.get(key), entry.getValue());
result.put(key, entry.getValue());
} else {
// 未映射字段:原样保留
result.put(key, entry.getValue());
}
}
return result;
}
/**
* 生成消息事件ID(用于 Kafka 消息 key)
*/
public static String generateEventId(String deviceType, String dataType, String rawId) {
String prefix = rawId == null || rawId.isEmpty() ? "" : rawId + "-";
return prefix + deviceType + "-" + dataType + "-" + System.nanoTime();
}
}
@@ -1,682 +0,0 @@
package com.collector;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.common.entity.DeviceApiConfig;
import com.config.AppProperties;
import com.kafka.KafkaProducerUtil;
import com.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 天眼(天空卫士)API 数据采集器
* 支持:weblog(访问日志)、alarm(告警日志)
*
* 认证方式:免密登录
* 1. client_id = SHA256(CLIENT_ID_SEED + "|" + login_key)
* 2. client_secret = SHA256(CLIENT_SECRET_SEED + "|" + login_key)
* 3. X-Authorization = SHA256(JSON({client_id, username}) + timestamp + client_secret)
* 4. POST /skyeye/v1/admin/auth 获取 access_token
* 5. GET /skyeye/v1/admin/auth 提取 csrf_token
*/
@Component
public class TianyanDataCollector implements DeviceDataCollector {
private static final Logger log = LoggerFactory.getLogger(TianyanDataCollector.class);
/** 免密登录种子(固定常量) */
private static final String CLIENT_ID_SEED = "mNSLP9UJCtBHtegjDPJnK3v";
private static final String CLIENT_SECRET_SEED = "3460681205014671737";
/** 默认用户名 */
private static final String DEFAULT_USERNAME = "xxadmin";
/** 认证相关路径 */
private static final String AUTH_PATH = "/skyeye/v1/admin/auth";
/** 日志查询接口(weblog 与 alarm 均走此接口,参数参考官方抓包示例) */
private static final String LOG_SEARCH_PATH = "/skyeye/v1/analysis/log-search/list";
/** 天眼 weblog 使用的 index */
private static final String INDEX_WEBLOG = "skyeye-weblog";
/** 天眼告警日志使用的 index(官方抓包示例:多索引逗号分隔) */
private static final String INDEX_ALARM = "alarm_collection,skyeye-sandbox,sandbox_mail,alarm_honeypot,"
+ "alarm_yunsuo,webids-webshell_dolog,webids-webattack_dolog,webids-ids_dolog,webids-ioc_dolog";
/** 从 HTML <meta name="csrf-token" content="..."> 提取 csrf_token(与官方 Python 正则一致) */
private static final java.util.regex.Pattern META_CSRF_PATTERN =
java.util.regex.Pattern.compile("<meta name=\"csrf-token\" content=\"([0-9a-fA-F]+)\"");
/** 深分页截断时对半拆分的最大深度:子窗口5分钟 → 2.5分钟 → 75秒 → 约37秒 */
private static final int MAX_SPLIT_DEPTH = 3;
@Autowired
private RestTemplate restTemplate;
@Autowired
private AppProperties properties;
@Autowired
private AuthManager authManager;
@Autowired
private KafkaProducerUtil kafkaProducer;
@Override
public String getDeviceType() {
return "tianyan";
}
@Override
public void authenticate() throws CollectorException {
DeviceApiConfig config = new DeviceApiConfig();
config.setHostApi(properties.getTianyanHost());
config.setAuthConfig("{\"login_key\":\"" + properties.getTianyanLoginKey()
+ "\",\"username\":\"" + properties.getTianyanUsername() + "\"}");
authenticate(config);
}
/**
* 天眼免密登录认证
* 流程:
* 1. 由 login_key 计算 client_id / client_secret
* 2. 计算 X-Authorization 签名头
* 3. POST /skyeye/v1/admin/auth 获取 access_token
* 4. GET /skyeye/v1/admin/auth 提取 csrf_token
*/
public void authenticate(DeviceApiConfig config) throws CollectorException {
// 优先使用配置中的 login_key / username,其次使用全局配置
String authConfig = config.getAuthConfig();
String loginKey = properties.getTianyanLoginKey();
String username = properties.getTianyanUsername();
if (username == null || username.isEmpty()) {
username = DEFAULT_USERNAME;
}
if (authConfig != null && !authConfig.isEmpty()) {
try {
JSONObject auth = JSON.parseObject(authConfig);
if (auth.getString("login_key") != null && !auth.getString("login_key").isEmpty()) {
loginKey = auth.getString("login_key");
}
if (auth.getString("username") != null && !auth.getString("username").isEmpty()) {
username = auth.getString("username");
}
} catch (Exception e) {
log.warn("解析天眼 authConfig 失败: {}", authConfig);
}
}
String host = config.getHostApi();
if (host == null || host.isEmpty()) {
host = properties.getTianyanHost();
}
try {
// 1. 计算 client_id / client_secret
String clientId = sha256(CLIENT_ID_SEED + "|" + loginKey);
String clientSecret = sha256(CLIENT_SECRET_SEED + "|" + loginKey);
// 2. 计算 X-Authorization 签名
// timestamp 为10位Unix时间戳,与平台时差不能超过10分钟
// 签名原文固定为 JSON 字符串(与官方 Python 示例一致)
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String jsonBody = "{\"client_id\":\"" + clientId + "\",\"username\":\"" + username + "\"}";
String xAuthorization = sha256(jsonBody + timestamp + clientSecret);
// 3. POST 获取 access_token
// 平台网关校验 X-Timestamp 请求头(与签名使用同一 timestamp)
// 参考官方 Python 示例:请求体按表单(client_id/username)提交
Map<String, String> headers = new HashMap<>();
headers.put("X-Timestamp", timestamp);
headers.put("X-Authorization", xAuthorization);
headers.put("Accept", "application/json");
Map<String, String> form = new HashMap<>();
form.put("client_id", clientId);
form.put("username", username);
String authUrl = host + AUTH_PATH;
String response = com.util.HttpUtil.postForm(restTemplate, authUrl, form, headers);
log.debug("天眼 POST 认证响应: {}", response);
JSONObject respObj = JSON.parseObject(response);
if (respObj == null) {
throw new CollectorException("天眼认证响应为空");
}
// 解析 access_token(兼容多种结构)
// 真实平台:JSON 顶层 access_token;Mock/部分环境:data.data.access_token / data.access_token
String accessToken = respObj.getString("access_token");
if (accessToken == null) {
JSONObject data = respObj.getJSONObject("data");
if (data != null) {
if (data.getString("access_token") != null) {
accessToken = data.getString("access_token");
}
JSONObject innerData = data.getJSONObject("data");
if (innerData != null && innerData.getString("access_token") != null) {
accessToken = innerData.getString("access_token");
}
}
}
if (accessToken == null) {
throw new CollectorException("天眼认证失败,未获取到 access_token: " + response);
}
authManager.saveToken(getDeviceType(), "access_token", accessToken);
// 4. GET 提取 csrf_token(响应 Set-Cookie 为后续数据接口的登录凭证,与官方示例 getCsrfAndCokkies 一致)
String csrfUrl = host + AUTH_PATH + "?token=" + accessToken;
Map<String, String> getHeaders = new HashMap<>();
getHeaders.put("X-Timestamp", timestamp);
getHeaders.put("X-Authorization", xAuthorization);
getHeaders.put("Accept", "application/json");
com.util.HttpUtil.GetResult authGet = com.util.HttpUtil.getWithCookies(restTemplate, csrfUrl, getHeaders);
String csrfResponse = authGet.getBody();
log.debug("天眼 GET 认证响应: {}", csrfResponse);
// 保存平台下发的真实 Cookie(name=value 拼接),数据接口请求必须携带;无 Set-Cookie 环境(如 Mock)则清除旧值
String cookie = buildCookieHeader(authGet.getSetCookies());
if (cookie != null && !cookie.isEmpty()) {
authManager.saveToken(getDeviceType(), "cookie", cookie);
} else {
authManager.removeToken(getDeviceType(), "cookie");
}
String csrfToken = extractCsrfToken(csrfResponse);
if (csrfToken != null) {
authManager.saveToken(getDeviceType(), "csrf_token", csrfToken);
}
log.info("天眼认证成功, host={}, username={}", host, username);
} catch (CollectorException e) {
throw e;
} catch (Exception e) {
throw new CollectorException("天眼认证异常: " + e.getMessage(), e);
}
}
/**
* 从 GET 认证响应中提取 csrf_token
* 真实平台返回 HTML(<meta name="csrf-token" content="...">),Mock 返回 JSON,兼容两种格式
*/
private String extractCsrfToken(String response) {
if (response == null || response.isEmpty()) {
return null;
}
// 1. 真实平台 HTML:<meta name="csrf-token" content="hex">(与官方 Python 正则一致)
java.util.regex.Matcher metaMatcher = META_CSRF_PATTERN.matcher(response);
if (metaMatcher.find()) {
return metaMatcher.group(1);
}
// 2. 尝试 JSON 格式
try {
JSONObject obj = JSON.parseObject(response);
if (obj != null) {
JSONObject data = obj.getJSONObject("data");
if (data != null) {
String token = data.getString("csrf_token");
if (token == null) {
token = data.getString("data");
}
if (token != null) {
return token;
}
}
}
} catch (Exception ignored) {
}
// 3. 兼容 csrf_token 键值形式
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("csrf_token[\"']?\\s*[:=]\\s*[\"']([^\"']+)[\"']");
java.util.regex.Matcher matcher = pattern.matcher(response);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
@Override
public boolean isAuthenticated() {
return authManager.hasToken(getDeviceType(), "access_token");
}
@Override
public CollectResult collect(DeviceApiConfig config, String dataType, long startTime, long endTime)
throws CollectorException {
// 未认证则先认证
if (!isAuthenticated()) {
authenticate(config);
}
String host = config.getHostApi();
if (host == null || host.isEmpty()) {
host = properties.getTianyanHost();
}
// 从配置解析 branch_id
String branchId = properties.getTianyanBranchId();
String authConfig = config.getAuthConfig();
if (authConfig != null && !authConfig.isEmpty()) {
try {
JSONObject auth = JSON.parseObject(authConfig);
if (auth.getString("branch_id") != null) {
branchId = auth.getString("branch_id");
}
} catch (Exception ignored) {
}
}
CollectResult result = new CollectResult();
result.setDeviceType(getDeviceType());
result.setDataType(dataType);
result.setStartTime(startTime);
result.setEndTime(endTime);
try {
// 窗口按子窗口切片逐段采集,控制单次查询数据量,预防平台深分页上限(返回空页被误判为已取完)
long subWindowMs = properties.getTianyanSubWindowMinutes() * 60_000L;
if (subWindowMs <= 0) {
subWindowMs = endTime - startTime;
}
long cursor = startTime;
while (cursor < endTime) {
long segEnd = Math.min(cursor + subWindowMs, endTime);
collectSegment(config, dataType, host, branchId, cursor, segEnd, result, 0);
cursor = segEnd;
}
log.info("天眼采集完成: dataType={}, start={}, end={}, total={}, success={}, fail={}",
dataType, startTime, endTime, result.getTotalCount(), result.getSuccessCount(), result.getFailCount());
return result;
} catch (CollectorException e) {
throw e;
} catch (Exception e) {
throw new CollectorException("天眼采集异常: " + e.getMessage(), e);
}
}
/**
* 采集单个时间子窗口 [segStart, segEnd],窗口数据量超过平台深分页上限时对半拆分重采
* 深分页截断判定:非首页返回空页(第 N-1 页为满页才会翻到第 N 页)说明窗口尾部数据未取完;
* 此时对半拆分窗口递归重采(拆分前已发送的数据靠 Kafka key eventId 幂等去重),
* 达到最大拆分深度仍截断则抛异常使本轮失败、断点不推进,避免尾部数据被静默丢弃
*/
private void collectSegment(DeviceApiConfig config, String dataType, String host, String branchId,
long segStart, long segEnd, CollectResult result, int depth)
throws CollectorException {
int pageSize = properties.getTianyanPageSize();
int page = 1;
boolean hasMore = true;
// 401 重认证重试次数(防账号异常时无限循环)
int unauthorizedRetries = 0;
// 窗口数据总数(首页响应提供时用于精确判定取完/截断,-1=未知)
long totalHint = -1;
// 本子窗口已采条数
long sentInSeg = 0;
while (hasMore) {
// 每页现取最新 csrf_token,认证重试后后续页/子窗口自动使用新 token
String csrfToken = authManager.getToken(getDeviceType(), "csrf_token");
if (csrfToken == null) {
authenticate(config);
csrfToken = authManager.getToken(getDeviceType(), "csrf_token");
}
// weblog 与 alarm 均走 log-search/list,差异仅在 index/category/mode(官方抓包示例一致)
String url = buildLogSearchUrl(host, branchId, dataType, segStart, segEnd, page, pageSize, csrfToken);
// 与官方示例一致:数据接口仅携带认证时下发并保存的 Cookie(登录态),URL 带 csrf_token,不额外带签名头
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
String sessionCookie = authManager.getToken(getDeviceType(), "cookie");
if (sessionCookie != null && !sessionCookie.isEmpty()) {
headers.put("Cookie", sessionCookie);
}
// 打印每页请求的时间参数(可读格式),便于核对子窗口切片与平台返回是否一致
log.info("天眼请求: dataType={}, 子窗口=[{} ~ {}], page={}",
dataType, TimeUtils.format(segStart), TimeUtils.format(segEnd), page);
String response;
try {
response = com.util.HttpUtil.get(restTemplate, url, headers);
} catch (HttpStatusCodeException httpEx) {
// 401 登录态失效(Cookie/csrf_token 过期):重新认证后重试当前页(限2次防死循环)
if (httpEx.getStatusCode() == HttpStatus.UNAUTHORIZED && unauthorizedRetries < 2) {
unauthorizedRetries++;
log.warn("天眼返回401登录态失效,重新认证后重试当前页: dataType={}, 子窗口=[{} ~ {}], page={}",
dataType, TimeUtils.format(segStart), TimeUtils.format(segEnd), page);
authenticate(config);
continue;
}
// 402 PAYMENT REQUIRED 属平台 license/授权问题,重认证无效,保持抛出由上层重试与记录
throw new CollectorException("天眼采集失败: " + httpEx.getStatusCode()
+ ": " + truncate(httpEx.getResponseBodyAsString(), 300), httpEx);
}
log.debug("天眼 {} 采集响应(seg=[{} ~ {}], page={}): {}",
dataType, TimeUtils.format(segStart), TimeUtils.format(segEnd), page, truncate(response, 500));
JSONObject respObj = JSON.parseObject(response);
if (respObj == null) {
throw new CollectorException("天眼采集响应为空");
}
// 校验返回状态
Integer status = respObj.getInteger("status");
if (status != null && status != 1000) {
String message = respObj.getString("message");
// 认证过期则重新认证重试
if ("csrf_token error".equalsIgnoreCase(message) || "token error".equalsIgnoreCase(message)
|| "auth failed".equalsIgnoreCase(message)) {
log.warn("天眼认证过期,重新认证后重试");
authenticate(config);
continue;
}
throw new CollectorException("天眼采集失败: " + message);
}
// 解析 hits
JSONArray hits = parseHits(respObj);
if (hits == null || hits.isEmpty()) {
if (page > 1) {
// 深分页截断:上一页为满页本页却为空,窗口尾部数据未取完
if (depth >= MAX_SPLIT_DEPTH) {
throw new CollectorException("天眼窗口数据量超过平台深分页上限且已达最小拆分粒度: seg=["
+ TimeUtils.format(segStart) + " ~ " + TimeUtils.format(segEnd) + "], page=" + page
+ ",本轮失败断点不推进,防止尾部数据静默丢失");
}
long mid = (segStart + segEnd) / 2;
log.warn("天眼深分页截断(seg=[{} ~ {}], page={}),对半拆分重采: depth={}",
TimeUtils.format(segStart), TimeUtils.format(segEnd), page, depth + 1);
collectSegment(config, dataType, host, branchId, segStart, mid, result, depth + 1);
collectSegment(config, dataType, host, branchId, mid, segEnd, result, depth + 1);
}
// 首页即为空:窗口内确实无数据
break;
}
for (int i = 0; i < hits.size(); i++) {
JSONObject hit = hits.getJSONObject(i);
JSONObject source = hit.getJSONObject("_source");
if (source == null) {
source = hit;
}
// 字段映射 + 构建 Kafka 消息
Map<String, Object> rawMap = new HashMap<>();
for (String key : source.keySet()) {
rawMap.put(key, source.get(key));
}
JSONObject mapped = FieldMapper.map(getDeviceType(), dataType, rawMap);
String eventId = rawMap.get("event_id") != null
? rawMap.get("event_id").toString()
: rawMap.get("_id") != null ? rawMap.get("_id").toString() : "";
long collectTime = System.currentTimeMillis();
// 前缀消息(设备信息) 与 日志内容体 合并为 Kafka value
String value = buildKafkaKey(config, eventId)
+ KafkaProducerUtil.buildValue(getDeviceType(), dataType, collectTime, mapped);
// Kafka key 使用 key-eventId,保证多源采集高并发下不重复(拆分重采的重复数据也靠它去重)
String key = "key-" + (eventId == null || eventId.isEmpty() ? System.nanoTime() : eventId);
if (kafkaProducer.send(key, value)) {
result.addMessage(value);
result.incrementSuccess();
} else {
result.incrementFail();
}
}
page++;
// 分页判断:如果本页不足 pageSize 则结束
if (hits.size() < pageSize) {
hasMore = false;
}
}
}
/**
* 构建日志查询 URL(weblog 与 alarm 共用 log-search/list,参数与官方抓包示例逐项一致)
* 差异:weblog → index=skyeye-weblog, category=skyeye, mode=fast_model
* alarm → index=告警多索引, category=event, mode=advance_model
* 参数:branch_id&start_time&end_time&keyword&index&category&offset&limit
* &asset_group_ids&mode&stime&etime&interval&page&size&key_fields&graph_conf&curBranch&sess_key_ids&csrf_token&r
*/
private String buildLogSearchUrl(String host, String branchId, String dataType, long startTime, long endTime,
int page, int pageSize, String csrfToken) {
boolean alarm = "alarm".equalsIgnoreCase(dataType);
String index = alarm ? INDEX_ALARM : INDEX_WEBLOG;
String category = alarm ? "event" : "skyeye";
String mode = alarm ? "advance_model" : "fast_model";
// offset 为页码语义:1=第1页,2=第2页(平台确认,非记录偏移量)
int offset = page;
StringBuilder sb = new StringBuilder(host).append(LOG_SEARCH_PATH).append("?");
sb.append("branch_id=").append(branchId) // 原样传递(配置中可能已含 % 编码)
.append("&start_time=").append(startTime)
.append("&end_time=").append(endTime)
.append("&keyword=")
.append("&index=").append(index)
.append("&category=").append(category)
.append("&offset=").append(offset)
.append("&limit=").append(pageSize)
.append("&asset_group_ids=")
.append("&mode=").append(mode)
.append("&stime=").append(startTime)
.append("&etime=").append(endTime)
.append("&interval=20s")
.append("&page=").append(page)
.append("&size=").append(pageSize)
.append("&key_fields=@timestamp")
.append("&graph_conf=%7B%7D")
.append("&curBranch=").append(branchId)
.append("&sess_key_ids=")
.append("&csrf_token=").append(csrfToken)
.append("&r=").append(System.nanoTime());
return sb.toString();
}
/**
* 从响应中解析记录数组,兼容 log-search 与 alarm/list 的多种响应结构:
* data.data.search.hits / data.data.hits|rows|list / data.hits|rows|list / data(数组) / 顶层 hits|rows|list
*/
private JSONArray parseHits(JSONObject respObj) {
JSONArray arr = tryArrayPath(respObj, "data", "data", "search", "hits");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "data", "data", "hits");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "data", "data", "rows");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "data", "data", "list");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "data", "hits");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "data", "rows");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "data", "list");
if (arr != null) {
return arr;
}
// data 本身为数组
Object data = respObj.get("data");
if (data instanceof JSONArray) {
return (JSONArray) data;
}
arr = tryArrayPath(respObj, "hits");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "rows");
if (arr != null) {
return arr;
}
arr = tryArrayPath(respObj, "list");
if (arr != null) {
return arr;
}
return null;
}
/**
* 沿字段路径逐层取值,若最终值为 JSONArray 则返回,否则返回 null
*/
private JSONArray tryArrayPath(JSONObject obj, String... path) {
Object cur = obj;
for (String key : path) {
if (!(cur instanceof JSONObject)) {
return null;
}
cur = ((JSONObject) cur).get(key);
if (cur == null) {
return null;
}
}
return cur instanceof JSONArray ? (JSONArray) cur : null;
}
/**
* 从响应中解析窗口数据总数(响应结构多样,做多路径兼容;无 total 返回 null)
*/
private Long parseTotal(JSONObject respObj) {
Long v = tryLong(respObj, "data", "data", "search", "total");
if (v == null) {
v = tryLong(respObj, "data", "data", "search", "hits", "total");
}
if (v == null) {
v = tryLong(respObj, "data", "data", "total");
}
if (v == null) {
v = tryLong(respObj, "data", "data", "hits", "total");
}
if (v == null) {
v = tryLong(respObj, "data", "total");
}
if (v == null) {
v = tryLong(respObj, "data", "hits", "total");
}
if (v == null) {
v = tryLong(respObj, "total");
}
return v;
}
/**
* 沿字段路径逐层取值,若最终值为数值则返回(兼容 ES hits.total 为 {value: N} 对象的形式)
*/
private Long tryLong(JSONObject obj, String... path) {
Object cur = obj;
for (String key : path) {
if (!(cur instanceof JSONObject)) {
return null;
}
cur = ((JSONObject) cur).get(key);
if (cur == null) {
return null;
}
}
if (cur instanceof Number) {
return ((Number) cur).longValue();
}
if (cur instanceof JSONObject) {
Object value = ((JSONObject) cur).get("value");
if (value instanceof Number) {
return ((Number) value).longValue();
}
}
return null;
}
/**
* 构建 Kafka 消息 key
* 格式参照 syslog-serve:[*receive_time=xx device_id=xx device_name=xx vendor=xx data_type=json device_collect_id=xx eventId=xx*]
*/
private String buildKafkaKey(DeviceApiConfig config, String eventId) {
StringBuilder sb = new StringBuilder();
sb.append("[").append("receive_time=").append(TimeUtils.formatCompactMs(System.currentTimeMillis()))
.append(" device_id=").append(config.getDeviceId() != null ? config.getDeviceId() : config.getId())
.append(" device_name=").append(config.getDeviceName() != null ? config.getDeviceName() : "天眼")
.append(" vendor=").append("天眼")
.append(", data_type=json")
.append(" device_collect_id=").append(properties.getDeviceCollectId() != null ? properties.getDeviceCollectId() : config.getId())
.append(" eventId=").append(eventId)
.append("]");
return sb.toString();
}
/**
* SHA256 哈希(返回小写十六进制)
*/
private String sha256(String input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
/**
* 将响应 Set-Cookie 解析为 Cookie 请求头值(name=value; name2=value2)
* 与官方示例 getCsrfAndCokkies() 拼接逻辑一致:仅保留 name=value,忽略 Path/Domain 等属性
*/
private String buildCookieHeader(List<String> setCookies) {
if (setCookies == null || setCookies.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
for (String sc : setCookies) {
if (sc == null || sc.trim().isEmpty()) {
continue;
}
String pair = sc.split(";", 2)[0].trim();
if (pair.isEmpty() || !pair.contains("=")) {
continue;
}
if (sb.length() > 0) {
sb.append("; ");
}
sb.append(pair);
}
return sb.toString();
}
/**
* 截断长字符串用于日志
*/
private String truncate(String str, int maxLen) {
if (str == null) {
return "";
}
return str.length() > maxLen ? str.substring(0, maxLen) + "..." : str;
}
}
@@ -1,459 +0,0 @@
package com.collector;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.common.entity.DeviceApiConfig;
import com.config.AppProperties;
import com.kafka.KafkaProducerUtil;
import com.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* 微步在线威胁感知平台(TDP)API 数据采集器
* 支持:attack(攻击流量,含 risk)、info(日志流量)
*
* 认证方式:HMAC-SHA256 签名
* SignData = ApiKey + Timestamp
* Sign = Base64.encodeBase64URLSafeString(HmacSHA256(SignData, Secret))
* POST /api/v1/log/searchBySql?api_key=<ApiKey>&auth_timestamp=<Timestamp>&sign=<Sign>
*
* 取数方式:size 两段式(与验证可运行的取数脚本一致)
* 1) size=1 探明窗口内数据总数 total(响应秒回)
* 2) size=total 一次取全窗口数据
* 不使用 page 翻页对象:平台 page 翻页存在深分页上限(约 4000 条后回绕返回重复页),
* size 一次取全可彻底绕开该问题;窗口数据量超过 size-cap 时对半拆分窗口递归采集
*/
@Component
public class WeibuDataCollector implements DeviceDataCollector {
private static final Logger log = LoggerFactory.getLogger(WeibuDataCollector.class);
/** 查询接口路径 */
private static final String SEARCH_BY_SQL_URI = "/api/v1/log/searchBySql";
/** 微步签名过期错误码:auth_timestamp 超过平台有效期,需刷新签名后重试 */
private static final int SIGN_EXPIRED_CODE = 1002;
/**
* 窗口数据量超过单次取数上限(size-cap)时对半拆分的最大深度:
* 5分钟 → 2.5分钟 → 75秒 → 37.5秒 → 18.75秒 → 9.4秒 → 4.7秒
* 洪峰期数据密度可达 2800条/秒(实测7秒窗口19610条),深度4不足以拆到上限以内
*/
private static final int MAX_SPLIT_DEPTH = 6;
/**
* 查询字段(与官方调用示例的 columns 一致:{label, value} 对象数组,attack/info 共用)
*/
private static final String[][] WEIBU_COLUMNS = {
{"告警时间", "time"},
{"威胁名称", "threat.name"},
{"攻击结果", "threat.result"},
{"严重级别", "threat.severity"},
{"攻击阶段", "threat.phase"},
{"威胁类型", "threat.type"},
{"告警主机", "machine"},
{"源IP", "net.src_ip"},
{"源端口", "net.src_port"},
{"目的IP", "net.dest_ip"},
{"目的端口", "net.dest_port"},
{"协议", "net.type"},
{"HTTP方法", "net.http.method"},
{"HTTP状态码", "net.http.status"},
{"请求头", "net.http.reqs_header"},
{"请求体", "net.http.reqs_body"},
{"响应头", "net.http.resp_header"},
{"响应体", "net.http.resp_body"},
{"URL/域名", "data"},
{"IOC", "threat.ioc"},
{"是否APT", "threat.is_apt"},
{"威胁标签", "threat.tag"},
{"处理状态", "threat.status"},
{"资产名称", "assets.name"},
{"业务组", "assets.group_name"}
};
@Autowired
private RestTemplate restTemplate;
@Autowired
private AppProperties properties;
@Autowired
private KafkaProducerUtil kafkaProducer;
@Override
public String getDeviceType() {
return "weibu";
}
@Override
public void authenticate() throws CollectorException {
// 微步无需持久化凭证,每次请求实时生成签名
if (properties.getWeibuApiKey() == null || properties.getWeibuApiKey().isEmpty()
|| properties.getWeibuSecret() == null || properties.getWeibuSecret().isEmpty()) {
throw new CollectorException("微步 api_key 或 secret 未配置");
}
}
@Override
public boolean isAuthenticated() {
return properties.getWeibuApiKey() != null && !properties.getWeibuApiKey().isEmpty()
&& properties.getWeibuSecret() != null && !properties.getWeibuSecret().isEmpty();
}
@Override
public CollectResult collect(DeviceApiConfig config, String dataType, long startTime, long endTime)
throws CollectorException {
// 微步签名需要秒级时间戳;签名在新鲜有效期内跨请求复用,1002 时刷新
long timestamp = System.currentTimeMillis() / 1000;
String apiKey = resolveApiKey(config);
String secret = resolveSecret(config);
String sign = generateSign(apiKey, secret, timestamp);
String host = config.getHostApi() != null && !config.getHostApi().isEmpty()
? config.getHostApi() : properties.getWeibuHost();
AuthState auth = new AuthState(timestamp, sign);
CollectResult result = new CollectResult();
result.setDeviceType(getDeviceType());
result.setDataType(dataType);
result.setStartTime(startTime);
result.setEndTime(endTime);
try {
collectSegment(config, dataType, host, apiKey, secret, auth, startTime, endTime, result, 0);
log.info("微步采集完成: dataType={}, start={}, end={}, total={}, success={}, fail={}",
dataType, startTime, endTime, result.getTotalCount(), result.getSuccessCount(), result.getFailCount());
return result;
} catch (CollectorException e) {
throw e;
} catch (Exception e) {
throw new CollectorException("微步采集异常: " + e.getMessage(), e);
}
}
/**
* 采集单个时间窗口 [segStart, segEnd],size 两段式取数:
* 1) size=1 探明窗口内数据总数 total
* 2) size=total 一次取全,并用 total 做完整性校验
* 窗口数据量超过单次取数上限(size-cap)时对半拆分窗口递归重采;
* 取回条数与 total 不符时先重试一次,仍不符则拆分重采(相邻子窗口边界重叠数据靠 Kafka key eventId 幂等去重);
* 达到最大拆分深度仍不完整则抛异常使断点不推进,防止数据静默丢失
*/
private void collectSegment(DeviceApiConfig config, String dataType, String host, String apiKey, String secret,
AuthState auth, long segStart, long segEnd, CollectResult result, int depth)
throws CollectorException {
int sizeCap = properties.getWeibuFetchSizeCap();
// 1. size=1 探明窗口内数据总数
JSONObject probeBody = buildQueryBody(dataType, segStart / 1000, segEnd / 1000, 1);
log.info("微步请求: dataType={}, 窗口=[{} ~ {}], size=1(探测total), depth={}",
dataType, TimeUtils.format(segStart), TimeUtils.format(segEnd), depth);
Long total = extractTotal(weibuPost(host, apiKey, secret, auth, probeBody));
if (total == null) {
throw new CollectorException("微步探测响应缺少 total 字段: dataType=" + dataType
+ ", 窗口=[" + TimeUtils.format(segStart) + " ~ " + TimeUtils.format(segEnd) + "]");
}
if (total <= 0) {
return; // 窗口内无数据
}
// 2. 数据量超过单次取数上限:对半拆分窗口递归重采
if (total > sizeCap) {
splitAndRecurse(config, dataType, host, apiKey, secret, auth, segStart, segEnd, result, depth,
"窗口数据量" + total + "超过单次取数上限" + sizeCap);
return;
}
// 3. size=total 一次取全
JSONObject fetchBody = buildQueryBody(dataType, segStart / 1000, segEnd / 1000, total.intValue());
log.info("微步请求: dataType={}, 窗口=[{} ~ {}], total={}, size={}",
dataType, TimeUtils.format(segStart), TimeUtils.format(segEnd), total, total.intValue());
JSONArray dataList = extractDataList(weibuPost(host, apiKey, secret, auth, fetchBody));
int fetched = dataList == null ? 0 : dataList.size();
// 4. 完整性校验:条数与 total 不符重试一次,仍不符则拆分重采
if (fetched != total) {
log.warn("微步 {} 取回条数与total不符({}/{}), 重试一次: 窗口=[{} ~ {}]",
dataType, fetched, total, TimeUtils.format(segStart), TimeUtils.format(segEnd));
dataList = extractDataList(weibuPost(host, apiKey, secret, auth, fetchBody));
fetched = dataList == null ? 0 : dataList.size();
if (fetched != total) {
splitAndRecurse(config, dataType, host, apiKey, secret, auth, segStart, segEnd, result, depth,
"取回条数" + fetched + "与total" + total + "不符");
return;
}
}
// 5. 发送 Kafka
if (dataList != null && !dataList.isEmpty()) {
sendItems(config, dataType, dataList, result);
}
}
/**
* 对半拆分窗口递归重采;达到最大拆分深度仍不完整则抛异常,断点不推进防数据丢失
*/
private void splitAndRecurse(DeviceApiConfig config, String dataType, String host, String apiKey, String secret,
AuthState auth, long segStart, long segEnd, CollectResult result, int depth,
String reason) throws CollectorException {
if (depth >= MAX_SPLIT_DEPTH) {
throw new CollectorException("微步窗口已达最小拆分粒度仍无法一次取全(" + reason + "): 窗口=["
+ TimeUtils.format(segStart) + " ~ " + TimeUtils.format(segEnd) + "]"
+ ",本轮失败断点不推进,防止数据静默丢失;可调大 device.weibu.fetch-size-cap 或调小 info-max-window-minutes");
}
long mid = (segStart + segEnd) / 2;
log.warn("微步 {} {},对半拆分重采: [{} ~ {}] → [{} ~ {}]+[{} ~ {}], depth={}",
dataType, reason,
TimeUtils.format(segStart), TimeUtils.format(segEnd),
TimeUtils.format(segStart), TimeUtils.format(mid),
TimeUtils.format(mid), TimeUtils.format(segEnd), depth + 1);
collectSegment(config, dataType, host, apiKey, secret, auth, segStart, mid, result, depth + 1);
collectSegment(config, dataType, host, apiKey, secret, auth, mid, segEnd, result, depth + 1);
}
/**
* 把单窗口数据映射并发送到 Kafka
*/
private void sendItems(DeviceApiConfig config, String dataType, JSONArray dataList, CollectResult result) {
for (int i = 0; i < dataList.size(); i++) {
JSONObject item = dataList.getJSONObject(i);
Map<String, Object> rawMap = new HashMap<>();
for (String key : item.keySet()) {
rawMap.put(key, item.get(key));
}
JSONObject mapped = FieldMapper.map(getDeviceType(), dataType, rawMap);
String eventId = rawMap.get("id") != null ? rawMap.get("id").toString() : "";
long collectTime = System.currentTimeMillis();
// 前缀消息(设备信息) 与 日志内容体 合并为 Kafka value
String value = buildKafkaKey(config, eventId)
+ KafkaProducerUtil.buildValue(getDeviceType(), dataType, collectTime, mapped);
// Kafka key 使用 key-eventId,保证多源采集高并发下不重复(相邻子窗口边界重叠数据也靠它去重)
String key = "key-" + (eventId == null || eventId.isEmpty() ? System.nanoTime() : eventId);
if (kafkaProducer.send(key, value)) {
result.addMessage(value);
result.incrementSuccess();
} else {
result.incrementFail();
}
}
}
/**
* 微步查询共用:签名 → POST → response_code 校验,返回完整响应
* 收到 1002(sign expired) 时刷新签名重试一次,保证当前请求不失败;
* 签名在新鲜有效期内跨请求复用,仅失效时才重新生成,避免每请求重复签名计算
*/
private JSONObject weibuPost(String host, String apiKey, String secret, AuthState auth, JSONObject body)
throws CollectorException {
for (int attempt = 1; attempt <= 2; attempt++) {
String url = host + SEARCH_BY_SQL_URI
+ "?api_key=" + apiKey
+ "&auth_timestamp=" + auth.timestamp
+ "&sign=" + auth.sign;
String response = com.util.HttpUtil.postJson(restTemplate, url, body.toJSONString(), null);
log.debug("微步采集响应(attempt={}): {}", attempt, truncate(response, 500));
JSONObject parsed = JSON.parseObject(response);
if (parsed == null) {
throw new CollectorException("微步采集响应为空");
}
Integer responseCode = parsed.getInteger("response_code");
if (responseCode == null || responseCode == 0) {
return parsed;
}
if (responseCode == SIGN_EXPIRED_CODE && attempt == 1) {
// 签名过期:刷新时间戳并重新签名,重试当前请求
log.warn("微步签名过期(response_code=1002),刷新签名重试当前请求");
auth.timestamp = System.currentTimeMillis() / 1000;
auth.sign = generateSign(apiKey, secret, auth.timestamp);
continue;
}
// 微步实际返回 verbose_msg 字段(response_msg 恒为 null),做兼容读取
String verboseMsg = parsed.getString("verbose_msg") != null
? parsed.getString("verbose_msg") : parsed.getString("response_msg");
throw new CollectorException("微步采集失败: response_code=" + responseCode
+ ", verbose_msg=" + verboseMsg
+ ", 完整响应: " + truncate(response, 1000));
}
throw new CollectorException("微步采集请求未获得有效响应");
}
/**
* 从响应 data 对象提取 total(平台在 data.total 返回窗口内数据总数),无 total 返回 null
*/
private Long extractTotal(JSONObject respObj) {
JSONObject data = respObj.getJSONObject("data");
if (data == null) {
return null;
}
return data.getLong("total");
}
/**
* 解析数据列表:data.data(兼容 data.data.list / 顶层 data 数组)
*/
private JSONArray extractDataList(JSONObject respObj) {
JSONArray dataList = null;
JSONObject data = respObj.getJSONObject("data");
if (data != null) {
dataList = data.getJSONArray("data");
if (dataList == null && data.getJSONObject("data") != null) {
dataList = data.getJSONObject("data").getJSONArray("list");
}
}
if (dataList == null) {
dataList = respObj.getJSONArray("data");
}
return dataList;
}
/**
* 构造查询 Body(size 两段式取数,与验证可运行的取数脚本一致;不再使用 page 翻页对象)
*/
private JSONObject buildQueryBody(String dataType, long timeFrom, long timeTo, int size) {
JSONObject body = new JSONObject();
// net_data_type: attack 口径含 risk(与验证脚本一致,risk 类攻击不漏采)/ info-日志流量
JSONArray netDataType = new JSONArray();
if ("attack".equalsIgnoreCase(dataType)) {
netDataType.add("attack");
netDataType.add("risk");
} else {
netDataType.add(dataType);
}
body.put("net_data_type", netDataType);
body.put("time_from", timeFrom);
body.put("time_to", timeTo);
// 资产组(空表示全部)
body.put("assets_group", new JSONArray());
body.put("search_connection", false);
// sql 置空,返回字段由 columns 指定(官方示例:columns 为 {label,value} 对象数组,不可用字符串 "*")
body.put("sql", "");
JSONArray columns = new JSONArray();
for (String[] col : WEIBU_COLUMNS) {
JSONObject c = new JSONObject();
c.put("label", col[0]);
c.put("value", col[1]);
columns.add(c);
}
body.put("columns", columns);
// 官方示例为倒序
body.put("asc", false);
// 顶层 size:单次取数条数(接口默认最多返回1000条,需显式设置;size=1 用于探测 total)
body.put("size", size);
return body;
}
/**
* 生成微步 API 签名:
* Sign = Base64.encodeBase64URLSafeString(HmacSHA256(ApiKey+Timestamp, Secret))
*/
private String generateSign(String apiKey, String secret, long timestamp) {
try {
String signData = apiKey + timestamp;
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
mac.init(keySpec);
byte[] hmacBytes = mac.doFinal(signData.getBytes(StandardCharsets.UTF_8));
// URL Safe Base64:将"+"替换为"-"、"/"替换为"_"并去除末尾"="
String base64 = Base64.getEncoder().encodeToString(hmacBytes);
return base64.replace('+', '-').replace('/', '_').replaceAll("=+$", "");
} catch (Exception e) {
throw new CollectorException("微步签名计算失败: " + e.getMessage(), e);
}
}
/**
* 解析 api_key:优先配置中的 authConfig,其次全局配置
*/
private String resolveApiKey(DeviceApiConfig config) {
String authConfig = config.getAuthConfig();
if (authConfig != null && !authConfig.isEmpty()) {
try {
JSONObject auth = JSON.parseObject(authConfig);
if (auth.getString("api_key") != null) {
return auth.getString("api_key");
}
} catch (Exception ignored) {
}
}
return properties.getWeibuApiKey();
}
/**
* 解析 secret
*/
private String resolveSecret(DeviceApiConfig config) {
String authConfig = config.getAuthConfig();
if (authConfig != null && !authConfig.isEmpty()) {
try {
JSONObject auth = JSON.parseObject(authConfig);
if (auth.getString("secret") != null) {
return auth.getString("secret");
}
} catch (Exception ignored) {
}
}
return properties.getWeibuSecret();
}
/**
* 构建 Kafka 消息 key
*/
private String buildKafkaKey(DeviceApiConfig config, String eventId) {
StringBuilder sb = new StringBuilder();
sb.append("[").append("receive_time=").append(TimeUtils.formatCompactMs(System.currentTimeMillis()))
.append(" device_id=").append(config.getDeviceId() != null ? config.getDeviceId() : config.getId())
.append(" device_name=").append(config.getDeviceName() != null ? config.getDeviceName() : "微步")
.append(" vendor=").append("微步")
.append(", data_type=json")
.append(" device_collect_id=").append(properties.getDeviceCollectId() != null ? properties.getDeviceCollectId() : config.getId())
.append(" eventId=").append(eventId)
.append("]");
return sb.toString();
}
/**
* 截断长字符串用于日志
*/
private String truncate(String str, int maxLen) {
if (str == null) {
return "";
}
return str.length() > maxLen ? str.substring(0, maxLen) + "..." : str;
}
/**
* 签名状态(timestamp + sign),1002 时刷新后跨请求复用
*/
private static class AuthState {
private long timestamp;
private String sign;
AuthState(long timestamp, String sign) {
this.timestamp = timestamp;
this.sign = sign;
}
}
}
@@ -1,100 +0,0 @@
package com.collector;
import com.common.entity.DeviceApiConfig;
import com.config.AppProperties;
import com.kafka.KafkaProducerUtil;
import com.util.Sm4Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
/**
* 深信服 XDR API 数据采集器(暂缓)
*
* 认证方式:AK/SK HMAC-SHA256 签名
* 1. 联动码解码:Hex解码 -> 按"|"分割为14段 -> 通过SHA256计算AES密钥 -> AES-CBC解密第10段得到AK,第11段得到SK
* 2. 添加公共头:sdk-content-type / sdk-host / sign-date
* 3. 构造规范串、计算签名、构造 Authorization
*/
@Component
public class XdrDataCollector implements DeviceDataCollector {
private static final Logger log = LoggerFactory.getLogger(XdrDataCollector.class);
@Autowired
private RestTemplate restTemplate;
@Autowired
private AppProperties properties;
@Autowired
private KafkaProducerUtil kafkaProducer;
/** AK / SK(联动码解码获得) */
private String ak;
private String sk;
@Override
public String getDeviceType() {
return "xdr";
}
/**
* 联动码解码获取 AK/SK
* Hex解码 -> 按"|"分割为14段 -> SHA256计算AES密钥 -> AES-CBC解密第10段(AK)、第11段(SK)
*/
public void decodeAuthCode(String authCode) throws CollectorException {
try {
byte[] hexBytes = cn.hutool.core.util.HexUtil.decodeHex(authCode);
String decoded = new String(hexBytes, StandardCharsets.UTF_8);
String[] segments = decoded.split("\\|");
if (segments.length < 14) {
throw new CollectorException("XDR 联动码格式错误,段数不足14");
}
// 计算 AES 密钥:SHA256(前几段拼接)
StringBuilder seed = new StringBuilder();
for (int i = 0; i < 9; i++) {
seed.append(segments[i]).append("|");
}
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] aesKey = digest.digest(seed.toString().getBytes(StandardCharsets.UTF_8));
// 第10段、第11段解密(AES-CBC,iv 固定为前16字节或配置)
// 说明:XDR 联动码的 AES 密钥与 IV 算法细节需在联调阶段确认,此处为骨架实现
this.ak = segments[9];
this.sk = segments[10];
log.info("XDR 联动码解码成功, ak={}", ak);
} catch (CollectorException e) {
throw e;
} catch (Exception e) {
throw new CollectorException("XDR 联动码解码失败: " + e.getMessage(), e);
}
}
@Override
public void authenticate() throws CollectorException {
String authCode = properties.getXdrAuthCode();
if (authCode == null || authCode.isEmpty()) {
throw new CollectorException("XDR auth-code 未配置,XDR 采集暂缓");
}
decodeAuthCode(authCode);
}
@Override
public boolean isAuthenticated() {
return ak != null && sk != null;
}
@Override
public CollectResult collect(DeviceApiConfig config, String dataType, long startTime, long endTime)
throws CollectorException {
// 暂缓实现:XDR 接口联调完成后实现
throw new CollectorException("XDR 采集器暂缓,待联动码解码与签名接口联调后实现");
}
}
@@ -1,170 +0,0 @@
package com.common.entity;
import java.time.LocalDateTime;
/**
* API 采集执行日志表
* 对应数据表 device_api_collect_log
*/
public class DeviceApiCollectLog {
/** 主键 */
private Long id;
/** 设备ID,关联 device_device.id,非空 */
private Integer deviceId;
/** 设备类型:tianyan / weibu / xdr */
private String deviceType;
/** 采集数据类型:weblog / alarm / attack / info */
private String dataType;
/** 采集开始时间 */
private LocalDateTime startTime;
/** 采集结束时间 */
private LocalDateTime endTime;
/** 采集总数 */
private Integer totalCount;
/** 成功条数 */
private Integer successCount;
/** 失败条数 */
private Integer failCount;
/** 耗时(毫秒) */
private Long costMs;
/** 采集状态:SUCCESS-成功 FAIL-失败 */
private String status;
/** 错误信息 */
private String errorMsg;
/** 创建时间 */
private LocalDateTime createTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getDeviceId() {
return deviceId;
}
public void setDeviceId(Integer deviceId) {
this.deviceId = deviceId;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
public LocalDateTime getEndTime() {
return endTime;
}
public void setEndTime(LocalDateTime endTime) {
this.endTime = endTime;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getSuccessCount() {
return successCount;
}
public void setSuccessCount(Integer successCount) {
this.successCount = successCount;
}
public Integer getFailCount() {
return failCount;
}
public void setFailCount(Integer failCount) {
this.failCount = failCount;
}
public Long getCostMs() {
return costMs;
}
public void setCostMs(Long costMs) {
this.costMs = costMs;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "DeviceApiCollectLog{" +
"id=" + id +
", deviceId=" + deviceId +
", deviceType='" + deviceType + '\'' +
", dataType='" + dataType + '\'' +
", startTime=" + startTime +
", endTime=" + endTime +
", totalCount=" + totalCount +
", successCount=" + successCount +
", failCount=" + failCount +
", costMs=" + costMs +
", status=" + status +
'}';
}
}
@@ -1,167 +0,0 @@
package com.common.entity;
import java.time.LocalDateTime;
/**
* API 采集设备配置表
* 对应数据表 device_api_config
*/
public class DeviceApiConfig {
/** 主键 */
private Long id;
/** 设备ID,关联 device_device.id */
private Integer deviceId;
/** 设备类型:tianyan-天眼 / weibu-微步 / xdr-XDR */
private String deviceType;
/** 设备名称 */
private String deviceName;
/** 设备IP */
private String deviceIp;
/** API 基础地址 */
private String hostApi;
/** 认证配置(JSON):login_key / api_key / secret / username / branch_id 等 */
private String authConfig;
/** 采集数据类型,逗号分隔:weblog,alarm / attack,info */
private String dataTypes;
/** 采集间隔(秒) */
private Integer collectIntervalSec;
/** 是否启用:1-启用 0-停用 */
private Integer enabled;
/** 备注 */
private String remark;
/** 创建时间 */
private LocalDateTime createTime;
/** 更新时间 */
private LocalDateTime updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getDeviceId() {
return deviceId;
}
public void setDeviceId(Integer deviceId) {
this.deviceId = deviceId;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getDeviceIp() {
return deviceIp;
}
public void setDeviceIp(String deviceIp) {
this.deviceIp = deviceIp;
}
public String getHostApi() {
return hostApi;
}
public void setHostApi(String hostApi) {
this.hostApi = hostApi;
}
public String getAuthConfig() {
return authConfig;
}
public void setAuthConfig(String authConfig) {
this.authConfig = authConfig;
}
public String getDataTypes() {
return dataTypes;
}
public void setDataTypes(String dataTypes) {
this.dataTypes = dataTypes;
}
public Integer getCollectIntervalSec() {
return collectIntervalSec;
}
public void setCollectIntervalSec(Integer collectIntervalSec) {
this.collectIntervalSec = collectIntervalSec;
}
public Integer getEnabled() {
return enabled;
}
public void setEnabled(Integer enabled) {
this.enabled = enabled;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "DeviceApiConfig{" +
"id=" + id +
", deviceId=" + deviceId +
", deviceType='" + deviceType + '\'' +
", deviceName='" + deviceName + '\'' +
", deviceIp='" + deviceIp + '\'' +
", hostApi='" + hostApi + '\'' +
", dataTypes='" + dataTypes + '\'' +
", enabled=" + enabled +
'}';
}
}
@@ -1,43 +0,0 @@
package com.common.mapper;
import com.common.entity.DeviceApiCollectLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* API 采集执行日志 Mapper
*/
@Mapper
public interface DeviceApiCollectLogMapper {
/**
* 插入采集日志
*/
int insert(DeviceApiCollectLog log);
/**
* 根据ID查询
*/
DeviceApiCollectLog selectById(@Param("id") Long id);
/**
* 根据设备类型查询
*/
List<DeviceApiCollectLog> selectByDeviceType(@Param("deviceType") String deviceType);
/**
* 按时间范围查询
*/
List<DeviceApiCollectLog> selectByTimeRange(
@Param("startTime") java.time.LocalDateTime startTime,
@Param("endTime") java.time.LocalDateTime endTime);
/**
* 删除旧数据
*/
int deleteByTimeRange(
@Param("startTime") java.time.LocalDateTime startTime,
@Param("endTime") java.time.LocalDateTime endTime);
}
@@ -1,54 +0,0 @@
package com.common.mapper;
import com.common.entity.DeviceApiConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* API 采集设备配置 Mapper
*/
@Mapper
public interface DeviceApiConfigMapper {
/**
* 查询所有启用的采集配置
*/
List<DeviceApiConfig> selectEnabled();
/**
* 根据ID查询
*/
DeviceApiConfig selectById(@Param("id") Long id);
/**
* 根据设备类型查询
*/
List<DeviceApiConfig> selectByDeviceType(@Param("deviceType") String deviceType);
/**
* 查询所有配置
*/
List<DeviceApiConfig> selectAll();
/**
* 新增配置
*/
int insert(DeviceApiConfig config);
/**
* 更新配置
*/
int update(DeviceApiConfig config);
/**
* 更新启用状态
*/
int updateEnabled(@Param("id") Long id, @Param("enabled") Integer enabled);
/**
* 删除配置
*/
int deleteById(@Param("id") Long id);
}
@@ -1,43 +0,0 @@
package com.common.service;
import com.common.entity.DeviceApiCollectLog;
import com.common.mapper.DeviceApiCollectLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
/**
* API 采集执行日志 Service
*/
@Service
public class DeviceApiCollectLogService {
@Autowired
private DeviceApiCollectLogMapper logMapper;
/**
* 记录采集日志
*/
public void record(DeviceApiCollectLog log) {
if (log.getCreateTime() == null) {
log.setCreateTime(LocalDateTime.now());
}
logMapper.insert(log);
}
/**
* 按设备类型查询
*/
public List<DeviceApiCollectLog> listByDeviceType(String deviceType) {
return logMapper.selectByDeviceType(deviceType);
}
/**
* 清理指定时间之前的旧日志
*/
public int cleanBefore(LocalDateTime startTime) {
return logMapper.deleteByTimeRange(startTime, LocalDateTime.now());
}
}
@@ -1,78 +0,0 @@
package com.common.service;
import com.common.entity.DeviceApiConfig;
import com.common.mapper.DeviceApiConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
/**
* API 采集设备配置 Service
*/
@Service
public class DeviceApiConfigService {
@Autowired
private DeviceApiConfigMapper configMapper;
/**
* 查询所有启用的采集配置
*/
public List<DeviceApiConfig> listEnabled() {
List<DeviceApiConfig> list = configMapper.selectEnabled();
return list == null ? Collections.emptyList() : list;
}
/**
* 根据ID查询
*/
public DeviceApiConfig getById(Long id) {
return configMapper.selectById(id);
}
/**
* 根据设备类型查询
*/
public List<DeviceApiConfig> listByDeviceType(String deviceType) {
List<DeviceApiConfig> list = configMapper.selectByDeviceType(deviceType);
return list == null ? Collections.emptyList() : list;
}
/**
* 查询所有配置
*/
public List<DeviceApiConfig> listAll() {
List<DeviceApiConfig> list = configMapper.selectAll();
return list == null ? Collections.emptyList() : list;
}
/**
* 新增配置
*/
public int add(DeviceApiConfig config) {
return configMapper.insert(config);
}
/**
* 更新配置
*/
public int update(DeviceApiConfig config) {
return configMapper.update(config);
}
/**
* 更新启用状态
*/
public int updateEnabled(Long id, Integer enabled) {
return configMapper.updateEnabled(id, enabled);
}
/**
* 删除配置
*/
public int delete(Long id) {
return configMapper.deleteById(id);
}
}
@@ -1,103 +0,0 @@
package com.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* API 采集器应用配置属性
*/
@Data
@Component
public class AppProperties {
// ========== 采集调度配置 ==========
/** 定时采集间隔(毫秒),默认5分钟 */
@Value("${device.collect.interval:300000}")
private long collectInterval;
/** 最大重试次数 */
@Value("${device.collect.retry.max-attempts:3}")
private int retryMaxAttempts;
/** 重试初始延迟(毫秒) */
@Value("${device.collect.retry.initial-delay:5000}")
private long retryInitialDelay;
/** 重试延迟倍数 */
@Value("${device.collect.retry.multiplier:2.0}")
private double retryMultiplier;
/** Kafka 推送 topic */
@Value("${device.collect.kafka.topic:device-api-collect}")
private String kafkaTopic;
/** 采集探针ID */
@Value("${app.service.device_collect_id:1}")
private Long deviceCollectId;
// ========== 天眼配置 ==========
@Value("${device.tianyan.host:https://127.0.0.1:443}")
private String tianyanHost;
@Value("${device.tianyan.login-key:}")
private String tianyanLoginKey;
@Value("${device.tianyan.username:xxadmin}")
private String tianyanUsername;
@Value("${device.tianyan.branch-id:}")
private String tianyanBranchId;
@Value("${device.tianyan.data-types:weblog,alarm}")
private String tianyanDataTypes;
@Value("${device.tianyan.page-size:100}")
private int tianyanPageSize;
/** 天眼单次查询子窗口跨度(分钟):采集窗口切片预防平台深分页上限,<=0 表示不切片 */
@Value("${device.tianyan.sub-window-minutes:5}")
private int tianyanSubWindowMinutes;
/** 采集执行线程池大小:各设备/数据类型并行采集 */
@Value("${device.collect.pool-size:15}")
private int collectPoolSize;
/** 微步 info(日志流量)单轮采集窗口上限(分钟):数据量大,限流防止单轮过久,积压时逐轮追赶 */
@Value("${device.weibu.info-max-window-minutes:5}")
private int weibuInfoMaxWindowMinutes;
// ========== 微步配置 ==========
@Value("${device.weibu.host:https://127.0.0.1:443}")
private String weibuHost;
@Value("${device.weibu.api-key:}")
private String weibuApiKey;
@Value("${device.weibu.secret:}")
private String weibuSecret;
@Value("${device.weibu.data-types:attack,info}")
private String weibuDataTypes;
/** 微步限流:每秒最大请求数 */
@Value("${device.weibu.rate-limit:10}")
private int weibuRateLimit;
/** 微步单次取数上限(size 两段式取数一次取全的条数上限,超出对半拆分窗口重采) */
@Value("${device.weibu.fetch-size-cap:10000}")
private int weibuFetchSizeCap;
// ========== XDR 配置(暂缓) ==========
@Value("${device.xdr.host:https://127.0.0.1}")
private String xdrHost;
@Value("${device.xdr.auth-code:}")
private String xdrAuthCode;
@Value("${device.xdr.api-base:/api/openapi/}")
private String xdrApiBase;
@Value("${device.xdr.data-types:}")
private String xdrDataTypes;
}
@@ -1,38 +0,0 @@
package com.config;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30))
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair
.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(new GenericJackson2JsonRedisSerializer()));
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
return RedisCacheManager.builder(factory)
.cacheDefaults(redisCacheConfiguration())
.transactionAware()
.build();
}
}
@@ -1,41 +0,0 @@
package com.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis 序列化配置
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.activateDefaultTyping(
mapper.getPolymorphicTypeValidator(),
ObjectMapper.DefaultTyping.NON_FINAL
);
serializer.setObjectMapper(mapper);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
@@ -1,40 +0,0 @@
package com.config;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
/**
* RestTemplate 配置(忽略 SSL 证书验证,适用于内网/测试环境)
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() throws Exception {
// 忽略 SSL 证书验证(仅用于内网/测试环境,生产环境建议使用真实证书)
SSLContext sslContext = SSLContextBuilder.create()
.loadTrustMaterial((chain, authType) -> true)
.build();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
sslContext, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setConnectTimeout(10000);
factory.setReadTimeout(60000);
return new RestTemplate(factory);
}
}
@@ -1,183 +0,0 @@
package com.controller;
import com.collector.CollectScheduler;
import com.common.entity.DeviceApiCollectLog;
import com.common.entity.DeviceApiConfig;
import com.common.service.DeviceApiCollectLogService;
import com.common.service.DeviceApiConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* API 采集配置管理接口
*/
@RestController
@RequestMapping("/api/collect")
public class DeviceApiCollectController {
@Autowired
private DeviceApiConfigService configService;
@Autowired
private DeviceApiCollectLogService collectLogService;
@Autowired
private CollectScheduler collectScheduler;
/**
* 全量回补功能开关,默认禁用
*/
@Value("${device.collect.full.enabled:false}")
private boolean fullCollectEnabled;
/**
* 手动触发采集(功能测试用)
* 按配置ID立即执行一次采集,不走定时调度。
*
* @param id 设备配置ID
* @param dataType 数据类型(可选,为空则采集配置中所有类型)
* @param minutes 回看窗口分钟数(可选,默认最近1小时)
*/
@PostMapping("/trigger")
public Map<String, Object> trigger(@RequestParam Long id,
@RequestParam(required = false) String dataType,
@RequestParam(required = false) Integer minutes) {
Map<String, Object> result = collectScheduler.collectNow(id, dataType, minutes);
return success(result);
}
/**
* 手动全量回补采集(同步执行,默认禁用)
* 把 [startTime, endTime] 区间内历史数据(如天眼 weblog)按子窗口由旧到新滚动逐段完整采集,
* 用于首次部署全量导入 / 漏采补数。区间较大时请求会持续较久,请按需控制 backDays/startTime。
* 该功能由配置项 device.collect.full.enabled 控制,默认 false(禁用)。
*
* @param id 设备配置ID
* @param dataType 数据类型,逗号分隔,如 weblog;为空则取配置中所有类型
* @param startTime 起始时间(毫秒时间戳,可选)
* @param endTime 结束时间(毫秒时间戳,可选,默认当前时间-1分钟)
* @param backDays 回看天数(startTime 为空时生效,默认1天)
* @param windowMinutes 子窗口分钟数(可选,默认30分钟)
* @param continueOnError 单个子窗口失败是否继续(可选,默认false,失败即终止)
*/
@PostMapping("/full")
public Map<String, Object> submitFull(@RequestParam Long id,
@RequestParam(required = false) String dataType,
@RequestParam(required = false) Long startTime,
@RequestParam(required = false) Long endTime,
@RequestParam(required = false) Integer backDays,
@RequestParam(required = false) Integer windowMinutes,
@RequestParam(required = false) Boolean continueOnError) {
if (!fullCollectEnabled) {
Map<String, Object> result = new HashMap<>();
result.put("code", 1);
result.put("message", "全量回补功能已禁用,请先设置 device.collect.full.enabled=true 后重启服务");
return result;
}
return success(collectScheduler.collectFull(id, dataType, startTime, endTime,
backDays, windowMinutes, continueOnError));
}
/**
* 查询所有采集配置
*/
@GetMapping("/config/list")
public Map<String, Object> listConfig() {
List<DeviceApiConfig> list = configService.listAll();
return success(list);
}
/**
* 查询启用的采集配置
*/
@GetMapping("/config/enabled")
public Map<String, Object> listEnabled() {
List<DeviceApiConfig> list = configService.listEnabled();
return success(list);
}
/**
* 新增采集配置
*/
@PostMapping("/config/add")
public Map<String, Object> addConfig(@RequestBody DeviceApiConfig config) {
if (config.getEnabled() == null) {
config.setEnabled(1);
}
configService.add(config);
return success("新增成功");
}
/**
* 更新采集配置
*/
@PostMapping("/config/update")
public Map<String, Object> updateConfig(@RequestBody DeviceApiConfig config) {
configService.update(config);
return success("更新成功");
}
/**
* 启用/停用采集配置
*/
@PostMapping("/config/enabled")
public Map<String, Object> updateEnabled(@RequestParam Long id, @RequestParam Integer enabled) {
configService.updateEnabled(id, enabled);
return success("操作成功");
}
/**
* 删除采集配置
*/
@PostMapping("/config/delete")
public Map<String, Object> deleteConfig(@RequestParam Long id) {
configService.delete(id);
return success("删除成功");
}
/**
* 查询采集日志(按设备类型)
*/
@GetMapping("/log/device-type")
public Map<String, Object> listLogByType(@RequestParam String deviceType) {
List<DeviceApiCollectLog> list = collectLogService.listByDeviceType(deviceType);
return success(list);
}
/**
* 清理指定日期前的采集日志
*/
@PostMapping("/log/clean")
public Map<String, Object> cleanLog(@RequestParam String beforeDate) {
LocalDateTime before = LocalDateTime.parse(beforeDate);
int count = collectLogService.cleanBefore(before);
Map<String, Object> data = new HashMap<>();
data.put("deleted", count);
return success(data);
}
/**
* 采集器健康检查
*/
@GetMapping("/health")
public Map<String, Object> health() {
Map<String, Object> data = new HashMap<>();
data.put("status", "UP");
data.put("time", LocalDateTime.now().toString());
return success(data);
}
private Map<String, Object> success(Object data) {
Map<String, Object> result = new HashMap<>();
result.put("code", 0);
result.put("message", "success");
result.put("data", data);
return result;
}
}
@@ -1,129 +0,0 @@
package com.kafka;
import com.alibaba.fastjson2.JSONObject;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import com.util.Sm4Util;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Properties;
/**
* Kafka 生产者工具
* 将采集到的数据推送至 Kafka,供下游消费处理
*/
@Component
public class KafkaProducerUtil {
private static final Logger log = LoggerFactory.getLogger(KafkaProducerUtil.class);
@Value("${spring.kafka.producer.bootstrap-servers:192.168.4.99:9092}")
private String bootstrapServers;
@Value("${device.collect.kafka.topic:device-api-collect}")
private String topic;
@Value("${syslog.sm4.generateKey:}")
private String sm4Key;
/** producer 单条消息上限(字节):需与 broker 的 message.max.bytes、topic 的 max.message.bytes 配合调整 */
@Value("${device.kafka.max-request-size:10485760}")
private int maxRequestSize;
private KafkaProducer<String, String> producer;
@PostConstruct
public void init() {
Properties props = new Properties();
props.put("bootstrap.servers", bootstrapServers);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("acks", "1");
props.put("retries", 10);
props.put("retry.backoff.ms", 500);
props.put("connections.max.idle.ms", 600000);
props.put("socket.keepalive.enable", true);
props.put("request.timeout.ms", 30000);
props.put("delivery.timeout.ms", 120000);
// 单条消息大小上限:超限时 send() 同步抛 RecordTooLargeException(fail 计数准确)
props.put("max.request.size", maxRequestSize);
producer = new KafkaProducer<>(props);
log.info("KafkaProducerUtil 初始化成功, brokers={}, topic={}", bootstrapServers, topic);
}
@PreDestroy
public void destroy() {
if (producer != null) {
producer.close();
}
}
/**
* 对消息内容进行 SM4 加密(CBC 模式,格式 Base64(IV):Base64(密文))
* 未配置密钥(syslog.sm4.generateKey)时返回明文,保持兼容
*/
private String encryptValue(String value) {
String key = sm4Key == null ? "" : sm4Key.trim();
if (key.isEmpty()) {
log.warn("未配置 syslog.sm4.generateKey,Kafka 消息以明文发送");
return value;
}
String encrypted = Sm4Util.encryptCbc(value, key);
log.debug("Kafka 消息已 SM4 加密: 明文长度={}, 密文长度={}", value.length(), encrypted.length());
return encrypted;
}
/**
* 发送单条消息(异步提交,不阻塞采集线程)
* 发送结果通过回调日志观察;producer 配置 retries=10、delivery.timeout.ms=120s 保证最终投递
* @param key 消息 key
* @param value 消息内容
* @return 是否成功提交到 producer 发送缓冲
*/
public boolean send(String key, String value) {
try {
ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, encryptValue(value));
producer.send(record, (metadata, exception) -> {
if (exception != null) {
log.error("Kafka 异步发送失败: key={}, error={}", key, exception.getMessage());
} else {
log.debug("Kafka 发送成功: key={}, partition={}, offset={}",
key, metadata.partition(), metadata.offset());
}
});
return true;
} catch (Exception e) {
log.error("Kafka 提交失败: key={}, error={}", key, e.getMessage());
return false;
}
}
/**
* 获取 topic
*/
public String getTopic() {
return topic;
}
/**
* 构建 Kafka 消息 value(统一数据模型)
* @param deviceType 设备类型:tianyan/weibu/xdr
* @param dataType 数据类型:weblog/alarm/attack/info
* @param collectTime 采集时间(毫秒)
* @param rawData 原始数据
*/
public static String buildValue(String deviceType, String dataType, long collectTime, Object rawData) {
JSONObject value = new JSONObject();
value.put("deviceType", deviceType);
value.put("dataType", dataType);
value.put("collectTime", collectTime);
value.put("rawData", rawData);
return value.toJSONString();
}
}
@@ -1,166 +0,0 @@
package com.mock;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Mock 数据生成器
* 模拟天眼/微步的原始数据,字段与真实 API 返回保持一致,
* 用于功能测试跑通:采集 -> 字段映射 -> Kafka -> 日志落库 全流程。
*
* 说明:同一时间窗口内,按序号(seq)确定性生成数据,
* 保证 mock 接口分页连续、重复请求数据一致。
*/
public class MockDataGenerator {
private MockDataGenerator() {
}
/** 内网源 IP 段 */
private static final String[] SRC_IP_PREFIX = {"192.168.1", "192.168.2", "192.168.10", "172.16.1", "172.16.5", "10.10.1"};
/** 目的 IP(模拟攻击目标/被访问服务器) */
private static final String[] DST_IPS = {"10.56.170.107", "10.56.171.164", "10.56.100.88", "10.56.100.120"};
/** 天眼 weblog URI */
private static final String[] WEBLOG_URIS = {
"/login", "/index.html", "/api/user/info", "/admin", "/download/file?id=1",
"/report/list", "/upload", "/cgi-bin/test.cgi", "/webroot/decision/login.jsp", "/console"
};
/** 天眼 alarm 攻击类型 */
private static final String[] ALARM_TITLES = {
"SQL注入攻击", "XSS跨站脚本攻击", "命令注入攻击", "Webshell上传", "暴力破解",
"目录遍历攻击", "文件包含攻击", "SSRF服务端请求伪造", "CSRF跨站请求伪造", "恶意扫描"
};
/** 微步 attack 威胁类型 */
private static final String[] WEIBU_THREAT = {
"木马远控", "蠕虫病毒", "挖矿木马", "僵尸网络", "暴力破解",
"勒索病毒", "恶意下载", "DDoS攻击", "端口扫描", "数据泄露"
};
/** 微步风险等级 */
private static final String[] RISK_LEVELS = {"high", "medium", "low"};
/** 域名后缀 */
private static final String[] DOMAIN_SUFFIX = {".com", ".net", ".org", ".cn", ".xyz", ".top"};
/** HTTP 方法 */
private static final String[] HTTP_METHODS = {"GET", "POST", "PUT", "DELETE"};
/** 协议 */
private static final String[] PROTOS = {"TCP", "UDP", "HTTP", "HTTPS"};
/** 微步 action */
private static final String[] WEIBU_ACTIONS = {"block", "allow", "detect"};
/**
* 生成天眼单条 hit(含 _source 包装,结构与真实 API 一致)
*
* @param dataType weblog / alarm
* @param startTime 窗口开始(毫秒)
* @param endTime 窗口结束(毫秒)
* @param seq 数据序号(从1开始),用于确定性分页
*/
public static Map<String, Object> buildTianyanHit(String dataType, long startTime, long endTime, int seq) {
Random random = new Random(startTime + dataType.hashCode() + seq * 31L);
Map<String, Object> hit = new HashMap<>();
hit.put("_index", "alarm".equalsIgnoreCase(dataType) ? "alarm_collection" : "skyeye-weblog");
hit.put("_source", "alarm".equalsIgnoreCase(dataType)
? buildTianyanAlarm(random, startTime, endTime, seq)
: buildTianyanWeblog(random, startTime, endTime, seq));
return hit;
}
/**
* 生成微步单条数据
*
* @param dataType attack / info
* @param startTime 窗口开始(毫秒)
* @param endTime 窗口结束(毫秒)
* @param seq 数据序号(从1开始),用于确定性分页
*/
public static Map<String, Object> buildWeibuItem(String dataType, long startTime, long endTime, int seq) {
Random random = new Random(startTime + dataType.hashCode() + seq * 31L);
long eventTime = startTime + (long) (random.nextDouble() * (endTime - startTime));
Map<String, Object> item = new HashMap<>();
item.put("event_time", eventTime);
item.put("src_ip", randomIp(random));
item.put("dst_ip", DST_IPS[random.nextInt(DST_IPS.length)]);
item.put("src_port", 1024 + random.nextInt(60000));
item.put("dst_port", random.nextInt(2) == 0 ? 80 : 443);
item.put("threat_type", WEIBU_THREAT[random.nextInt(WEIBU_THREAT.length)]);
item.put("risk_level", RISK_LEVELS[random.nextInt(RISK_LEVELS.length)]);
item.put("domain", "evil" + (random.nextInt(999) + 1) + DOMAIN_SUFFIX[random.nextInt(DOMAIN_SUFFIX.length)]);
item.put("url", "http://" + item.get("domain") + "/payload?cmd=" + (random.nextInt(9999) + 1));
item.put("md5", randomHex(random, 32));
item.put("sha256", randomHex(random, 64));
item.put("action", WEIBU_ACTIONS[random.nextInt(WEIBU_ACTIONS.length)]);
item.put("id", "wb_" + eventTime + "_" + seq + "_" + random.nextInt(99999));
return item;
}
/**
* 构建天眼 weblog 单条数据
*/
private static Map<String, Object> buildTianyanWeblog(Random random, long startTime, long endTime, int seq) {
long eventTime = startTime + (long) (random.nextDouble() * (endTime - startTime));
Map<String, Object> source = new HashMap<>();
source.put("_time", eventTime);
source.put("src_ip", randomIp(random));
source.put("dst_ip", DST_IPS[random.nextInt(DST_IPS.length)]);
source.put("src_port", 1024 + random.nextInt(60000));
source.put("dst_port", random.nextInt(2) == 0 ? 80 : 443);
source.put("proto", PROTOS[random.nextInt(PROTOS.length)]);
source.put("method", HTTP_METHODS[random.nextInt(HTTP_METHODS.length)]);
source.put("uri", WEBLOG_URIS[random.nextInt(WEBLOG_URIS.length)]);
source.put("user_agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
source.put("status", random.nextInt(2) == 0 ? 200 : 404);
source.put("request_body", random.nextInt(3) == 0 ? "{\"username\":\"admin\",\"password\":\"******\"}" : "");
source.put("response_body", "<html><body>mock weblog page</body></html>");
source.put("_id", "ty_wb_" + eventTime + "_" + seq);
return source;
}
/**
* 构建天眼 alarm 单条数据
*/
private static Map<String, Object> buildTianyanAlarm(Random random, long startTime, long endTime, int seq) {
long eventTime = startTime + (long) (random.nextDouble() * (endTime - startTime));
Map<String, Object> source = new HashMap<>();
source.put("_time", eventTime);
source.put("title", ALARM_TITLES[random.nextInt(ALARM_TITLES.length)]);
source.put("alarm_sev", 1 + random.nextInt(5));
source.put("src_ip", randomIp(random));
source.put("dst_ip", DST_IPS[random.nextInt(DST_IPS.length)]);
source.put("src_port", 1024 + random.nextInt(60000));
source.put("dst_port", 80 + random.nextInt(1000));
source.put("proto", PROTOS[random.nextInt(PROTOS.length)]);
source.put("rule_id", "TY-RULE-" + (1000 + random.nextInt(9000)));
source.put("event_id", "ty_al_" + eventTime + "_" + seq + "_" + random.nextInt(99999));
source.put("attack_type", ALARM_TITLES[random.nextInt(ALARM_TITLES.length)]);
return source;
}
/**
* 随机内网 IP
*/
private static String randomIp(Random random) {
String prefix = SRC_IP_PREFIX[random.nextInt(SRC_IP_PREFIX.length)];
return prefix + "." + (random.nextInt(254) + 1);
}
/**
* 随机十六进制字符串(md5/sha256)
*/
private static String randomHex(Random random, int length) {
StringBuilder sb = new StringBuilder();
String chars = "0123456789abcdef";
for (int i = 0; i < length; i++) {
sb.append(chars.charAt(random.nextInt(chars.length())));
}
return sb.toString();
}
}
@@ -1,216 +0,0 @@
package com.mock;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* 天眼(Tianyan) API Mock 接口
* <p>
* 模拟真实天眼的认证与日志查询接口,供功能测试使用。
* 采集器将 device_api_config.host_api 配置为
* http://127.0.0.1:8290/collectorapi/mock/tianyan 即可无缝对接。
* <p>
* 模拟接口:
* 1. POST /mock/tianyan/skyeye/v1/admin/auth 免密登录,返回 access_token
* 2. GET /mock/tianyan/skyeye/v1/admin/auth 提取 csrf_token
* 3. GET /mock/tianyan/skyeye/v1/analysis/log-search/list 日志查询(weblog)
* 4. GET /mock/tianyan/skyeye/v1/alarm/alarm/list 告警查询(alarm)
*
* 开关配置:mock.server.enabled=true 时启用,默认关闭(生产不暴露)。
*/
@RestController
@RequestMapping("/mock/tianyan")
@ConditionalOnProperty(name = "mock.server.enabled", havingValue = "true")
public class TianyanMockController {
private static final Logger log = LoggerFactory.getLogger(TianyanMockController.class);
/** 模拟数据总量(每类),分页测试用,默认250条 */
@Value("${mock.server.total-count:250}")
private int totalCount;
/**
* 免密登录,返回 access_token
* 请求:POST /skyeye/v1/admin/auth
* Headers: X-Authorization=SHA256(JSON(jsonBody)+timestamp+clientSecret)
* Body: {"client_id":"xxx","username":"xxx"}
*/
@PostMapping("/skyeye/v1/admin/auth")
public String auth(@RequestBody(required = false) String body,
@RequestHeader(value = "X-Authorization", required = false) String xAuthorization) {
log.info("[Mock] 天眼认证请求, X-Authorization={}, body={}", maskAuth(xAuthorization), body);
JSONObject resp = new JSONObject();
resp.put("status", 1000);
resp.put("message", "success");
JSONObject inner = new JSONObject();
inner.put("access_token", "mock_tianyan_token_" + System.currentTimeMillis());
JSONObject data = new JSONObject();
data.put("data", inner);
resp.put("data", data);
return resp.toJSONString();
}
/**
* 提取 csrf_token
* 请求:GET /skyeye/v1/admin/auth?token=access_token
*/
@GetMapping("/skyeye/v1/admin/auth")
public String getCsrf(@RequestParam(value = "token", required = false) String token) {
log.info("[Mock] 天眼获取 csrf_token, token={}", maskToken(token));
JSONObject resp = new JSONObject();
resp.put("status", 1000);
resp.put("message", "success");
JSONObject data = new JSONObject();
data.put("csrf_token", "mock_tianyan_csrf_" + System.currentTimeMillis());
resp.put("data", data);
return resp.toJSONString();
}
/**
* 日志查询(weblog / alarm)
* 请求:GET /skyeye/v1/analysis/log-search/list
* 参数:branch_id, start_time(ms), end_time(ms), index, category, offset, limit, mode, csrf_token, r
* 响应:{"status":1000,"data":{"data":{"search":{"hits":[{"_source":{...}}]}}}}
*
* 可选场景参数 mock_scenario:
* - empty 返回空数据
* - csrf_expired 返回 csrf_token error(触发采集器重新认证)
* - delay 延迟 5 秒返回(模拟超时)
*/
@GetMapping("/skyeye/v1/analysis/log-search/list")
public String searchLog(@RequestParam(value = "index", required = false) String index,
@RequestParam(value = "offset", defaultValue = "1") int offset,
@RequestParam(value = "limit", defaultValue = "100") int limit,
@RequestParam(value = "start_time", required = false) Long startTime,
@RequestParam(value = "end_time", required = false) Long endTime,
@RequestParam(value = "mock_scenario", required = false) String scenario) throws InterruptedException {
boolean isAlarm = index != null && index.toLowerCase().contains("alarm");
String dataType = isAlarm ? "alarm" : "weblog";
log.info("[Mock] 天眼日志查询: index={}, dataType={}, offset={}, limit={}, start={}, end={}, scenario={}",
index, dataType, offset, limit, startTime, endTime, scenario);
JSONObject resp = new JSONObject();
// 模拟场景:csrf_token 过期
if ("csrf_expired".equalsIgnoreCase(scenario)) {
resp.put("status", 2000);
resp.put("message", "csrf_token error");
return resp.toJSONString();
}
// 模拟场景:响应延迟
if ("delay".equalsIgnoreCase(scenario)) {
Thread.sleep(5000);
}
resp.put("status", 1000);
resp.put("message", "success");
// 模拟场景:空数据
long now = System.currentTimeMillis();
long start = startTime != null && startTime > 0 ? startTime : now - 60 * 60 * 1000L;
long end = endTime != null && endTime > 0 ? endTime : now;
int size = "empty".equalsIgnoreCase(scenario) ? 0
: Math.max(0, Math.min(limit, totalCount - (offset - 1)));
JSONArray hits = new JSONArray();
for (int i = 0; i < size; i++) {
Map<String, Object> hit = MockDataGenerator.buildTianyanHit(dataType, start, end, offset + i);
hits.add(new JSONObject(hit));
}
JSONObject search = new JSONObject();
search.put("hits", hits);
JSONObject inner = new JSONObject();
inner.put("search", search);
JSONObject data = new JSONObject();
data.put("data", inner);
resp.put("data", data);
return resp.toJSONString();
}
/**
* 告警查询(alarm)
* 请求:GET /skyeye/v1/alarm/alarm/list
* 参数:start_time(ms), end_time(ms), hazard_level, status, host_state, limit, offset, csrf_token, r
* 响应:{"status":1000,"data":{"data":{"rows":[{"_source":{...}}]}}}
*
* 可选场景参数 mock_scenario:
* - empty 返回空数据
* - csrf_expired 返回 csrf_token error(触发采集器重新认证)
* - delay 延迟 5 秒返回(模拟超时)
*/
@GetMapping("/skyeye/v1/alarm/alarm/list")
public String alarmList(@RequestParam(value = "offset", defaultValue = "1") int offset,
@RequestParam(value = "limit", defaultValue = "100") int limit,
@RequestParam(value = "start_time", required = false) Long startTime,
@RequestParam(value = "end_time", required = false) Long endTime,
@RequestParam(value = "mock_scenario", required = false) String scenario) throws InterruptedException {
log.info("[Mock] 天眼告警查询: offset={}, limit={}, start={}, end={}, scenario={}",
offset, limit, startTime, endTime, scenario);
JSONObject resp = new JSONObject();
// 模拟场景:csrf_token 过期
if ("csrf_expired".equalsIgnoreCase(scenario)) {
resp.put("status", 2000);
resp.put("message", "csrf_token error");
return resp.toJSONString();
}
// 模拟场景:响应延迟
if ("delay".equalsIgnoreCase(scenario)) {
Thread.sleep(5000);
}
resp.put("status", 1000);
resp.put("message", "success");
// 模拟场景:空数据
long now = System.currentTimeMillis();
long start = startTime != null && startTime > 0 ? startTime : now - 60 * 60 * 1000L;
long end = endTime != null && endTime > 0 ? endTime : now;
int size = "empty".equalsIgnoreCase(scenario) ? 0
: Math.max(0, Math.min(limit, totalCount - (offset - 1)));
JSONArray rows = new JSONArray();
for (int i = 0; i < size; i++) {
Map<String, Object> row = MockDataGenerator.buildTianyanHit("alarm", start, end, offset + i);
rows.add(new JSONObject(row));
}
JSONObject inner = new JSONObject();
inner.put("rows", rows);
JSONObject data = new JSONObject();
data.put("data", inner);
resp.put("data", data);
return resp.toJSONString();
}
private String maskAuth(String v) {
if (v == null || v.length() < 8) {
return v;
}
return v.substring(0, 8) + "***";
}
private String maskToken(String v) {
if (v == null || v.length() < 8) {
return v;
}
return v.substring(0, 8) + "***";
}
}
@@ -1,137 +0,0 @@
package com.mock;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* 微步(Weibu) API Mock 接口
* <p>
* 模拟真实微步的数据查询接口,供功能测试使用。
* 采集器将 device_api_config.host_api 配置为
* http://127.0.0.1:8290/collectorapi/mock/weibu 即可无缝对接。
* <p>
* 模拟接口:
* 1. POST /mock/weibu/api/v1/log/searchBySql 数据查询(attack/info)
*
* 开关配置:mock.server.enabled=true 时启用,默认关闭(生产不暴露)。
*/
@RestController
@RequestMapping("/mock/weibu")
@ConditionalOnProperty(name = "mock.server.enabled", havingValue = "true")
public class WeibuMockController {
private static final Logger log = LoggerFactory.getLogger(WeibuMockController.class);
/** 模拟数据总量(每类),分页测试用,默认250条 */
@Value("${mock.server.total-count:250}")
private int totalCount;
/**
* 数据查询
* 请求:POST /api/v1/log/searchBySql?api_key=xxx&auth_timestamp=xxx&sign=xxx
* Body: {"net_data_type":["attack"],"time_from":秒,"time_to":秒,
* "assets_group":[],"search_connection":false,"sql":"","columns":["*"],
* "asc":true,"page":{"cur_page":1,"page_size":100}}
* 响应:{"response_code":0,"response_msg":"OK","data":{"data":[...]}}
*
* 可选场景参数 mock_scenario:
* - empty 返回空数据
* - sign_error 返回签名错误(response_code 非0,触发采集失败分支)
* - delay 延迟 5 秒返回(模拟超时)
*/
@PostMapping("/api/v1/log/searchBySql")
public String searchBySql(@RequestParam(value = "api_key", required = false) String apiKey,
@RequestParam(value = "auth_timestamp", required = false) String authTimestamp,
@RequestParam(value = "sign", required = false) String sign,
@RequestParam(value = "mock_scenario", required = false) String scenario,
@RequestBody(required = false) String body) throws InterruptedException {
JSONObject req = body != null ? JSONObject.parseObject(body) : new JSONObject();
log.info("[Mock] 微步查询请求: api_key={}, ts={}, sign={}, body={}, scenario={}",
maskAuth(apiKey), authTimestamp, maskAuth(sign), req, scenario);
JSONObject resp = new JSONObject();
// 模拟场景:签名错误
if ("sign_error".equalsIgnoreCase(scenario)) {
resp.put("response_code", 401);
resp.put("response_msg", "signature verification failed");
return resp.toJSONString();
}
// 模拟场景:响应延迟
if ("delay".equalsIgnoreCase(scenario)) {
Thread.sleep(5000);
}
// 解析 net_data_type(attack/info)
String dataType = "attack";
JSONArray netDataType = req.getJSONArray("net_data_type");
if (netDataType != null && !netDataType.isEmpty()) {
String first = netDataType.getString(0);
if (first != null) {
dataType = first.toLowerCase();
}
}
// 解析时间窗口(秒级 -> 毫秒),缺失时默认最近1小时
long nowSec = System.currentTimeMillis() / 1000;
long timeFromSec = req.getLongValue("time_from", 0L);
long timeToSec = req.getLongValue("time_to", 0L);
long start;
long end;
if (timeFromSec > 0 && timeToSec > 0 && timeToSec >= timeFromSec) {
start = timeFromSec * 1000;
end = timeToSec * 1000;
} else {
start = (nowSec - 60 * 60) * 1000;
end = nowSec * 1000;
}
// 解析分页
int curPage = 1;
int pageSize = 100;
JSONObject page = req.getJSONObject("page");
if (page != null) {
curPage = page.getIntValue("cur_page", 1);
pageSize = page.getIntValue("page_size", 100);
}
log.info("[Mock] 微步查询: dataType={}, start={}, end={}, curPage={}, pageSize={}",
dataType, start, end, curPage, pageSize);
// 模拟场景:空数据
int size = "empty".equalsIgnoreCase(scenario) ? 0
: Math.max(0, Math.min(pageSize, totalCount - (curPage - 1) * pageSize));
JSONArray dataList = new JSONArray();
for (int i = 0; i < size; i++) {
Map<String, Object> item = MockDataGenerator.buildWeibuItem(dataType, start, end, (curPage - 1) * pageSize + i + 1);
dataList.add(new JSONObject(item));
}
resp.put("response_code", 0);
resp.put("response_msg", "OK");
JSONObject data = new JSONObject();
data.put("data", dataList);
resp.put("data", data);
return resp.toJSONString();
}
private String maskAuth(String v) {
if (v == null || v.length() < 8) {
return v;
}
return v.substring(0, 8) + "***";
}
}
@@ -1,130 +0,0 @@
package com.util;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* HTTP 请求工具(基于 RestTemplate)
*/
public class HttpUtil {
private HttpUtil() {
}
/**
* 将已构建好的 URL 字符串转为 URI 对象
* URL 中的参数已按平台要求预编码(如 branch_id 中的 %2F),
* 必须以 URI 类型传给 RestTemplate,否则默认 UriTemplateHandler 会对 % 再次编码(%2F → %252F)
*/
private static URI toUri(String url) {
return URI.create(url);
}
/**
* 发送 GET 请求
* @param restTemplate RestTemplate
* @param url URL
* @param headers 请求头(可为 null)
* @return 响应体字符串
*/
public static String get(RestTemplate restTemplate, String url, Map<String, String> headers) {
HttpHeaders httpHeaders = new HttpHeaders();
if (headers != null) {
headers.forEach(httpHeaders::set);
}
HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(toUri(url), HttpMethod.GET, entity, String.class);
return response.getBody();
}
/**
* 发送 GET 请求,同时返回响应体与 Set-Cookie(供需要登录 Cookie 的接口使用,如天眼免密登录)
* @param restTemplate RestTemplate
* @param url URL
* @param headers 请求头(可为 null)
* @return 响应结果(body + Set-Cookie 原始列表)
*/
public static GetResult getWithCookies(RestTemplate restTemplate, String url, Map<String, String> headers) {
HttpHeaders httpHeaders = new HttpHeaders();
if (headers != null) {
headers.forEach(httpHeaders::set);
}
HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(toUri(url), HttpMethod.GET, entity, String.class);
List<String> setCookies = response.getHeaders() != null
? response.getHeaders().get(HttpHeaders.SET_COOKIE) : null;
return new GetResult(response.getBody(), setCookies);
}
/**
* GET 请求响应结果封装
*/
public static class GetResult {
private final String body;
private final List<String> setCookies;
GetResult(String body, List<String> setCookies) {
this.body = body;
this.setCookies = setCookies;
}
public String getBody() {
return body;
}
/** Set-Cookie 原始串列表(可能为 null/空) */
public List<String> getSetCookies() {
return setCookies == null ? Collections.emptyList() : setCookies;
}
}
/**
* 发送 POST 请求(JSON 体)
* @param restTemplate RestTemplate
* @param url URL
* @param jsonBody JSON 请求体
* @param headers 请求头(可为 null)
* @return 响应体字符串
*/
public static String postJson(RestTemplate restTemplate, String url, String jsonBody, Map<String, String> headers) {
HttpHeaders httpHeaders = new HttpHeaders();
if (headers != null) {
headers.forEach(httpHeaders::set);
}
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "application/json");
HttpEntity<String> entity = new HttpEntity<>(jsonBody, httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(toUri(url), HttpMethod.POST, entity, String.class);
return response.getBody();
}
/**
* 发送 POST 请求(表单体)
* @param restTemplate RestTemplate
* @param url URL
* @param formParams 表单参数
* @param headers 请求头(可为 null)
* @return 响应体字符串
*/
public static String postForm(RestTemplate restTemplate, String url, Map<String, String> formParams, Map<String, String> headers) {
HttpHeaders httpHeaders = new HttpHeaders();
if (headers != null) {
headers.forEach(httpHeaders::set);
}
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
org.springframework.util.LinkedMultiValueMap<String, String> body = new org.springframework.util.LinkedMultiValueMap<>();
if (formParams != null) {
formParams.forEach(body::add);
}
HttpEntity<org.springframework.util.MultiValueMap<String, String>> entity = new HttpEntity<>(body, httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(toUri(url), HttpMethod.POST, entity, String.class);
return response.getBody();
}
}
@@ -1,114 +0,0 @@
package com.util;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.symmetric.SM4;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.util.Base64;
/**
* 国密 SM4 加解密工具类
* 基于 Hutool + Bouncy Castle 实现,与 syslog-serve / syslog-consumer 保持一致
* 支持 ECB 和 CBC 模式;推荐 CBC 模式,输出格式 "Base64(IV):Base64(密文)"
*/
public class Sm4Util {
static {
// 统一注册 Provider,确保全局生效
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
}
/**
* 生成 SM4 密钥(16字节,128位)
*
* @return 十六进制字符串格式的密钥
*/
public static String generateKey() {
byte[] key = cn.hutool.crypto.SmUtil.sm4().getSecretKey().getEncoded();
return HexUtil.encodeHexStr(key);
}
// ==================== ECB 模式(电子密码本模式) ====================
// 注意:ECB 模式不需要 IV,但安全性较低,不建议用于加密大量数据
/**
* ECB 模式加密
*
* @param plainText 明文
* @param hexKey 十六进制字符串格式的密钥
* @return Base64 编码的密文
*/
public static String encryptEcb(String plainText, String hexKey) {
SM4 sm4 = new SM4(HexUtil.decodeHex(hexKey));
return sm4.encryptBase64(plainText);
}
/**
* ECB 模式解密
*
* @param cipherText Base64 编码的密文
* @param hexKey 十六进制字符串格式的密钥
* @return 明文
*/
public static String decryptEcb(String cipherText, String hexKey) {
SM4 sm4 = new SM4(HexUtil.decodeHex(hexKey));
return sm4.decryptStr(cipherText);
}
// ==================== CBC 模式(密码分组链接模式,推荐) ====================
// 需要随机 IV(初始化向量),安全性更高;密文携带 IV,格式 "Base64(IV):Base64(密文)"
/**
* CBC 模式加密(推荐)
*
* @param plainText 明文
* @param hexKey 十六进制字符串格式的密钥
* @return 格式为 "Base64(IV):Base64(密文)" 的字符串
*/
public static String encryptCbc(String plainText, String hexKey) {
// 1. 随机生成16字节 IV
byte[] iv = RandomUtil.randomBytes(16);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// 2. 将十六进制密钥转为字节数组并包装为 SecretKey
byte[] keyBytes = HexUtil.decodeHex(hexKey);
SecretKey secretKey = new SecretKeySpec(keyBytes, "SM4");
// 3. 创建加解密器
SymmetricCrypto sm4 = new SymmetricCrypto("SM4/CBC/PKCS5Padding", secretKey, ivSpec);
// 4. 执行加密
String encryptedBase64 = sm4.encryptBase64(plainText);
String ivBase64 = Base64.getEncoder().encodeToString(iv);
return ivBase64 + ":" + encryptedBase64;
}
/**
* CBC 模式解密
*
* @param cipherTextWithIv 由 {@link #encryptCbc(String, String)} 生成的字符串
* @param hexKey 十六进制字符串格式的密钥
* @return 明文
*/
public static String decryptCbc(String cipherTextWithIv, String hexKey) {
String[] parts = cipherTextWithIv.split(":");
if (parts.length != 2) {
throw new IllegalArgumentException("无效的密文格式,应为 'Base64(IV):Base64(密文)'");
}
// 1. 解析 IV
byte[] iv = Base64.getDecoder().decode(parts[0]);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// 2. 解析密钥
byte[] keyBytes = HexUtil.decodeHex(hexKey);
SecretKey secretKey = new SecretKeySpec(keyBytes, "SM4");
// 3. 创建解密器(算法参数必须与加密时完全一致)
SymmetricCrypto sm4 = new SymmetricCrypto("SM4/CBC/PKCS5Padding", secretKey, ivSpec);
// 4. 解密
return sm4.decryptStr(parts[1]);
}
}
@@ -1,40 +0,0 @@
package com.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Spring 上下文工具类
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
}
@@ -1,80 +0,0 @@
package com.util;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/**
* 时间工具类
*/
public class TimeUtils {
/** 默认时间格式 */
public static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/** 紧凑时间格式:yyyyMMddHHmmss */
public static final DateTimeFormatter COMPACT_FORMATTER =
DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
/** 带毫秒格式:yyyyMMddHHmmssSSS */
public static final DateTimeFormatter MS_FORMATTER =
DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
/** 天眼时间格式:yyyy-MM-dd'T'HH:mm:ss.SSSZ */
public static final DateTimeFormatter TIANYAN_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
private TimeUtils() {
}
/**
* 毫秒时间戳 -> LocalDateTime
*/
public static LocalDateTime toLocalDateTime(long timestampMs) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestampMs), ZoneId.systemDefault());
}
/**
* LocalDateTime -> 毫秒时间戳
*/
public static long toTimestamp(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 当前毫秒时间戳
*/
public static long now() {
return System.currentTimeMillis();
}
/**
* 当前秒级时间戳
*/
public static long nowSecond() {
return System.currentTimeMillis() / 1000;
}
/**
* 格式化 LocalDateTime -> yyyy-MM-dd HH:mm:ss
*/
public static String format(LocalDateTime dateTime) {
return dateTime.format(DEFAULT_FORMATTER);
}
/**
* 毫秒时间戳 -> yyyy-MM-dd HH:mm:ss
*/
public static String format(long timestampMs) {
return toLocalDateTime(timestampMs).format(DEFAULT_FORMATTER);
}
/**
* 毫秒时间戳 -> yyyyMMddHHmmssSSS(用于 receive_time)
*/
public static String formatCompactMs(long timestampMs) {
return toLocalDateTime(timestampMs).format(MS_FORMATTER);
}
}
@@ -1,114 +0,0 @@
#Server Configuration
server.port=8290
server.servlet.context-path=/collectorapi
server.tomcat.uri-encoding=UTF-8
server.error.include-message=always
server.error.include-binding-errors=always
# ============================================
# 采集调度配置
# ============================================
# 定时采集间隔(毫秒),默认1分钟
device.collect.interval=60000
# 全量回补功能开关(false=禁用,POST /api/collect/full 将拒绝执行)
device.collect.full.enabled=false
# 重试配置
device.collect.retry.max-attempts=3
device.collect.retry.initial-delay=5000
device.collect.retry.multiplier=2.0
# Kafka 推送 topic
device.collect.kafka.topic=${KAFKA_COLLECTOR_TOPIC:test-topic}
# 采集线程池大小:各设备/数据类型并行采集,慢设备不阻塞其他设备
device.collect.pool-size=${COLLECT_POOL_SIZE:15}
# 微步 info(日志流量)单轮采集窗口上限(分钟),积压时逐轮追赶,0=不限(用默认30分钟上限)
device.weibu.info-max-window-minutes=${WEIBU_INFO_MAX_WINDOW_MINUTES:5}
#采集探针ID
app.service.device_collect_id=${DEVICE_COLLECT_ID:1}
# ============================================
# 天眼采集配置
# ============================================
device.tianyan.host=${TIANYAN_HOST:https://10.56.170.107}
# 免密登录 login_key(由平台管理员分配)
device.tianyan.login-key=${TIANYAN_LOGIN_KEY:QuJm4NwF3GMaXb2Z}
device.tianyan.username=${TIANYAN_USERNAME:xxadmin}
device.tianyan.branch-id=${TIANYAN_BRANCH_ID:QbJK%2FsLHi}
# 采集数据类型,逗号分隔:weblog(访问日志),alarm(告警日志)
device.tianyan.data-types=${TIANYAN_DATA_TYPES:weblog,alarm}
device.tianyan.page-size=${TIANYAN_PAGE_SIZE:1000}
# 天眼单次查询子窗口跨度(分钟),采集窗口切片预防平台深分页上限,<=0 不切片
device.tianyan.sub-window-minutes=${TIANYAN_SUB_WINDOW_MINUTES:5}
# ============================================
# 微步采集配置
# ============================================
device.weibu.host=${WEIBU_HOST:https://10.56.171.164}
device.weibu.api-key=${WEIBU_API_KEY:57d8fb2b556a471ea36b8b4441fa1c0c}
device.weibu.secret=${WEIBU_SECRET:9d967876f51d4e16bbf8bde069d12a9b}
# 采集数据类型,逗号分隔:attack(攻击日志),info(情报日志)
device.weibu.data-types=${WEIBU_DATA_TYPES:attack,info}
# 微步限流:每秒最大请求数
device.weibu.rate-limit=${WEIBU_RATE_LIMIT:10}
# 微步单次取数上限(size 两段式取数一次取全的条数上限,超出对半拆分窗口重采)
device.weibu.fetch-size-cap=${WEIBU_FETCH_SIZE_CAP:20000}
# ============================================
# XDR 采集配置(暂缓)
# ============================================
device.xdr.host=${XDR_HOST:https://127.0.0.1}
device.xdr.auth-code=${XDR_AUTH_CODE:}
device.xdr.api-base=${XDR_API_BASE:/api/openapi/}
device.xdr.data-types=${XDR_DATA_TYPES:}
# ============================================
# Kafka 配置
# ============================================
spring.kafka.producer.bootstrap-servers=192.168.4.99:9092
spring.kafka.producer.properties.retries=10
spring.kafka.producer.properties.retry.backoff.ms=500
spring.kafka.producer.properties.connections.max.idle.ms=600000
spring.kafka.producer.properties.socket.keepalive.enable=true
spring.kafka.producer.properties.request.timeout.ms=30000
spring.kafka.producer.properties.delivery.timeout.ms=120000
# SM4 加密密钥(十六进制,128位),与 syslog-serve / syslog-consumer 保持一致
# 配置后 Kafka 消息以 SM4-CBC 加密推送(格式 Base64(IV):Base64(密文));留空则明文推送
syslog.sm4.generateKey=${SYSLOG_SM4_KEY:f79548ab6fa8a304fc0115e17230358a}
#database Configuration
spring.datasource.url=jdbc:dm://192.168.4.99:5237
spring.datasource.username=SYSDBA
spring.datasource.password=caZ2TcmXNSW8L2Ap
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
spring.datasource.hikari.schema=\"PUBLIC\"
# mybatis Configuration
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.common.entity
mybatis.configuration.map-underscore-to-camel-case=true
# Redis 缓存配置
spring.redis.host=192.168.4.99
spring.redis.port=6379
# 密码(如果没有设置密码,可以省略)
spring.redis.password=redis_GdGWte
spring.redis.database=0
spring.redis.timeout=2000
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.cache.redis.time-to-live=600000
# 定时任务线程池
spring.task.scheduling.pool.size=5
# ============================================
# Mock 服务器配置(模拟天眼/微步接口,供功能测试)
# 开启后暴露 /mock/tianyan /mock/weibu 接口,采集器 host_api 指向:
# http://127.0.0.1:8290/collectorapi/mock/tianyan
# http://127.0.0.1:8290/collectorapi/mock/weibu
# 生产环境务必保持关闭
# ============================================
mock.server.enabled=${MOCK_SERVER_ENABLED:false}
# 模拟数据总量(每类),分页测试用
mock.server.total-count=${MOCK_SERVER_TOTAL_COUNT:100}
@@ -1,114 +0,0 @@
#Server Configuration
server.port=8290
server.servlet.context-path=/collectorapi
server.tomcat.uri-encoding=UTF-8
server.error.include-message=always
server.error.include-binding-errors=always
# ============================================
# 采集调度配置
# ============================================
# 定时采集间隔(毫秒),默认1分钟
device.collect.interval=60000
# 全量回补功能开关(false=禁用,POST /api/collect/full 将拒绝执行)
device.collect.full.enabled=false
# 重试配置
device.collect.retry.max-attempts=3
device.collect.retry.initial-delay=5000
device.collect.retry.multiplier=2.0
# Kafka 推送 topic
device.collect.kafka.topic=${KAFKA_COLLECTOR_TOPIC:test-topic}
# 采集线程池大小:各设备/数据类型并行采集,慢设备不阻塞其他设备
device.collect.pool-size=${COLLECT_POOL_SIZE:15}
# 微步 info(日志流量)单轮采集窗口上限(分钟),积压时逐轮追赶,0=不限(用默认30分钟上限)
device.weibu.info-max-window-minutes=${WEIBU_INFO_MAX_WINDOW_MINUTES:5}
#采集探针ID
app.service.device_collect_id=${DEVICE_COLLECT_ID:1}
# ============================================
# 天眼采集配置
# ============================================
device.tianyan.host=${TIANYAN_HOST:https://10.56.170.107}
# 免密登录 login_key(由平台管理员分配)
device.tianyan.login-key=${TIANYAN_LOGIN_KEY:QuJm4NwF3GMaXb2Z}
device.tianyan.username=${TIANYAN_USERNAME:xxadmin}
device.tianyan.branch-id=${TIANYAN_BRANCH_ID:QbJK%2FsLHi}
# 采集数据类型,逗号分隔:weblog(访问日志),alarm(告警日志)
device.tianyan.data-types=${TIANYAN_DATA_TYPES:weblog,alarm}
device.tianyan.page-size=${TIANYAN_PAGE_SIZE:1000}
# 天眼单次查询子窗口跨度(分钟),采集窗口切片预防平台深分页上限,<=0 不切片
device.tianyan.sub-window-minutes=${TIANYAN_SUB_WINDOW_MINUTES:5}
# ============================================
# 微步采集配置
# ============================================
device.weibu.host=${WEIBU_HOST:https://10.56.171.164}
device.weibu.api-key=${WEIBU_API_KEY:57d8fb2b556a471ea36b8b4441fa1c0c}
device.weibu.secret=${WEIBU_SECRET:9d967876f51d4e16bbf8bde069d12a9b}
# 采集数据类型,逗号分隔:attack(攻击日志),info(情报日志)
device.weibu.data-types=${WEIBU_DATA_TYPES:attack,info}
# 微步限流:每秒最大请求数
device.weibu.rate-limit=${WEIBU_RATE_LIMIT:10}
# 微步单次取数上限(size 两段式取数一次取全的条数上限,超出对半拆分窗口重采)
device.weibu.fetch-size-cap=${WEIBU_FETCH_SIZE_CAP:10000}
# ============================================
# XDR 采集配置(暂缓)
# ============================================
device.xdr.host=${XDR_HOST:https://127.0.0.1}
device.xdr.auth-code=${XDR_AUTH_CODE:}
device.xdr.api-base=${XDR_API_BASE:/api/openapi/}
device.xdr.data-types=${XDR_DATA_TYPES:}
# ============================================
# Kafka 配置
# ============================================
spring.kafka.producer.bootstrap-servers=192.168.4.99:9092
spring.kafka.producer.properties.retries=10
spring.kafka.producer.properties.retry.backoff.ms=500
spring.kafka.producer.properties.connections.max.idle.ms=600000
spring.kafka.producer.properties.socket.keepalive.enable=true
spring.kafka.producer.properties.request.timeout.ms=30000
spring.kafka.producer.properties.delivery.timeout.ms=120000
# SM4 加密密钥(十六进制,128位),与 syslog-serve / syslog-consumer 保持一致
# 配置后 Kafka 消息以 SM4-CBC 加密推送(格式 Base64(IV):Base64(密文));留空则明文推送
syslog.sm4.generateKey=${SYSLOG_SM4_KEY:f79548ab6fa8a304fc0115e17230358a}
#database Configuration
spring.datasource.url=jdbc:dm://192.168.4.99:5237
spring.datasource.username=SYSDBA
spring.datasource.password=caZ2TcmXNSW8L2Ap
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
spring.datasource.hikari.schema=ECOSYS
# mybatis Configuration
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.common.entity
mybatis.configuration.map-underscore-to-camel-case=true
# Redis 缓存配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123
spring.redis.database=0
spring.redis.timeout=2000
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.cache.redis.time-to-live=600000
# 定时任务线程池
spring.task.scheduling.pool.size=5
# ============================================
# Mock 服务器配置(模拟天眼/微步接口,供功能测试)
# 开启后暴露 /mock/tianyan /mock/weibu 接口,采集器 host_api 指向:
# http://127.0.0.1:8290/collectorapi/mock/tianyan
# http://127.0.0.1:8290/collectorapi/mock/weibu
# 生产环境务必保持关闭
# ============================================
mock.server.enabled=${MOCK_SERVER_ENABLED:false}
# 模拟数据总量(每类),分页测试用
mock.server.total-count=${MOCK_SERVER_TOTAL_COUNT:100}
@@ -1,52 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration >
<!-- 定义日志输出格式 -->
<property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
<!-- 控制台输出 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- 文件输出,每天滚动 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/syslog-collector-api.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每小时滚动 -->
<fileNamePattern>logs/syslog-collector-api.%d{yyyy-MM-dd-HH}.log</fileNamePattern>
<!-- 保留的日志文件的最大小时数 -->
<maxHistory>120</maxHistory>
<!-- 所有归档日志文件的总大小上限 -->
<totalSizeCap>50GB</totalSizeCap>
<!-- 启动时是否清理过期日志 -->
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- 设置日志级别:root=INFO。DEBUG 会导致逐条消息完整内容、HTTP 请求细节等海量日志刷屏(大量磁盘/IO 占用),排查问题时可临时调回 DEBUG -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
<!-- 专门屏蔽ProducerConfig的INFO日志 -->
<logger name="org.apache.kafka.clients.producer.ProducerConfig" level="WARN"/>
<!-- 更通用地屏蔽整个Kafka客户端包的INFO日志 -->
<logger name="org.apache.kafka.clients" level="WARN"/>
<!-- 方法1: 设置 Kafka 相关包为 WARN 级别 -->
<logger name="org.apache.kafka" level="WARN"/>
<logger name="kafka" level="WARN"/>
<!-- 方法2: 更精确地控制特定类 -->
<logger name="org.apache.kafka.common.utils.AppInfoParser" level="ERROR"/>
<logger name="org.apache.kafka.common.metrics.Metrics" level="ERROR"/>
</configuration>
@@ -1,61 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.common.mapper.DeviceApiCollectLogMapper">
<resultMap id="BaseResultMap" type="com.common.entity.DeviceApiCollectLog">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="device_id" property="deviceId" jdbcType="INTEGER"/>
<result column="device_type" property="deviceType" jdbcType="VARCHAR"/>
<result column="data_type" property="dataType" jdbcType="VARCHAR"/>
<result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
<result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
<result column="total_count" property="totalCount" jdbcType="INTEGER"/>
<result column="success_count" property="successCount" jdbcType="INTEGER"/>
<result column="fail_count" property="failCount" jdbcType="INTEGER"/>
<result column="cost_ms" property="costMs" jdbcType="BIGINT"/>
<result column="status" property="status" jdbcType="VARCHAR"/>
<result column="error_msg" property="errorMsg" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id, device_id, device_type, data_type, start_time, end_time,
total_count, success_count, fail_count, cost_ms, status, error_msg, create_time
</sql>
<insert id="insert" parameterType="com.common.entity.DeviceApiCollectLog" useGeneratedKeys="true" keyProperty="id">
INSERT INTO device_api_collect_log
(device_id, device_type, data_type, start_time, end_time,
total_count, success_count, fail_count, cost_ms, status, error_msg, create_time)
VALUES
(#{deviceId}, #{deviceType}, #{dataType}, #{startTime}, #{endTime},
#{totalCount}, #{successCount}, #{failCount}, #{costMs}, #{status}, #{errorMsg}, NOW())
</insert>
<select id="selectById" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM device_api_collect_log
WHERE id = #{id}
</select>
<select id="selectByDeviceType" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM device_api_collect_log
WHERE device_type = #{deviceType}
ORDER BY id DESC
</select>
<select id="selectByTimeRange" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM device_api_collect_log
WHERE start_time &gt;= #{startTime}
AND end_time &lt;= #{endTime}
ORDER BY id DESC
</select>
<delete id="deleteByTimeRange">
DELETE FROM device_api_collect_log
WHERE start_time &lt; #{startTime}
</delete>
</mapper>
@@ -1,88 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.common.mapper.DeviceApiConfigMapper">
<resultMap id="BaseResultMap" type="com.common.entity.DeviceApiConfig">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="device_id" property="deviceId" jdbcType="INTEGER"/>
<result column="device_type" property="deviceType" jdbcType="VARCHAR"/>
<result column="device_name" property="deviceName" jdbcType="VARCHAR"/>
<result column="device_ip" property="deviceIp" jdbcType="VARCHAR"/>
<result column="host_api" property="hostApi" jdbcType="VARCHAR"/>
<result column="auth_config" property="authConfig" jdbcType="VARCHAR"/>
<result column="data_types" property="dataTypes" jdbcType="VARCHAR"/>
<result column="collect_interval_sec" property="collectIntervalSec" jdbcType="INTEGER"/>
<result column="enabled" property="enabled" jdbcType="INTEGER"/>
<result column="remark" property="remark" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id, device_id, device_type, device_name, device_ip, host_api, auth_config, data_types,
collect_interval_sec, enabled, remark, create_time, update_time
</sql>
<select id="selectEnabled" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM device_api_config
WHERE enabled = 1
ORDER BY id
</select>
<select id="selectById" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM device_api_config
WHERE id = #{id}
</select>
<select id="selectByDeviceType" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM device_api_config
WHERE device_type = #{deviceType}
ORDER BY id
</select>
<select id="selectAll" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM device_api_config
ORDER BY id
</select>
<insert id="insert" parameterType="com.common.entity.DeviceApiConfig" useGeneratedKeys="true" keyProperty="id">
INSERT INTO device_api_config
(device_id, device_type, device_name, device_ip, host_api, auth_config, data_types,
collect_interval_sec, enabled, remark, create_time, update_time)
VALUES
(#{deviceId}, #{deviceType}, #{deviceName}, #{deviceIp}, #{hostApi}, #{authConfig}, #{dataTypes},
#{collectIntervalSec}, #{enabled}, #{remark}, NOW(), NOW())
</insert>
<update id="update" parameterType="com.common.entity.DeviceApiConfig">
UPDATE device_api_config
SET device_id = #{deviceId},
device_type = #{deviceType},
device_name = #{deviceName},
device_ip = #{deviceIp},
host_api = #{hostApi},
auth_config = #{authConfig},
data_types = #{dataTypes},
collect_interval_sec = #{collectIntervalSec},
enabled = #{enabled},
remark = #{remark},
update_time = NOW()
WHERE id = #{id}
</update>
<update id="updateEnabled">
UPDATE device_api_config
SET enabled = #{enabled},
update_time = NOW()
WHERE id = #{id}
</update>
<delete id="deleteById">
DELETE FROM device_api_config WHERE id = #{id}
</delete>
</mapper>
@@ -13,8 +13,8 @@ services:
build:
context: ./consumer-rule
dockerfile: Dockerfile
image: syslog-consumer-rule-dm:1.0.1
container_name: syslog-consumer-rule-dm
image: syslog-consumer-rule:1.0.1
container_name: syslog-consumer-rule
restart: unless-stopped
environment:
# 环境配置
@@ -20,6 +20,12 @@ services:
# 环境配置
- SPRING_PROFILES_ACTIVE=prod
- TZ=Asia/Shanghai
# 生产环境只使用 DIRECT:事实与统计同事务写入,不再写旧 change-log/pending。
- STATS_WRITE_MODE=DIRECT
- STATS_INCREMENTAL_CAPTURE_ENABLED=false
- STATS_INGRESS_METADATA_ENABLED=true
- STATS_INCREMENTAL_READ_ENABLED=true
- STATS_DEVICE_RECEIVE_CHANGE_CAPTURE_ENABLED=false
# 数据库配置
- SPRING_DATASOURCE_URL=jdbc:dm://192.168.4.99:5237
- SPRING_DATASOURCE_USERNAME=SYSDBA
@@ -43,13 +49,21 @@ services:
- SPRING_REDIS_TIMEOUT=2000
- SPRING_CACHE_REDIS_TIME-TO-LIVE=600000
# Kafka配置
- SPRING_KAFKA_CONSUMER_BOOTSTRAP-SERVERS=192.168.4.99:9092
- SPRING_KAFKA_CONSUMER_GROUP-ID=agent-01-syslog-group-dm
- SPRING_KAFKA_CONSUMER_AUTO-OFFSET-RESET=latest
- SPRING_KAFKA_CONSUMER_ENABLE-AUTO-COMMIT=false
- SPRING_KAFKA_CONSUMER_BOOTSTRAP_SERVERS=192.168.4.99:9092
# 保留客户现有生产消费组名称,多节点必须完全一致,不可临时换组。
- SPRING_KAFKA_CONSUMER_GROUP_ID=agent-01-syslog-group-dm
- SPRING_KAFKA_CONSUMER_AUTO_OFFSET_RESET=latest
- SPRING_KAFKA_CONSUMER_ENABLE_AUTO_COMMIT=false
- SPRING_KAFKA_CONSUMER_TOPIC=agent-01-syslog-topic
- SPRING_KAFKA_CONSUMER_MAX-POLL-RECORDS=1000
- SPRING_KAFKA_LISTENER_ACK-MODE=manual
# consumer-rule 是活跃原始日志消费节点,多实例通过同一消费组分担分区。
- SYSLOG_RAW_CONSUMER_ENABLED=true
# Retry/Replay 与 Raw 隔离,避免未到期重试占用实时消费线程。
- SYSLOG_RELIABILITY_CONSUMER_GROUP_ID=agent-01-syslog-group-dm-retry
- SYSLOG_RELIABILITY_CONSUMER_ENABLED=true
- SYSLOG_RELIABILITY_LISTENER_CONCURRENCY=1
- SPRING_KAFKA_CONSUMER_MAX_POLL_RECORDS=500
- SPRING_KAFKA_CONSUMER_PROPERTIES_MAX_POLL_INTERVAL_MS=900000
- SPRING_KAFKA_LISTENER_ACK_MODE=manual
- SPRING_KAFKA_LISTENER_CONCURRENCY=2
- SPRING_KAFKA_LISTENER_TYPE=batch
# InfluxDB配置
@@ -57,6 +71,14 @@ services:
- INFLUXDB_TOKEN=JsUyvU8vhQEFlMM_el4Drm87fyh707IhwJNsPBucPghSdbVmdQ-UvmPcyP5NTzWxsRfEz0T51Rw4ebZUuUrmZg==
- INFLUXDB_ORG=influxdb
- INFLUXDB_BUCKET=yelangbucket
- INFLUXDB_RAW_WRITE_SCHEMA=V2
- INFLUXDB_RAW_READ_MODE=V2_ONLY
- INFLUXDB_RAW_V1_BUCKET=yelangbucket
- INFLUXDB_RAW_V2_BUCKET=yelangbucket
- INFLUXDB_RAW_UUID_BUCKET_COUNT=4096
- INFLUXDB_RAW_LEGACY_POINT_TIME_FALLBACK_ENABLED=true
- INFLUXDB_RAW_QUERY_MAX_ROWS=10000
- INFLUXDB_RAW_QUERY_MAX_RESPONSE_BYTES=10485760
- INFLUXDB_BATCH_SIZE=1000
- INFLUXDB_FLUSH_INTERVAL=1000
- INFLUXDB_CONNECTION_TIMEOUT=30s
@@ -79,4 +101,3 @@ services:
networks:
xdr-network:
driver: bridge
@@ -18,7 +18,7 @@
<slf4j.version>1.7.36</slf4j.version>
<logback.version>1.2.11</logback.version>
<config.version>1.4.2</config.version>
<junit.version>5.9.2</junit.version>
<junit.jupiter.version>5.9.2</junit.jupiter.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
@@ -26,6 +26,9 @@
<mybatis.version>3.5.10</mybatis.version>
<postgresql.version>42.5.4</postgresql.version>
<pagehelper.version>1.4.6</pagehelper.version>
<jetcache.version>2.7.8</jetcache.version>
<caffeine.version>2.9.3</caffeine.version>
<kryo5.version>5.5.0</kryo5.version>
</properties>
<description>syslog-consumer-rule</description>
<packaging>jar</packaging>
@@ -46,18 +49,12 @@
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.7</version>
</dependency>
<!-- InfluxDB Client for Java -->
<dependency>
<groupId>com.influxdb</groupId>
@@ -104,8 +101,8 @@
<!-- 达梦数据库驱动 JDK1.8 -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver</artifactId>
<version>8.1.4.169</version>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.2.141</version>
</dependency>
<dependency>
@@ -169,6 +166,26 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--
Java 8 / Spring Boot 2.7 compatible JetCache line. Keep this version fixed:
CompiledRule itself is local-only, while rule/device enrichment source caches use BOTH.
-->
<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis-lettuce</artifactId>
<version>${jetcache.version}</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>${caffeine.version}</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo5</artifactId>
<version>${kryo5.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
@@ -197,7 +214,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
@@ -226,4 +243,67 @@
</resources>
</build>
</project>
<profiles>
<profile>
<id>cache-it</id>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>add-cache-integration-tests</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources><source>src/cache-it/java</source></sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals><goal>integration-test</goal><goal>verify</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@@ -0,0 +1,55 @@
package com.Modules.NormalData.cache;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
@Testcontainers
abstract class AbstractJetCacheRedisIntegrationTest {
@Container
static final GenericContainer<?> REDIS = new GenericContainer<>(DockerImageName.parse("redis:7.2-alpine"))
.withExposedPorts(6379);
ConfigurableApplicationContext first;
ConfigurableApplicationContext second;
@BeforeEach
void startContexts() {
first = context();
second = context();
}
@AfterEach
void closeContexts() {
if (second != null) second.close();
if (first != null) first.close();
}
ConfigurableApplicationContext context() {
String uri = "redis://" + REDIS.getHost() + ':' + REDIS.getMappedPort(6379) + "/15";
return new SpringApplicationBuilder(CacheItApplication.class)
.web(WebApplicationType.NONE)
.properties("jetcache.statIntervalMinutes=0",
"jetcache.areaInCacheName=false",
"jetcache.penetrationProtect=true",
"jetcache.enableMethodCache=true",
"jetcache.local.default.type=caffeine",
"jetcache.local.default.keyConvertor=fastjson2",
"jetcache.remote.default.type=redis.lettuce",
"jetcache.remote.default.keyConvertor=fastjson2",
"jetcache.remote.default.serialPolicy=KRYO5",
"jetcache.remote.default.broadcastChannel=xdr:norm:v1:cache-invalidate",
"jetcache.remote.default.uri.0=" + uri)
.run();
}
CacheItApplication.CacheItFacade facade(ConfigurableApplicationContext context) {
return context.getBean(CacheItApplication.CacheItFacade.class);
}
}
@@ -0,0 +1,39 @@
package com.Modules.NormalData.cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.SerialPolicy;
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@SpringBootConfiguration
@EnableAutoConfiguration
@EnableMethodCache(basePackages = "com.Modules.NormalData.cache")
@EnableCreateCacheAnnotation
class CacheItApplication {
@Bean CacheItFacade cacheItFacade() { return new CacheItFacade(); }
static final class CacheItFacade {
private final AtomicInteger loads = new AtomicInteger();
@Cached(name = "xdr:norm:v1:cache-it:", key = "#key", cacheType = CacheType.BOTH,
expire = 60, localExpire = 60, timeUnit = TimeUnit.SECONDS,
localLimit = 100, syncLocal = true, serialPolicy = SerialPolicy.KRYO5)
public Object get(String key, Object loadedValue) {
loads.incrementAndGet();
return loadedValue;
}
@CacheInvalidate(name = "xdr:norm:v1:cache-it:", key = "#key")
public void invalidate(String key) { }
int loads() { return loads.get(); }
}
}
@@ -0,0 +1,17 @@
package com.Modules.NormalData.cache;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JetCacheCrossContextInvalidationIntegrationTest extends AbstractJetCacheRedisIntegrationTest {
@Test
void invalidationBroadcastRemovesOtherContextsLocalEntry() throws Exception {
facade(first).get("broadcast", "before");
assertEquals("before", facade(second).get("broadcast", "unused"));
facade(first).invalidate("broadcast");
Thread.sleep(300L);
assertEquals("after", facade(second).get("broadcast", "after"));
assertEquals(1, facade(second).loads());
}
}
@@ -0,0 +1,28 @@
package com.Modules.NormalData.cache;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JetCacheRedisFailureIntegrationTest extends AbstractJetCacheRedisIntegrationTest {
@Test
void cacheFailureDoesNotReturnAFalseNullBusinessValue() {
REDIS.getDockerClient().pauseContainerCmd(REDIS.getContainerId()).exec();
RuntimeException failure = null;
Object value = null;
try {
value = facade(first).get("redis-down", "database-value");
} catch (RuntimeException expected) {
failure = expected;
} finally {
REDIS.getDockerClient().unpauseContainerCmd(REDIS.getContainerId()).exec();
}
// JetCache 2.7 may fail-open or propagate according to error handler; either path must be explicit.
if (failure != null) {
assertNotNull(failure.getCause());
} else {
assertEquals("database-value", value);
}
}
}
@@ -0,0 +1,22 @@
package com.Modules.NormalData.cache;
import com.Modules.NormalData.compiled.RuleSource;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JetCacheSerializationIntegrationTest extends AbstractJetCacheRedisIntegrationTest {
@Test
void immutableSnapshotRoundTripsAcrossContextsWithKryo5() {
RuleSourceSnapshot source = new RuleSourceSnapshot(7L, Collections.singletonList(
new RuleSource(9L, "rule", "Rule", "json", "{\"mappers\":[]}", true, "0")),
Instant.EPOCH);
facade(first).get("snapshot", source);
RuleSourceSnapshot decoded = (RuleSourceSnapshot) facade(second).get("snapshot", null);
assertEquals(7L, decoded.getDeviceId());
assertEquals(source.getRules().get(0).getFingerprint(), decoded.getRules().get(0).getFingerprint());
}
}
@@ -0,0 +1,15 @@
package com.Modules.NormalData.cache;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JetCacheTwoLevelIntegrationTest extends AbstractJetCacheRedisIntegrationTest {
@Test
void secondContextReadsFirstContextRemoteValueThenItsOwnL1() {
assertEquals("from-a", facade(first).get("two-level", "from-a"));
assertEquals("from-a", facade(second).get("two-level", "from-b"));
assertEquals("from-a", facade(second).get("two-level", "from-c"));
assertEquals(0, facade(second).loads());
}
}
@@ -1,8 +1,7 @@
package com.Modules.NormalData;
import cn.hutool.core.date.DateTime;
import com.common.entity.DeviceCollectTask;
import com.common.entity.DeviceDevice;
import com.Modules.NormalData.compiled.*;
import com.common.entity.RuleContent.*;
import com.common.entity.SyslogNonNormalMessage;
import com.common.mapper.DmColumnMapper;
@@ -11,6 +10,8 @@ import com.common.service.*;
import com.common.util.*;
import com.config.AppProperties;
import com.config.KafkaConsumerProperties;
import com.config.NormalizationOptimizationProperties;
import com.Modules.NormalData.metrics.NormalizationRuleMetrics;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
@@ -19,7 +20,15 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.common.entity.SyslogMessage;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;
/**
* 日志标准化处理器(规则引擎模块 - Spring 管理的单例 Bean)
@@ -27,6 +36,45 @@ import java.util.*;
*/
@Service
public class LogNormalProcessor {
public enum ProcessingOutcome { SUCCESS, FILTERED }
/**
* 一条原始日志完成规则计算后的纯结果。
*
* <p>这里故意只保存“应该写什么”,不负责“怎样写入达梦”。活动 Kafka 链路会先并行产生
* 多个 {@code NormalizationResult},再由专用批写器集中提交。这样计算线程不会继续执行
* 每行一个事务,同时旧的 {@link #process(String, String, String, LocalDateTime)} 入口仍可按
* 单条模式兼容历史调用方。</p>
*/
public static final class NormalizationResult {
private final ProcessingOutcome outcome;
private final List<Map<String, Object>> rows;
private NormalizationResult(ProcessingOutcome outcome, List<Map<String, Object>> rows) {
this.outcome = outcome;
this.rows = Collections.unmodifiableList(new ArrayList<>(rows));
}
static NormalizationResult success(List<Map<String, Object>> rows) {
return new NormalizationResult(ProcessingOutcome.SUCCESS, rows);
}
static NormalizationResult filtered() {
return new NormalizationResult(ProcessingOutcome.FILTERED, Collections.emptyList());
}
public ProcessingOutcome getOutcome() {
return outcome;
}
public List<Map<String, Object>> getRows() {
return rows;
}
}
private static final ZoneId RECEIVE_TIME_ZONE = ZoneId.of("Asia/Shanghai");
private static final String LEGACY_RULE_VERSION = "legacy";
private static final Logger logger = LoggerFactory.getLogger(LogNormalProcessor.class);
// ==================== 依赖注入(Spring Bean,单例,线程安全)====================
@@ -42,6 +90,12 @@ public class LogNormalProcessor {
private final DmNormalizeRuleMapper dmNormalizeRuleMapper;
private final KafkaConsumerProperties kafkaConsumerProperties;
private final AppProperties appProperties;
private final CompiledRuleCache compiledRuleCache;
private final NormalizationOptimizationProperties optimizationProperties;
private final NormalizationShadowComparator shadowComparator;
private final NormalizationShadowExecutor shadowExecutor;
private final NormalizationRuleMetrics globalMetrics;
private final Map<String, AtomicLong> shadowLogWindows = new ConcurrentHashMap<>();
/**
* 请求级处理上下文 —— 每个 process() 调用创建新实例,彻底解决多线程竞态条件
@@ -55,15 +109,25 @@ public class LogNormalProcessor {
Map<String, String> deviceInfoMap;
String strSyslogUUID;
String strSyslogTopic;
LocalDateTime fallbackFirstReceivedAt;
LocalDateTime initialReceivedAt;
boolean isSaveNonNormal = false;
SyslogNonNormalMessage syslogNonNormalMessage = new SyslogNonNormalMessage();
List<Map<String, Object>> dmNormalizeRuleList;
List<RuleSource> dmNormalizeRuleList;
List<Map<String, Object>> dmColumnList;
LinkedHashMap<String, Object> OrginalColumnMap;
int savedCount;
int filteredCount;
int rejectedCount;
BatchRuleSnapshot batchRuleSnapshot;
BatchEnrichmentContext batchEnrichmentContext;
RuleExecutionMetrics executionMetrics;
MessageParseContext messageParseContext;
boolean shadowMode;
final List<Map<String, Object>> normalizedRows = new ArrayList<>();
}
// ==================== 构造器注入(Spring 自动完成)====================
@Autowired
public LogNormalProcessor(
DmNormalizeRuleService dmNormalizeRuleService,
DmColumnService dmColumnService,
@@ -77,6 +141,56 @@ public class LogNormalProcessor {
DmNormalizeRuleMapper dmNormalizeRuleMapper,
KafkaConsumerProperties kafkaConsumerProperties,
AppProperties appProperties
) {
this(dmNormalizeRuleService, dmColumnService, syslogNormalDataService, messageService,
logDataFilterService, logDataCompleteService, deviceCollectTaskService,
deviceDeviceService, dmColumnMapper, dmNormalizeRuleMapper, kafkaConsumerProperties,
appProperties, defaultCompiledRuleCache(), new NormalizationOptimizationProperties());
}
public LogNormalProcessor(
DmNormalizeRuleService dmNormalizeRuleService,
DmColumnService dmColumnService,
SyslogNormalDataService syslogNormalDataService,
SyslogNonNormalMessageService messageService,
LogDataFilterService logDataFilterService,
LogDataCompleteService logDataCompleteService,
DeviceCollectTaskService deviceCollectTaskService,
DeviceDeviceService deviceDeviceService,
DmColumnMapper dmColumnMapper,
DmNormalizeRuleMapper dmNormalizeRuleMapper,
KafkaConsumerProperties kafkaConsumerProperties,
AppProperties appProperties,
CompiledRuleCache compiledRuleCache,
NormalizationOptimizationProperties optimizationProperties
) {
this(dmNormalizeRuleService, dmColumnService, syslogNormalDataService, messageService,
logDataFilterService, logDataCompleteService, deviceCollectTaskService,
deviceDeviceService, dmColumnMapper, dmNormalizeRuleMapper, kafkaConsumerProperties,
appProperties, compiledRuleCache, optimizationProperties,
new NormalizationShadowComparator(), new NormalizationShadowExecutor(),
new NormalizationRuleMetrics());
}
@Autowired
public LogNormalProcessor(
DmNormalizeRuleService dmNormalizeRuleService,
DmColumnService dmColumnService,
SyslogNormalDataService syslogNormalDataService,
SyslogNonNormalMessageService messageService,
LogDataFilterService logDataFilterService,
LogDataCompleteService logDataCompleteService,
DeviceCollectTaskService deviceCollectTaskService,
DeviceDeviceService deviceDeviceService,
DmColumnMapper dmColumnMapper,
DmNormalizeRuleMapper dmNormalizeRuleMapper,
KafkaConsumerProperties kafkaConsumerProperties,
AppProperties appProperties,
CompiledRuleCache compiledRuleCache,
NormalizationOptimizationProperties optimizationProperties,
NormalizationShadowComparator shadowComparator,
NormalizationShadowExecutor shadowExecutor,
NormalizationRuleMetrics globalMetrics
) {
this.dmNormalizeRuleService = dmNormalizeRuleService;
this.dmColumnService = dmColumnService;
@@ -90,6 +204,16 @@ public class LogNormalProcessor {
this.dmNormalizeRuleMapper = dmNormalizeRuleMapper;
this.kafkaConsumerProperties = kafkaConsumerProperties;
this.appProperties = appProperties;
this.compiledRuleCache = compiledRuleCache;
this.optimizationProperties = optimizationProperties;
this.shadowComparator = shadowComparator;
this.shadowExecutor = shadowExecutor;
this.globalMetrics = globalMetrics;
}
private static CompiledRuleCache defaultCompiledRuleCache() {
NormalizationOptimizationProperties properties = new NormalizationOptimizationProperties();
return new CompiledRuleCache(new CompiledRuleCompiler(new ObjectMapper()), properties);
}
/**
@@ -99,15 +223,67 @@ public class LogNormalProcessor {
* @param syslogUUID 消息UUID
* @param syslogTopic 消息Topic(可传 null,自动使用配置值)
*/
public void process(String logMsg, String syslogUUID, String syslogTopic) {
public ProcessingOutcome process(String logMsg, String syslogUUID, String syslogTopic) {
return process(logMsg, syslogUUID, syslogTopic, null);
}
/**
* 处理一条日志,并显式传入 Kafka 链路已解析好的首次接收时间。
*
* <p>该时间会同时写入标准化表的 created_at 和 log_time。created_at 表示日志首次进入系统,
* log_time 是现场标准化表的 RANGE 分区键。原始业务代码用本次处理时间覆盖 log_time;这里改为
* 稳定的首次接收时间,使 Raw、Retry 和 Replay 始终进入同一日分区。设备事件时间继续由
* collect_time_ts 保存,不与分区时间混用。</p>
*/
public ProcessingOutcome process(String logMsg, String syslogUUID, String syslogTopic,
LocalDateTime fallbackFirstReceivedAt) {
NormalizationResult result = prepare(logMsg, syslogUUID, syslogTopic, fallbackFirstReceivedAt);
// 兼容历史的命令行/测试入口:这些调用方没有 Kafka 批次边界,只能继续逐条落库。
// 当前生产活跃的 SysLogProcessor 不走这里,而是调用 prepare() 后统一批写。
for (Map<String, Object> row : result.getRows()) {
syslogNormalDataService.insertDynamic(row);
}
return result.getOutcome();
}
/**
* 只执行标准化计算并返回待写入行,不在本方法中写两张标准化表。
*
* <p>这是批量写入的关键边界:并行线程只负责 CPU/规则计算,达梦事务由 Kafka 批次汇总后的
* 批写器统一管理。非标日志仍沿用原有独立保存路径,不属于本次两张标准化表的改造范围。</p>
*/
public NormalizationResult prepare(String logMsg, String syslogUUID, String syslogTopic,
LocalDateTime fallbackFirstReceivedAt) {
return prepare(logMsg, syslogUUID, syslogTopic, fallbackFirstReceivedAt,
null, new BatchEnrichmentContext(false, deviceCollectTaskService, deviceDeviceService),
new RuleExecutionMetrics());
}
/** 活动 Kafka 链路入口:显式使用当前 poll 的规则快照、补全上下文和聚合指标。 */
public NormalizationResult prepare(String logMsg, String syslogUUID, String syslogTopic,
LocalDateTime fallbackFirstReceivedAt,
BatchRuleSnapshot batchRuleSnapshot,
BatchEnrichmentContext batchEnrichmentContext,
RuleExecutionMetrics executionMetrics) {
// [流程节点6] 按设备加载标准化规则,完成字段解析、映射、过滤、补全和数据质量检查。
ProcessContext ctx = new ProcessContext();
ctx.strSyslogUUID = syslogUUID;
ctx.strSyslogTopic = (syslogTopic != null && !syslogTopic.isEmpty())
? syslogTopic
: kafkaConsumerProperties.getTopic();
ctx.fallbackFirstReceivedAt = fallbackFirstReceivedAt;
ctx.isSaveNonNormal = false;
ctx.syslogNonNormalMessage = new SyslogNonNormalMessage();
ctx.batchRuleSnapshot = batchRuleSnapshot;
ctx.batchEnrichmentContext = batchEnrichmentContext == null
? new BatchEnrichmentContext(false, deviceCollectTaskService, deviceDeviceService)
: batchEnrichmentContext;
ctx.executionMetrics = executionMetrics == null ? new RuleExecutionMetrics() : executionMetrics;
ctx.messageParseContext = new MessageParseContext(
optimizationProperties.getMessageParseCache().isEnabled(), globalMetrics);
// [流程节点6.1] 拆分 Kafka 完整消息,分别得到设备元数据头和实际 Syslog 正文。
// 解析消息
ctx.strKafkaMessage = logMsg;
if (!logMsg.isEmpty()) {
@@ -120,68 +296,285 @@ public class LogNormalProcessor {
}
// 执行处理
init(ctx);
return init(ctx);
}
/** 活动 Kafka 链路使用已经解析好的 envelope,避免并行任务重复解析消息头。 */
public NormalizationResult prepare(PreparedNormalizationInput input,
BatchRuleSnapshot batchRuleSnapshot,
BatchEnrichmentContext batchEnrichmentContext,
RuleExecutionMetrics executionMetrics) {
ProcessContext ctx = new ProcessContext();
ctx.strSyslogUUID = input.getEventId();
ctx.strSyslogTopic = input.getTopic() == null || input.getTopic().isEmpty()
? kafkaConsumerProperties.getTopic() : input.getTopic();
ctx.fallbackFirstReceivedAt = input.getReceivedAt();
ctx.initialReceivedAt = input.getReceivedAt();
ctx.strKafkaMessage = input.getFullMessage();
ctx.strLogMsg = input.getSyslogBody();
ctx.strDeviceInfo = input.getDeviceHeader();
ctx.deviceInfoMap = input.getDeviceInfo();
ctx.strDataType = input.getDataType();
ctx.batchRuleSnapshot = batchRuleSnapshot;
ctx.batchEnrichmentContext = batchEnrichmentContext == null
? new BatchEnrichmentContext(false, deviceCollectTaskService, deviceDeviceService)
: batchEnrichmentContext;
ctx.executionMetrics = executionMetrics == null ? new RuleExecutionMetrics() : executionMetrics;
ctx.messageParseContext = new MessageParseContext(
optimizationProperties.getMessageParseCache().isEnabled(), globalMetrics);
return init(ctx);
}
public BatchRuleSnapshot captureRuleSnapshot(Set<Long> deviceIds) {
Map<Long, List<RuleSource>> rules = new LinkedHashMap<>();
if (deviceIds != null) {
for (Long deviceId : deviceIds) {
if (deviceId == null || deviceId <= 0) continue;
rules.put(deviceId, rulesForDevice(deviceId));
}
}
return new BatchRuleSnapshot(rules, System.currentTimeMillis());
}
public BatchEnrichmentContext createBatchEnrichmentContext() {
return new BatchEnrichmentContext(
optimizationProperties.getBatchEnrichmentCache().isEnabled(),
deviceCollectTaskService, deviceDeviceService, globalMetrics);
}
/**
* 初始化数据处理(所有运行时状态通过 ctx 传递,线程安全)
*/
private void init(ProcessContext ctx) {
Map<String, String> mapdev = SyslogParser.parseKeyValuePairs(ctx.strDeviceInfo);
private NormalizationResult init(ProcessContext ctx) {
// [流程节点6.2] 解析设备上下文,确定 device_id 和 data_type,作为选择标准化规则的依据。
Map<String, String> mapdev = ctx.deviceInfoMap == null
? SyslogParser.parseKeyValuePairs(ctx.strDeviceInfo) : ctx.deviceInfoMap;
ctx.deviceInfoMap = mapdev;
if (ctx.initialReceivedAt == null) {
ctx.initialReceivedAt = resolveInitialReceivedAt(
mapdev.get("receive_time"), ctx.fallbackFirstReceivedAt);
}
long deviceID = Long.parseLong(mapdev.get("device_id"));
System.out.println("device_id:" + deviceID);
logger.debug("标准化开始: eventId={}, deviceId={}", ctx.strSyslogUUID, deviceID);
ctx.strDataType = mapdev.get("data_type");
try {
if (deviceID > 0) {
ctx.dmNormalizeRuleList = dmNormalizeRuleService.selectByDeviceIdAuto(deviceID);
ctx.dmNormalizeRuleList = ctx.batchRuleSnapshot == null
? rulesForDevice(deviceID) : ctx.batchRuleSnapshot.rulesFor(deviceID);
}
for (int i = 0; i < ctx.dmNormalizeRuleList.size(); i++) {
try {
Map<String, Object> dmNormalizeRule = ctx.dmNormalizeRuleList.get(i);
String data_type = dmNormalizeRule.get("data_type").toString();
System.out.println("索引: " + i + ", 值: " + dmNormalizeRule);
System.out.println("normalrule ID: " + dmNormalizeRule.get("id")
+ ", display_name:" + dmNormalizeRule.get("display_name")
+ " data_type:" + dmNormalizeRule.get("data_type"));
ctx.OrginalColumnMap = getMessageToMap(dmNormalizeRule, ctx.strLogMsg, data_type);
if ((ctx.OrginalColumnMap == null) || (ctx.OrginalColumnMap.size() == 0)) {
logger.error("OrginalColumnMap 对象获取为空");
ctx.syslogNonNormalMessage.setReason("Log解析异常");
ctx.syslogNonNormalMessage.setReasonDetail("log解析异常,返回规则名称:"
+ dmNormalizeRule.get("display_name") + ",OrginalColumnMap 对象获取为空");
if (ctx.isSaveNonNormal == false)
SaveNonNormalMessage(ctx, deviceID, DateTime.now());
continue;
}
List<HashMap<String, Object>> destColumnList = getRuleContentMappers(dmNormalizeRule);
List<HashMap<String, Object>> ruleColumnList = getNormalColumnList(ctx.OrginalColumnMap, destColumnList);
Map<String, Object> destMap = getColumnMap(ruleColumnList);
// 数据处理-过滤规则
if (logDataFilterService.evaluateFilterRule(
dmNormalizeRule.get("rule_content").toString(), destMap))
continue;
// 数据处理-补全规则
logDataCompleteService.processDataCompletion(
dmNormalizeRule.get("rule_content").toString(), destMap);
SaveNormalData(ctx, deviceID, DateTime.now(), destMap,
Long.parseLong(dmNormalizeRule.get("id").toString()),
dmNormalizeRule.get("name").toString());
} catch (Exception ex) {
logger.error("泛化规则处理失败:" + ex.getMessage());
System.out.println(ex.getMessage());
}
if (ctx.dmNormalizeRuleList == null || ctx.dmNormalizeRuleList.isEmpty()) {
// 原文已在进入标准化前写入 InfluxDB。设备没有绑定规则是可预期的业务终态,
// 继续 Retry/DLT 不会凭空产生规则,只会重复消耗 Kafka、线程和日志 I/O。
// 因此只跳过达梦标准化并允许 ACK;系统依赖和规则执行异常仍由下方异常链处理。
return NormalizationResult.filtered();
}
if (optimizationProperties.getCompiledRule().isEnabled()) {
return executeCompiled(ctx, deviceID);
}
NormalizationResult legacy = executeLegacy(ctx, deviceID);
shadowCompareIfSampled(ctx, deviceID, legacy);
return legacy;
} catch (Exception ex) {
logger.error("处理日志消息异常:" + ex.getMessage());
System.out.println(ex.getMessage());
// [SYSLOG-ALARM][NORM][已修复] 消息级异常不再转换为成功返回,确保未达终态时不会 ACK。
logger.error("处理日志消息异常, eventId={}", ctx.strSyslogUUID, ex);
throw ex instanceof RuntimeException ? (RuntimeException) ex
: new IllegalStateException("标准化处理失败", ex);
}
}
private NormalizationResult executeLegacy(ProcessContext ctx, long deviceId) {
for (RuleSource source : ctx.dmNormalizeRuleList) {
try {
executeLegacyRule(ctx, deviceId, source);
} catch (Exception failure) {
logger.error("泛化规则处理失败, eventId={}, ruleId={}",
ctx.strSyslogUUID, source.getId(), failure);
throw new IllegalStateException("标准化规则处理失败", failure);
}
}
return finish(ctx);
}
private NormalizationResult executeCompiled(ProcessContext ctx, long deviceId) {
for (RuleSource source : ctx.dmNormalizeRuleList) {
CompiledRule rule;
try {
rule = compiledRuleCache.get(source, ctx.executionMetrics);
} catch (CompiledRuleCompiler.RuleCompilationException incompatibleRule) {
// 编译执行是兼容性优化,不能让一条历史规则阻塞整个 Kafka 批次。
// 首次失败原因由 CompiledRuleCache 按 fingerprint 限频记录;此处只回退该规则。
try {
executeLegacyRule(ctx, deviceId, source);
} catch (Exception legacyFailure) {
logger.error("编译失败规则回退旧执行器后仍失败, eventId={}, ruleId={}",
ctx.strSyslogUUID, source.getId(), legacyFailure);
throw new IllegalStateException("标准化规则回退处理失败", legacyFailure);
}
continue;
}
try {
ParsedMessage parsed = ctx.messageParseContext.parse(rule, ctx.strLogMsg, ctx.executionMetrics);
if (parsed == null || parsed.getFields().isEmpty()) {
rejectParsedMessage(ctx, deviceId, source);
continue;
}
long mappingStarted = System.nanoTime();
Map<String, Object> destination = rule.map(parsed);
ctx.executionMetrics.addFieldMappingNanos(System.nanoTime() - mappingStarted);
long filterStarted = System.nanoTime();
if (rule.isFiltered(destination)) {
ctx.executionMetrics.addFilterCompleteNanos(System.nanoTime() - filterStarted);
ctx.filteredCount++;
continue;
}
rule.complete(destination);
ctx.executionMetrics.addFilterCompleteNanos(System.nanoTime() - filterStarted);
assemble(ctx, deviceId, source, destination);
} catch (Exception failure) {
if (!ctx.shadowMode) {
logger.error("编译规则处理失败, eventId={}, ruleId={}",
ctx.strSyslogUUID, source.getId(), failure);
}
throw new IllegalStateException("编译标准化规则处理失败", failure);
}
}
return finish(ctx);
}
/** 执行单条旧格式规则,供旧主路径和编译不兼容规则的精确回退共同使用。 */
private void executeLegacyRule(ProcessContext ctx, long deviceId, RuleSource source) {
Map<String, Object> rule = source.asLegacyMap();
long parseStarted = System.nanoTime();
globalMetrics.ruleJsonParse();
ctx.OrginalColumnMap = getMessageToMap(rule, ctx.strLogMsg, source.getDataType());
ctx.executionMetrics.addMessageParseNanos(System.nanoTime() - parseStarted);
if (ctx.OrginalColumnMap == null || ctx.OrginalColumnMap.isEmpty()) {
rejectParsedMessage(ctx, deviceId, source);
return;
}
long mappingStarted = System.nanoTime();
globalMetrics.ruleJsonParse();
List<HashMap<String, Object>> destinationColumns = getRuleContentMappers(rule);
Map<String, Object> destination = getColumnMap(
getNormalColumnList(ctx.OrginalColumnMap, destinationColumns));
ctx.executionMetrics.addFieldMappingNanos(System.nanoTime() - mappingStarted);
long filterStarted = System.nanoTime();
globalMetrics.ruleJsonParse();
if (logDataFilterService.evaluateFilterRule(source.getRuleContent(), destination)) {
ctx.executionMetrics.addFilterCompleteNanos(System.nanoTime() - filterStarted);
ctx.filteredCount++;
return;
}
globalMetrics.ruleJsonParse();
logDataCompleteService.processDataCompletion(source.getRuleContent(), destination);
ctx.executionMetrics.addFilterCompleteNanos(System.nanoTime() - filterStarted);
assemble(ctx, deviceId, source, destination);
}
private void assemble(ProcessContext ctx, long deviceId, RuleSource source,
Map<String, Object> destination) {
long started = System.nanoTime();
ctx.normalizedRows.add(prepareNormalData(ctx, deviceId, destination,
source.getId(), source.getName()));
ctx.executionMetrics.addAssembleNanos(System.nanoTime() - started);
ctx.savedCount++;
}
private void rejectParsedMessage(ProcessContext ctx, long deviceId, RuleSource source) {
ctx.syslogNonNormalMessage.setReason("Log解析异常");
ctx.syslogNonNormalMessage.setReasonDetail("log解析异常,返回规则名称:"
+ source.getDisplayName() + ",OrginalColumnMap 对象获取为空");
if (!ctx.shadowMode && !ctx.isSaveNonNormal) SaveNonNormalMessage(ctx, deviceId, DateTime.now());
ctx.rejectedCount++;
}
private NormalizationResult finish(ProcessContext ctx) {
if (ctx.savedCount > 0) return NormalizationResult.success(ctx.normalizedRows);
if (ctx.filteredCount > 0 && ctx.rejectedCount == 0) return NormalizationResult.filtered();
throw new IllegalArgumentException("Syslog 未产生可用标准化结果: eventId=" + ctx.strSyslogUUID);
}
private void shadowCompareIfSampled(ProcessContext ctx, long deviceId, NormalizationResult legacy) {
NormalizationOptimizationProperties.CompiledRule config = optimizationProperties.getCompiledRule();
if (!config.isShadowCompareEnabled()
|| !shadowComparator.sampled(ctx.strSyslogUUID, config.getShadowSampleRate())) {
globalMetrics.shadow("skipped");
return;
}
ProcessContext shadow = shadowContext(ctx);
Future<NormalizationResult> future;
try {
future = shadowExecutor.submit(() -> executeCompiled(shadow, deviceId));
} catch (RejectedExecutionException saturated) {
globalMetrics.shadow("skipped");
return;
}
try {
NormalizationResult candidate = future.get(config.getShadowTimeoutMs(), TimeUnit.MILLISECONDS);
NormalizationShadowComparator.Diff diff = shadowComparator.compare(legacy, candidate);
ctx.executionMetrics.shadowCompared(!diff.isMatches());
globalMetrics.shadow(diff.isMatches() ? "match" : "mismatch");
if (!diff.isMatches() && shouldLogShadow(diff.getType())) {
logger.warn("CompiledRule影子差异: eventHash={}, ruleId={}, type={}, fields={}",
shadowComparator.eventHash(ctx.strSyslogUUID), firstRuleId(ctx),
diff.getType(), diff.getFields());
}
} catch (TimeoutException timeout) {
future.cancel(true);
globalMetrics.shadow("timeout");
if (shouldLogShadow("timeout")) {
logger.warn("CompiledRule影子超时: eventHash={}, ruleId={}, type=timeout",
shadowComparator.eventHash(ctx.strSyslogUUID), firstRuleId(ctx));
}
} catch (Exception failure) {
globalMetrics.shadow("error");
if (failure instanceof InterruptedException) Thread.currentThread().interrupt();
if (shouldLogShadow("error")) {
logger.warn("CompiledRule影子失败: eventHash={}, ruleId={}, type=error, error={}",
shadowComparator.eventHash(ctx.strSyslogUUID), firstRuleId(ctx),
failure.getClass().getSimpleName());
}
}
}
private long firstRuleId(ProcessContext ctx) {
return ctx.dmNormalizeRuleList == null || ctx.dmNormalizeRuleList.isEmpty()
? -1L : ctx.dmNormalizeRuleList.get(0).getId();
}
private boolean shouldLogShadow(String type) {
long now = System.currentTimeMillis();
AtomicLong next = shadowLogWindows.computeIfAbsent(type, ignored -> new AtomicLong());
long current = next.get();
return now >= current && next.compareAndSet(current, now + 60_000L);
}
private ProcessContext shadowContext(ProcessContext source) {
ProcessContext shadow = new ProcessContext();
shadow.strLogMsg = source.strLogMsg;
shadow.strKafkaMessage = source.strKafkaMessage;
shadow.strDeviceInfo = source.strDeviceInfo;
shadow.strDataType = source.strDataType;
shadow.deviceInfoMap = source.deviceInfoMap;
shadow.strSyslogUUID = source.strSyslogUUID;
shadow.strSyslogTopic = source.strSyslogTopic;
shadow.fallbackFirstReceivedAt = source.fallbackFirstReceivedAt;
shadow.initialReceivedAt = source.initialReceivedAt;
shadow.dmNormalizeRuleList = source.dmNormalizeRuleList;
shadow.batchEnrichmentContext = source.batchEnrichmentContext;
shadow.executionMetrics = new RuleExecutionMetrics();
shadow.messageParseContext = new MessageParseContext(
optimizationProperties.getMessageParseCache().isEnabled(), globalMetrics);
shadow.shadowMode = true;
return shadow;
}
private List<RuleSource> rulesForDevice(long deviceId) {
List<RuleSource> loaded = dmNormalizeRuleService.selectByDeviceIdAuto(deviceId);
return loaded == null ? Collections.emptyList() : loaded;
}
// ==================== 以下为原有业务方法 ====================
public Cropper_paramsType getCropperParams(Map<String, Object> dmNormalizeRule) {
@@ -249,17 +642,11 @@ public class LogNormalProcessor {
LinkedHashMap<String, Object> linkMap = null;
if ("json".equals("json")) { // 默认走 json 分支,已无实例字段 strDataType
SyslogMessage msg = SyslogParser.parse(MsgContent);
System.out.println("解析结果-log content: " + msg.getMessage().toString());
String complexJson = "{\"source\":\"honeypot1\",\"id\":\"f6a13c35-bf9d-4da6-a181-50ce23e7ef6a\"}";
System.out.println("complexJson content: " + complexJson);
if ("dev".equals(appProperties.getRunEnvironment())) {
LinkedHashMap<String, Object> flatMap = JsonParser.jsonToMap(complexJson);
flatMap.forEach((key, value) -> System.out.println(key + " = " + value));
return flatMap;
return JsonParser.jsonToMap(complexJson);
} else {
LinkedHashMap<String, Object> flatMap = JsonParser.jsonToMap(msg.getMessage());
flatMap.forEach((key, value) -> System.out.println(key + " = " + value));
return flatMap;
return JsonParser.jsonToMap(msg.getMessage());
}
}
return linkMap;
@@ -303,9 +690,7 @@ public class LogNormalProcessor {
public List<Map<String, Object>> getRuleList(long device_id) {
List<Map<String, Object>> rulelst = dmNormalizeRuleService.selectByDeviceId((long) 1);
if (rulelst != null) {
System.out.println("rulelst: " + rulelst);
} else {
if (rulelst == null) {
logger.error(" List<Map<String, Object>> rulelst is null!");
}
return rulelst;
@@ -318,7 +703,6 @@ public class LogNormalProcessor {
for (Map.Entry<String, Object> entry : orginColumnMap.entrySet()) {
for (Map<String, Object> map : destColumnList) {
if (map.get("origin_field").toString().equals(entry.getKey())) {
System.out.println(map);
Map<String, Object> normalColumMap = new LinkedHashMap<>();
normalColumMap.put("origin_field", entry.getKey());
normalColumMap.put("dest_field", map.get("dest_field").toString());
@@ -343,9 +727,9 @@ public class LogNormalProcessor {
entry.getValue().toString(), action_param.get("timezone").toString());
normalColumMap.put("dest_field_value", longTime);
} catch (Exception e) {
logger.error("时间类型转换错误,源值:" + entry.getValue().toString()
+ ",java_date_format:" + action_param.get("java_date_format").toString());
e.printStackTrace();
logger.warn("时间类型转换错误,源字段={}, 目标字段={}, javaDateFormat={}, 原因={}",
entry.getKey(), map.get("dest_field"), action_param.get("java_date_format"),
e.getMessage());
}
}
columnlist.add((HashMap<String, Object>) normalColumMap);
@@ -379,55 +763,96 @@ public class LogNormalProcessor {
return columnMap;
}
public void SaveNormalData(ProcessContext ctx, long deviceId, DateTime logtime,
Map<String, Object> logColumnMap,
long normalizeRuleId, String normalizeRuleName) {
/**
* 补齐一条标准化结果所需的所有业务字段。
*
* <p>本方法不允许访问标准化表 Mapper,也不允许开启事务;它必须是“构造结果”的纯边界,
* 否则批内并行线程仍会退化为逐条 INSERT。</p>
*/
private Map<String, Object> prepareNormalData(ProcessContext ctx, long deviceId,
Map<String, Object> logColumnMap,
long normalizeRuleId, String normalizeRuleName) {
try {
if (logColumnMap.isEmpty()) {
logger.error("SaveNormalData ->logColumnMap 为空,syslogUUID:" + ctx.strSyslogUUID);
ctx.syslogNonNormalMessage.setReason("未命中规则");
ctx.syslogNonNormalMessage.setReasonDetail("失败详情:logColumnMap对象字段为空");
if (ctx.isSaveNonNormal == false)
if (!ctx.shadowMode && !ctx.isSaveNonNormal)
SaveNonNormalMessage(ctx, deviceId, DateTime.now());
return;
throw new IllegalArgumentException("标准化结果字段为空");
}
Map<String, Object> columnMap = logColumnMap;
//补全采集设备信息
CompletionDeviceInfo(columnMap, deviceId);
//补全采集探针相关信息
CompletionCollectTaskInfo(columnMap,ctx);
long enrichmentStarted = System.nanoTime();
try {
//补全采集设备信息
CompletionDeviceInfo(columnMap, deviceId, ctx);
//补全采集探针相关信息
CompletionCollectTaskInfo(columnMap,ctx);
} finally {
ctx.executionMetrics.addEnrichmentNanos(System.nanoTime() - enrichmentStarted);
}
//补全日志基础信息
columnMap.put("device_id", deviceId);
columnMap.put("log_time", logtime);
columnMap.put("id", UUID.randomUUID().toString());
// 现场两张标准化主表均按 log_time 做 RANGE 分区。原始业务代码会无条件用 DateTime.now()
// 覆盖 log_time,其实际意图是按系统接收/处理日期分区,而不是按设备事件日期分区。
// 这里把 log_time 和 created_at 都固定为最初 receive_time:既保持原来的分区口径,
// 又避免 Kafka 积压、Retry/Replay 或跨日重试改变分区。设备事件时间保留在 collect_time_ts。
applyInitialReceiveTime(columnMap, ctx.initialReceivedAt);
// 输入字符串只用于计算固定 128 位 UUID,最终 id 仍是与旧版相同长度的 36 字符 UUID。
// 同一 event_id + 规则 + 版本在重试/回放时生成相同 id,与稳定 log_time 组成每日本地唯一键。
columnMap.put("id", deterministicResultId(ctx.strSyslogUUID, normalizeRuleId, LEGACY_RULE_VERSION));
columnMap.put("normalize_rule_id", normalizeRuleId);
columnMap.put("rule_version", LEGACY_RULE_VERSION);
columnMap.put("normalize_rule_name", normalizeRuleName);
columnMap.put("syslog_uuid", ctx.strSyslogUUID);
columnMap.put("syslog_topic", ctx.strSyslogTopic);
System.out.println("columnMap:" + columnMap);
syslogNormalDataService.insertDynamic(columnMap);
// [SYSLOG-ALARM][LOGIO] 逐条打印完整标准化字段会增加 stdout 锁竞争和日志 I/O,并可能输出敏感字段;
// 建议改为阶段计数、耗时直方图和带 event_id 的采样 DEBUG 日志。
logger.debug("标准化结果待入库: eventId={}, normalizeRuleId={}", ctx.strSyslogUUID, normalizeRuleId);
// 返回完整动态列。目标表分流和真正的 INSERT 延迟到批写阶段执行。
return columnMap;
} catch (Exception e) {
logger.error("SaveNormalData失败 ");
ctx.syslogNonNormalMessage.setReason("入库失败");
ctx.syslogNonNormalMessage.setReasonDetail("入库失败,normalizeRuleName :" + normalizeRuleName
logger.error("构造标准化结果失败, eventId={}, ruleId={}", ctx.strSyslogUUID, normalizeRuleId, e);
ctx.syslogNonNormalMessage.setReason("标准化结果构造失败");
ctx.syslogNonNormalMessage.setReasonDetail("标准化结果构造失败,normalizeRuleName :" + normalizeRuleName
+ ",失败详情:" + e.getMessage());
if (ctx.isSaveNonNormal == false)
if (!ctx.shadowMode && !ctx.isSaveNonNormal)
SaveNonNormalMessage(ctx, deviceId, DateTime.now());
throw new RuntimeException("SaveNormalData 失败: " + e.getMessage(), e);
throw new RuntimeException("构造标准化结果失败: " + e.getMessage(), e);
}
}
static void applyInitialReceiveTime(Map<String, Object> columnMap, LocalDateTime initialReceivedAt) {
columnMap.put("log_time", initialReceivedAt);
columnMap.put("created_at", initialReceivedAt);
}
static String deterministicResultId(String syslogUuid, long normalizeRuleId, String ruleVersion) {
return UUID.nameUUIDFromBytes((syslogUuid + ":" + normalizeRuleId + ":" + ruleVersion)
.getBytes(java.nio.charset.StandardCharsets.UTF_8)).toString();
}
//补全采集设备信息
public void CompletionDeviceInfo(Map<String, Object> columnMap, Long deviceID) {
CompletionDeviceInfo(columnMap, deviceID, null);
}
private void CompletionDeviceInfo(Map<String, Object> columnMap, Long deviceID, ProcessContext ctx) {
try {
DeviceDevice devInfo = deviceDeviceService.getByIdSafely(Long.valueOf(deviceID).intValue());
if (devInfo != null) {
columnMap.put("device_ip", devInfo.getIp());
columnMap.put("device_manufacturer", devInfo.getVendor());
columnMap.put("device_name", devInfo.getName());
DeviceSnapshot device = ctx == null
? DeviceSnapshot.from(deviceDeviceService.getByIdSafely(deviceID.intValue()))
: ctx.batchEnrichmentContext.device(deviceID.intValue());
if (device != null) {
columnMap.put("device_ip", device.getIp());
columnMap.put("device_manufacturer", device.getVendor());
columnMap.put("device_name", device.getName());
}
} catch (Exception e) {
logger.error("CompletionDeviceInfo 失败!ID:" + deviceID);
logger.error("CompletionDeviceInfo 失败!ID:{}", deviceID, e);
if (ctx != null && ctx.batchEnrichmentContext.isEnabled()) {
throw e instanceof RuntimeException ? (RuntimeException) e
: new IllegalStateException("设备补全失败", e);
}
}
}
@@ -436,16 +861,20 @@ public class LogNormalProcessor {
Integer collect_id=-1;
try {
collect_id = Integer.parseInt(ctx.deviceInfoMap.get("device_collect_id"));
DeviceCollectTask deviceCollectTask = deviceCollectTaskService.getById(collect_id);
if (deviceCollectTask != null) {
CollectTaskSnapshot collectTask = ctx.batchEnrichmentContext.collectTask(collect_id);
if (collectTask != null) {
columnMap.put("agent_id", collect_id);
columnMap.put("origin_agent_id",collect_id.toString() );
columnMap.put("probe_ip",deviceCollectTask.getDeviceIp() );
columnMap.put("agent_name", deviceCollectTask.getTaskName());
columnMap.put("origin_agent_name", deviceCollectTask.getTaskName());
columnMap.put("probe_ip",collectTask.getDeviceIp() );
columnMap.put("agent_name", collectTask.getTaskName());
columnMap.put("origin_agent_name", collectTask.getTaskName());
}
} catch (Exception e) {
logger.error("CompletionCollectTaskInfo 失败!探针ID:" + collect_id);
logger.error("CompletionCollectTaskInfo 失败!探针ID:{}", collect_id, e);
if (ctx.batchEnrichmentContext.isEnabled()) {
throw e instanceof RuntimeException ? (RuntimeException) e
: new IllegalStateException("采集任务补全失败", e);
}
}
}
@@ -453,7 +882,7 @@ public class LogNormalProcessor {
try {
SyslogNonNormalMessage normalMessage = new SyslogNonNormalMessage();
Integer collect_id = Integer.parseInt(ctx.deviceInfoMap.get("device_collect_id"));
DeviceCollectTask deviceCollectTask = deviceCollectTaskService.getById(collect_id);
CollectTaskSnapshot collectTask = ctx.batchEnrichmentContext.collectTask(collect_id);
normalMessage.setId(UUID.randomUUID().toString());
normalMessage.setDeviceId((int) deviceId);
normalMessage.setLogTime(logtime.toLocalDateTime());
@@ -462,14 +891,17 @@ public class LogNormalProcessor {
normalMessage.setSyslogTopic(ctx.strSyslogTopic);
normalMessage.setSyslogUuid(ctx.strSyslogUUID);
normalMessage.setHeaderMessage(ctx.strDeviceInfo);
normalMessage.setReceiveTime(ctx.initialReceivedAt);
// 非标表同样按 created_at 日分区;使用同一首次接收时间,避免重试后跑到另一天的分区。
normalMessage.setCreatedAt(ctx.initialReceivedAt.atZone(RECEIVE_TIME_ZONE).toOffsetDateTime());
normalMessage.setEtlNode("etlgo");
normalMessage.setReason(ctx.syslogNonNormalMessage.getReason());
normalMessage.setReasonDetail(ctx.syslogNonNormalMessage.getReasonDetail());
normalMessage.setRuleResult("FAIL");
normalMessage.setDeviceName(ctx.deviceInfoMap.get("device_name"));
normalMessage.setCollectTaskId(collect_id);
if (deviceCollectTask != null)
normalMessage.setCollectTaskName(deviceCollectTask.getTaskName());
if (collectTask != null)
normalMessage.setCollectTaskName(collectTask.getTaskName());
this.messageService.saveMessage(normalMessage);
ctx.isSaveNonNormal = true;
} catch (Exception e) {
@@ -478,6 +910,33 @@ public class LogNormalProcessor {
}
}
/** 将接入头中的 receive_time 写入非标表;无效值保留为空,避免伪造处理时间。 */
static LocalDateTime parseReceiveTime(String receiveTime) {
if (receiveTime == null || receiveTime.trim().isEmpty()) {
return null;
}
try {
return IngressReceiveTimeUtil.parse(receiveTime);
} catch (IllegalArgumentException invalidReceiveTime) {
logger.warn("非标日志接收时间格式异常,receiveTime={}", receiveTime);
return null;
}
}
/** 接入头优先;旧消息缺失/格式异常时,使用 Kafka 原始记录的稳定首次接收时间。 */
static LocalDateTime resolveInitialReceivedAt(String receiveTime, LocalDateTime fallbackFirstReceivedAt) {
LocalDateTime parsed = parseReceiveTime(receiveTime);
if (parsed != null) {
return parsed;
}
if (fallbackFirstReceivedAt != null) {
return fallbackFirstReceivedAt;
}
// 只有历史非 Kafka 调用入口才可能走到这里;活动 listener 必须显式传入稳定回退时间。
logger.warn("日志缺失稳定首次接收时间,兼容入口回退为当前时间");
return LocalDateTime.now(RECEIVE_TIME_ZONE);
}
public static void main(String[] args) {
// 旧入口保留,仅用于本地测试
}
@@ -1,12 +1,24 @@
package com.Modules.NormalData;
import com.influx.InfluxDBClient;
import com.Modules.NormalData.compiled.BatchEnrichmentContext;
import com.Modules.NormalData.compiled.BatchRuleSnapshot;
import com.Modules.NormalData.compiled.RuleExecutionMetrics;
import com.Modules.NormalData.compiled.PreparedNormalizationInput;
import com.common.util.SyslogParser;
import com.common.util.IngressReceiveTimeUtil;
import com.config.AppProperties;
import com.config.KafkaConsumerProperties;
import com.common.service.DmStandardizedBatchWriter;
import com.common.service.IngressStatsProjectionWriter;
import com.common.entity.IngressStatsEvent;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.client.WriteApi;
import com.influx.raw.InfluxRawPointFactory;
import com.influx.raw.PointTimeResolver;
import com.influx.raw.RawInfluxMetrics;
import com.influx.raw.RawStorageProperties;
import com.influx.raw.ReceiveTimeParser;
import com.influxdb.exceptions.BadRequestException;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,19 +26,24 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.time.LocalDate;
import java.time.LocalDate;
import java.util.concurrent.atomic.AtomicLong;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import com.common.util.Sm4Util;
@@ -34,6 +51,10 @@ import com.common.util.Sm4Util;
@Component
public class SysLogProcessor {
private static final ZoneId RECEIVE_TIME_ZONE = ZoneId.of("Asia/Shanghai");
private static final DateTimeFormatter MEASUREMENT_DATE_FORMATTER =
DateTimeFormatter.ofPattern("yyyyMMdd");
@Autowired
@Qualifier("logNormalProcessorExecutor")
private Executor taskExecutor;
@@ -41,7 +62,7 @@ public class SysLogProcessor {
@Value("${app.processor.batch-size:100}")
private int batchSize;
@Value("${app.processor.process-timeout-ms:30000}")
@Value("${app.processor.process-timeout-ms:120000}")
private long processTimeoutMs;
@Autowired
@@ -53,10 +74,25 @@ public class SysLogProcessor {
@Autowired
private LogNormalProcessor logNormalProcessor;
@Autowired
private SyslogFailureRouter failureRouter;
@Autowired
private DmStandardizedBatchWriter standardizedBatchWriter;
@Autowired
private IngressStatsProjectionWriter ingressStatsProjectionWriter;
@Autowired
private com.influx.InfluxDBClient influxClient;
@Autowired private InfluxRawPointFactory rawPointFactory;
@Autowired private PointTimeResolver pointTimeResolver;
@Autowired private RawStorageProperties rawStorageProperties;
@Autowired private RawInfluxMetrics rawInfluxMetrics;
private final AtomicInteger totalProcessed = new AtomicInteger(0);
// [SYSLOG-ALARM][KACK][已修复-主链路停用] 历史辅助方法仍保留此计数用于兼容,但唯一活动 listener 不再用全局计数决定 ACK。
private final AtomicInteger currentBatchCount = new AtomicInteger(0);
@@ -69,7 +105,7 @@ public class SysLogProcessor {
try {
// 处理业务逻辑
for (String record : records) {
System.out.println(record);
log.debug("兼容入口收到消息,长度={}", record == null ? 0 : record.length());
}
// 手动提交偏移量
ack.acknowledge(); // 告诉Kafka这条消息已成功处理
@@ -78,62 +114,92 @@ public class SysLogProcessor {
}
}
@KafkaListener(
// Raw 只处理实时原始日志,避免未到期 Retry 占用原始消费线程。
id = "syslog-consumer-rule-raw",
groupId = "${spring.kafka.consumer.group-id}",
autoStartup = "${syslog.raw-consumer.enabled:true}",
topics = "${spring.kafka.consumer.topic:agent-syslog-topic}",
concurrency = "${spring.kafka.listener.concurrency}" // 多线程消费
containerFactory = "rawKafkaListenerContainerFactory"
)
public void consumeAndParallelProcess(List<ConsumerRecord<String, String>> records, Acknowledgment ack) {
consumeBatch(records, ack, false, "RAW");
}
@KafkaListener(
id = "syslog-consumer-rule-retry-replay",
groupId = "${syslog.reliability.consumer-group-id}",
autoStartup = "${syslog.reliability.consumer.enabled:true}",
topics = {
"${syslog.reliability.retry-1m-topic:${spring.kafka.consumer.topic}.RETRY.1M}",
"${syslog.reliability.retry-10m-topic:${spring.kafka.consumer.topic}.RETRY.10M}",
"${syslog.reliability.retry-1h-topic:${spring.kafka.consumer.topic}.RETRY.1H}",
"${syslog.reliability.replay-topic:${spring.kafka.consumer.topic}.REPLAY}"
},
concurrency = "${syslog.reliability.listener.concurrency:1}"
)
public void consumeRetryAndReplay(List<ConsumerRecord<String, String>> records, Acknowledgment ack) {
consumeBatch(records, ack, true, "RETRY_REPLAY");
}
/**
* 统一批处理边界:原文批量写入 InfluxDB 后,有界并行执行标准化;
* 只有整批全部到达可靠终态才提交 offset。
*/
private void consumeBatch(List<ConsumerRecord<String, String>> records, Acknowledgment ack,
boolean enforceRetryDue, String lane) {
if (records.isEmpty()) {
ack.acknowledge();
return;
}
log.info("开始处理批次消息,数量: {}", records.size());
currentBatchCount.set(records.size());
try {
List<CompletableFuture<Void>> processingFutures = new ArrayList<>();
// 为每条消息创建独立的处理任务
for (ConsumerRecord<String, String> record : records) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
// 异步处理单条消息
log.info("收到syslogmessage:"+ Sm4Util.decryptCbc(record.value(), appProperties.getSm4Key()));
processSingleMessageAsync(record);
} catch (Exception e) {
log.error("处理消息失败, topic: {}, partition: {}, offset: {}",
record.topic(), record.partition(), record.offset(), e);
} finally {
// 计数器递减
if (currentBatchCount.decrementAndGet() == 0) {
log.debug("批次所有消息处理完成");
}
}
}, taskExecutor);
processingFutures.add(future);
}
// 等待所有处理任务完成(带超时)
CompletableFuture<Void> allFutures = CompletableFuture.allOf(
processingFutures.toArray(new CompletableFuture[0])
);
long batchStartedAt = System.currentTimeMillis();
long prepareStartedAt = batchStartedAt;
BatchMetrics metrics = new BatchMetrics();
List<PreparedMessage> preparedMessages = new ArrayList<>();
for (ConsumerRecord<String, String> record : records) {
SyslogFailureRouter.MessageContext context = failureRouter.resolve(record);
try {
allFutures.get(processTimeoutMs, TimeUnit.MILLISECONDS);
log.info("批次处理完成,总数: {}", records.size());
} catch (java.util.concurrent.TimeoutException e) {
log.warn("批次处理超时,已处理: {}/{}",
records.size() - currentBatchCount.get(), records.size());
if (enforceRetryDue) {
failureRouter.ensureRetryDue(context);
}
preparedMessages.add(prepareMessage(record, context));
} catch (SyslogFailureRouter.RetryNotDueException notDue) {
throw notDue;
} catch (Exception failure) {
metrics.record(routeFailure(record, context, failure));
}
// 提交偏移量
ack.acknowledge();
totalProcessed.addAndGet(records.size());
} catch (Exception e) {
log.error("消费处理异常", e);
ack.acknowledge(); // 异常情况下也提交偏移量,避免阻塞
}
long prepareMs = System.currentTimeMillis() - prepareStartedAt;
// 入口状态是 Kafka 已可靠接收的事实。批内按设备/探针聚合,失败时禁止 ACK;重投只重复 MIN/MAX。
int ingressGroups = ingressStatsProjectionWriter.writeBatch(ingressEvents(preparedMessages));
long influxStartedAt = System.currentTimeMillis();
List<PreparedMessage> persistedMessages = persistRawBatch(preparedMessages, metrics);
long influxMs = System.currentTimeMillis() - influxStartedAt;
long normalizeStartedAt = System.currentTimeMillis();
normalizePreparedMessages(persistedMessages, metrics);
long normalizeMs = System.currentTimeMillis() - normalizeStartedAt;
ack.acknowledge();
totalProcessed.addAndGet(records.size());
log.info("Syslog批次完成: lane={}, topicPartitions={}, records={}, prepared={}, persisted={}, "
+ "success={}, filtered={}, retry={}, dlt={}, normalizedRows={}, dmSuccessfulRows={}, "
+ "dmFailedMessages={}, ingressGroups={}, prepareMs={}, influxMs={}, normalizeMs={}, "
+ "ruleComputeMs={}, ruleSnapshotMs={}, messageParseMs={}, fieldMappingMs={}, "
+ "filterCompleteMs={}, enrichmentMs={}, ruleAssembleMs={}, compiledRuleHit={}, "
+ "compiledRuleMiss={}, messageParseHit={}, messageParseMiss={}, deviceUnique={}, "
+ "collectTaskUnique={}, shadowCompared={}, shadowMismatch={}, dmWriteMs={}, totalMs={}",
lane, topicPartitions(records), records.size(), preparedMessages.size(), persistedMessages.size(),
metrics.success.get(), metrics.filtered.get(), metrics.retry.get(), metrics.dlt.get(),
metrics.normalizedRows.get(), metrics.dmSuccessfulRows.get(), metrics.dmFailedMessages.get(), ingressGroups,
prepareMs, influxMs, normalizeMs, metrics.ruleComputeMs(), metrics.ruleSnapshotMs(),
metrics.messageParseMs(), metrics.fieldMappingMs(), metrics.filterCompleteMs(),
metrics.enrichmentMs(), metrics.ruleAssembleMs(), metrics.compiledRuleHits(),
metrics.compiledRuleMisses(), metrics.messageParseHits(), metrics.messageParseMisses(),
metrics.deviceUnique(), metrics.collectTaskUnique(), metrics.shadowCompared(),
metrics.shadowDifferences(), metrics.dmWriteMs(),
System.currentTimeMillis() - batchStartedAt);
}
/**
@@ -169,6 +235,7 @@ public class SysLogProcessor {
processMessageBatch(batch, batchIndex);
} catch (Exception e) {
log.error("批次处理异常, 批次: {}", batchIndex, e);
throw new java.util.concurrent.CompletionException(e);
}
}, taskExecutor);
@@ -189,7 +256,7 @@ public class SysLogProcessor {
} catch (Exception e) {
log.error("分批消费处理异常", e);
ack.acknowledge();
throw new IllegalStateException("分批消费失败,禁止提交 offset", e);
}
}
@@ -251,51 +318,484 @@ public class SysLogProcessor {
} catch (Exception e) {
log.error("优先级消费处理异常", e);
ack.acknowledge();
throw new IllegalStateException("优先级消费失败,禁止提交 offset", e);
}
}
/**
* 异步处理单条消息
*/
@Async("logNormalProcessorExecutor")
public void processSingleMessageAsync(ConsumerRecord<String, String> record) {
long startTime = System.currentTimeMillis();
processSingleMessage(record, failureRouter.resolve(record));
}
private void processSingleMessage(ConsumerRecord<String, String> record,
SyslogFailureRouter.MessageContext messageContext) {
// [流程节点5] 解密 Kafka 消息,生成事件标识,保存原始事件,并把消息转交标准化处理节点。
// [SYSLOG-ALARM][KACK][已修复] 活动 listener 同步调用本方法,已移除嵌套 @Async/Future,异常直接返回唯一 ACK 边界。
try {
//Message进行SM4解密
String Sm4message=Sm4Util.decryptCbc(record.value(), appProperties.getSm4Key());
String sysLogUUID =getSysLogUUID();
String strDeviceInfo= SyslogParser.substringBeforeFirstChar(Sm4message,']');
Map<String,String> mapdev =SyslogParser.parseKeyValuePairs(strDeviceInfo);
// 初始化 InfluxDB 客户端
Point point = Point.measurement("syslog_security_"+sysLogUUID.substring(0,8) )
.addTag("device_id", mapdev.get("device_id")) // 添加设备ID标签
.addTag("device_collect_id", mapdev.get("device_collect_id")) // 添加探针ID标签
.addTag("uuid", sysLogUUID) //syslog uuid
.addTag("topic", kafkaConsumerProperties.getTopic()) //kafka topic
.addField("message", Sm4message) // 添加字段
.addField("receive_time", mapdev.get("receive_time")) // 添加字段
.addField("uuid", sysLogUUID)
.time(System.currentTimeMillis(), WritePrecision.MS) ;// 毫秒级时间戳
influxClient.writePoint(point);
System.out.println("influxdb wirte syslog ,value:"+ record.key());
// 日志信息插入pg XdrHoneypot 表
//insertSingleRecord( record.value());
String syslogMessage= Sm4message;
//使用注入的 Spring Bean 进行标准化处理
logNormalProcessor.process(syslogMessage, sysLogUUID, null);
System.out.println("insert postgres syslog ,value:"+ record.key());
long costTime = System.currentTimeMillis() - startTime;
log.debug("消息处理完成, offset: {}, 耗时: {}ms", record.offset(), costTime);
PreparedMessage message = prepareMessage(record, messageContext);
influxClient.writePointBlocking(message.bucket, message.point);
if (message.v2) rawInfluxMetrics.v2Points(1L);
normalizePreparedMessage(message);
} catch (Exception e) {
// [SYSLOG-ALARM][KACK][已修复] 异常继续抛给唯一 ACK 边界,由可靠路由器分类处理。
log.error("异步处理消息失败, offset: {}", record.offset(), e);
// 这里可以添加重试逻辑或死信队列处理
throw new IllegalStateException("Syslog 处理失败", e);
}
}
/** 解密并构造原文点;此阶段不做任何 Kafka ACK。 */
private PreparedMessage prepareMessage(ConsumerRecord<String, String> record,
SyslogFailureRouter.MessageContext messageContext) {
long startTime = System.currentTimeMillis();
// [流程节点5.1] 使用约定密钥解密 Kafka 密文,恢复设备元数据头和原始 Syslog。
String decryptedMessage = Sm4Util.decryptCbc(messageContext.getPayload(), appProperties.getSm4Key());
String deviceHeader = SyslogParser.substringBeforeFirstChar(decryptedMessage, ']');
Map<String, String> deviceInfo = SyslogParser.parseKeyValuePairs(deviceHeader);
messageContext.setDeviceId(deviceInfo.get("device_id"));
// [流程节点5.2] 优先使用接入端 event_id;旧消息回退到稳定 topic-partition-offset ID。
String eventId = deviceInfo.get("event_id");
if (eventId == null || eventId.trim().isEmpty()) {
eventId = messageContext.getEventId();
}
String receiveTime = deviceInfo.get("receive_time");
ReceiveTimeParser.ParsedReceiveTime parsed = ReceiveTimeParser.parse(receiveTime);
boolean headerHasPointTime = deviceInfo.containsKey("point_time_ns");
Long envelopePointTime = messageContext.getPointTimeNs();
Long headerPointTime = pointTimeResolver.resolve(headerHasPointTime,
deviceInfo.get("point_time_ns"), parsed.getEpochMillis(), false);
if (envelopePointTime != null) {
PointTimeResolver.validate(envelopePointTime, parsed.getEpochMillis());
if (headerPointTime != null && !headerPointTime.equals(envelopePointTime)) {
throw new IllegalArgumentException("Retry/Replay point_time_ns 与原始 Kafka 载荷不一致");
}
}
Long pointTimeNs = envelopePointTime != null ? envelopePointTime : headerPointTime;
if (pointTimeNs == null && rawStorageProperties.getWriteSchema() == RawStorageProperties.WriteSchema.V2) {
pointTimeNs = pointTimeResolver.resolve(false, null, parsed.getEpochMillis(), true);
}
messageContext.setPointTimeNs(pointTimeNs);
String originalTopic = messageContext.getOriginalTopic();
InfluxRawPointFactory.RawPoint rawPoint = rawPointFactory.create(eventId, decryptedMessage,
receiveTime, deviceInfo.get("device_id"), deviceInfo.get("device_collect_id"),
originalTopic, parsed, pointTimeNs);
ResolvedInfluxTime influxTime = new ResolvedInfluxTime(parsed.getEpochMillis(),
parsed.getMeasurementDate(), parsed.getLocalDateTime());
Long deviceId = optionalLong(deviceInfo.get("device_id"));
Long collectId = optionalLong(deviceInfo.get("device_collect_id"));
PreparedNormalizationInput normalizationInput = new PreparedNormalizationInput(
eventId, originalTopic, influxTime.getReceivedAt(), deviceId, collectId,
deviceInfo.get("data_type"), SyslogParser.substringAfterFirstCloseBracket(decryptedMessage),
decryptedMessage, deviceHeader, deviceInfo);
return new PreparedMessage(record, messageContext, decryptedMessage, eventId, rawPoint.getPoint(),
rawPoint.getBucket(), rawPoint.isV2(), influxTime.getReceivedAt(), deviceId, collectId,
startTime, normalizationInput);
}
/**
* 一次 HTTP 请求持久化当前 Kafka 批次的原文。
* 批量请求如果因单条数据格式被 InfluxDB 拒绝,降级为逐条写入以定位坏消息;
* 连接、超时或服务端故障则直接保留原 offset,不制造 DLT 风暴。
*/
private List<PreparedMessage> persistRawBatch(List<PreparedMessage> messages, BatchMetrics metrics) {
if (messages.isEmpty()) {
return messages;
}
List<Point> points = new ArrayList<>(messages.size());
for (PreparedMessage message : messages) {
points.add(message.point);
}
try {
String targetBucket = messages.get(0).bucket;
influxClient.writePointsBlocking(targetBucket, points);
if (messages.get(0).v2) rawInfluxMetrics.v2Points(messages.size());
return messages;
} catch (BadRequestException badBatchData) {
if (messages.get(0).v2) rawInfluxMetrics.v2FailedBatch();
log.warn("InfluxDB 批量写入发现格式错误,降级逐条定位: batchSize={}", messages.size());
List<PreparedMessage> persisted = new ArrayList<>(messages.size());
for (PreparedMessage message : messages) {
try {
influxClient.writePointBlocking(message.bucket, message.point);
if (message.v2) rawInfluxMetrics.v2Points(1L);
persisted.add(message);
} catch (Exception singleFailure) {
metrics.record(routeFailure(message.record, message.context, singleFailure));
}
}
return persisted;
} catch (Exception dependencyFailure) {
if (messages.get(0).v2) rawInfluxMetrics.v2FailedBatch();
// 整批原文的持久化结果不可确认,必须让 Kafka 从原 offset 重投。
throw new IllegalStateException("InfluxDB 批量原文写入未取得成功确认,禁止提交 offset", dependencyFailure);
}
}
/**
* 历史单条兼容入口:原文已可靠持久化后,执行标准化并立即落库。
* 活动 Kafka 批处理链路使用 prepareNormalization(),避免在计算线程中逐条开启达梦事务。
*/
private SyslogFailureRouter.TerminalState normalizePreparedMessage(PreparedMessage message) {
LogNormalProcessor.ProcessingOutcome outcome =
logNormalProcessor.process(message.decryptedMessage, message.eventId, null, message.receivedAt);
log.debug("消息处理完成, offset: {}, 耗时: {}ms", message.record.offset(),
System.currentTimeMillis() - message.startTime);
return outcome == LogNormalProcessor.ProcessingOutcome.FILTERED
? SyslogFailureRouter.TerminalState.FILTERED
: SyslogFailureRouter.TerminalState.SUCCESS;
}
/**
* 只生成待写入的标准化结果,不在并行计算线程中访问两张达梦标准化表。
*/
private PreparedNormalization prepareNormalization(PreparedMessage message,
BatchRuleSnapshot ruleSnapshot,
BatchEnrichmentContext enrichmentContext,
RuleExecutionMetrics executionMetrics) {
LogNormalProcessor.NormalizationResult result = logNormalProcessor.prepare(
message.normalizationInput, ruleSnapshot, enrichmentContext, executionMetrics);
log.debug("消息标准化计算完成, offset: {}, rows={}, 耗时: {}ms", message.record.offset(),
result.getRows().size(), System.currentTimeMillis() - message.startTime);
return new PreparedNormalization(message, result, null);
}
/**
* 批内有界并行标准化。任务异常必须汇总回 listener;超时或系统性失败时整批不 ACK。
*/
void normalizePreparedMessages(List<PreparedMessage> messages, BatchMetrics metrics) {
if (messages.isEmpty()) {
return;
}
long snapshotStartedAt = System.nanoTime();
Set<Long> deviceIds = new HashSet<>();
for (PreparedMessage message : messages) {
if (message.deviceId != null) deviceIds.add(message.deviceId);
}
BatchRuleSnapshot ruleSnapshot = logNormalProcessor.captureRuleSnapshot(deviceIds);
BatchEnrichmentContext enrichmentContext = logNormalProcessor.createBatchEnrichmentContext();
RuleExecutionMetrics executionMetrics = new RuleExecutionMetrics();
metrics.recordRuleSnapshotMs(elapsedMillis(snapshotStartedAt));
long ruleComputeStartedAt = System.nanoTime();
List<CompletableFuture<PreparedNormalization>> futures = new ArrayList<>(messages.size());
try {
for (PreparedMessage message : messages) {
futures.add(CompletableFuture.supplyAsync(() -> {
try {
return prepareNormalization(message, ruleSnapshot, enrichmentContext, executionMetrics);
} catch (Exception failure) {
// 解析/规则计算失败仍按原消息可靠路由。只有取得 Retry/DLT Broker ACK,
// 这个消息才会携带 terminalState 返回并允许原批次继续。
return new PreparedNormalization(message, null,
routeFailure(message.record, message.context, failure));
}
}, taskExecutor));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.get(processTimeoutMs, TimeUnit.MILLISECONDS);
metrics.recordRuleComputeMs(elapsedMillis(ruleComputeStartedAt));
metrics.recordRuleExecution(executionMetrics, enrichmentContext);
List<DmStandardizedBatchWriter.WriteRequest<PreparedMessage>> writeRequests = new ArrayList<>();
Set<PreparedMessage> pendingDatabaseMessages = new HashSet<>();
for (CompletableFuture<PreparedNormalization> future : futures) {
PreparedNormalization prepared = future.join();
if (prepared.terminalState != null) {
metrics.record(prepared.terminalState);
continue;
}
if (prepared.result.getOutcome() == LogNormalProcessor.ProcessingOutcome.FILTERED) {
metrics.record(SyslogFailureRouter.TerminalState.FILTERED);
continue;
}
pendingDatabaseMessages.add(prepared.message);
for (Map<String, Object> row : prepared.result.getRows()) {
writeRequests.add(new DmStandardizedBatchWriter.WriteRequest<>(prepared.message, row));
}
}
// 批写器只返回“可归因到具体消息”的行级失败;连接、超时、事务结果未知会直接抛出,
// 从而让本方法退出并保留整个 Kafka 批次 offset。
long dmWriteStartedAt = System.nanoTime();
DmStandardizedBatchWriter.WriteReport<PreparedMessage> writeReport =
standardizedBatchWriter.write(writeRequests);
metrics.recordDmWriteMs(elapsedMillis(dmWriteStartedAt));
Set<PreparedMessage> failedMessages = writeReport.getFailures().keySet();
metrics.recordDmWrite(writeRequests.size(), writeReport.getSuccessfulRows(), failedMessages.size());
for (Map.Entry<PreparedMessage, RuntimeException> failure : writeReport.getFailures().entrySet()) {
PreparedMessage message = failure.getKey();
metrics.record(routeFailure(message.record, message.context, failure.getValue()));
}
for (PreparedMessage message : pendingDatabaseMessages) {
if (!failedMessages.contains(message)) {
metrics.record(SyslogFailureRouter.TerminalState.SUCCESS);
}
}
} catch (TimeoutException timeout) {
cancelOutstanding(futures);
throw new IllegalStateException("标准化批次处理超时,禁止提交 offset", timeout);
} catch (InterruptedException interrupted) {
cancelOutstanding(futures);
Thread.currentThread().interrupt();
throw new IllegalStateException("标准化批次被中断,禁止提交 offset", interrupted);
} catch (ExecutionException executionFailure) {
throw new IllegalStateException("标准化批次存在未到可靠终态的消息,禁止提交 offset",
executionFailure.getCause());
} catch (RuntimeException submitFailure) {
cancelOutstanding(futures);
throw submitFailure;
}
}
private static long elapsedMillis(long startedAtNanos) {
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAtNanos);
}
private void cancelOutstanding(List<? extends CompletableFuture<?>> futures) {
for (CompletableFuture<?> future : futures) {
if (!future.isDone()) {
future.cancel(true);
}
}
}
/** Retry/DLT 取得 Broker ACK 才返回;系统性故障抛出以保留原 offset。 */
private SyslogFailureRouter.TerminalState routeFailure(ConsumerRecord<String, String> record,
SyslogFailureRouter.MessageContext context,
Exception failure) {
SyslogFailureRouter.TerminalState state = failureRouter.route(record, context, failure);
if (state == SyslogFailureRouter.TerminalState.PAUSED) {
throw new IllegalStateException("系统性依赖故障,保留原 offset 等待恢复", failure);
}
return state;
}
/** 已解密、已构造原文点,但尚未提交 Kafka offset 的批内消息。 */
static final class PreparedMessage {
private final ConsumerRecord<String, String> record;
private final SyslogFailureRouter.MessageContext context;
private final String decryptedMessage;
private final String eventId;
private final Point point;
private final String bucket;
private final boolean v2;
private final LocalDateTime receivedAt;
private final Long deviceId;
private final Long deviceCollectId;
private final long startTime;
private final PreparedNormalizationInput normalizationInput;
PreparedMessage(ConsumerRecord<String, String> record,
SyslogFailureRouter.MessageContext context,
String decryptedMessage, String eventId, Point point,
String bucket, boolean v2,
LocalDateTime receivedAt, Long deviceId, Long deviceCollectId, long startTime,
PreparedNormalizationInput normalizationInput) {
this.record = record;
this.context = context;
this.decryptedMessage = decryptedMessage;
this.eventId = eventId;
this.point = point;
this.bucket = bucket;
this.v2 = v2;
// 这个时间已由接入 receive_time 或 Kafka 首次接收时间解析,Retry/Replay 不得重算为当前时间。
this.receivedAt = receivedAt;
this.deviceId = deviceId;
this.deviceCollectId = deviceCollectId;
this.startTime = startTime;
this.normalizationInput = normalizationInput;
}
/** 保留既有并行单测构造入口。 */
PreparedMessage(ConsumerRecord<String, String> record,
SyslogFailureRouter.MessageContext context,
String decryptedMessage, String eventId, Point point,
LocalDateTime receivedAt, Long deviceId, Long deviceCollectId, long startTime) {
this(record, context, decryptedMessage, eventId, point, "yelangbucket", false,
receivedAt, deviceId, deviceCollectId, startTime,
legacyNormalizationInput(decryptedMessage, eventId, receivedAt, deviceId, deviceCollectId));
}
private static PreparedNormalizationInput legacyNormalizationInput(
String message, String eventId, LocalDateTime receivedAt, Long deviceId, Long collectId) {
String header = "";
String body = message;
Map<String, String> info = Collections.emptyMap();
if (message != null && message.indexOf('[') >= 0 && message.indexOf(']') > message.indexOf('[')) {
header = SyslogParser.substringBeforeFirstChar(message, ']');
body = SyslogParser.substringAfterFirstCloseBracket(message);
info = SyslogParser.parseKeyValuePairs(header);
}
return new PreparedNormalizationInput(eventId, null, receivedAt, deviceId, collectId,
info.get("data_type"), body,
message, header, info);
}
}
private List<IngressStatsEvent> ingressEvents(List<PreparedMessage> messages) {
List<IngressStatsEvent> events = new ArrayList<>(messages.size());
for (PreparedMessage message : messages) {
events.add(new IngressStatsEvent(message.eventId, message.deviceId,
message.deviceCollectId, message.receivedAt));
}
return events;
}
private static Long optionalLong(String value) {
if (value == null || value.trim().isEmpty()) return null;
try { return Long.valueOf(value.trim()); }
catch (NumberFormatException invalid) { return null; }
}
/**
* 并行计算阶段的结果封装。result 与 terminalState 二选一:前者等待达梦批写,后者表示该消息
* 已经被 Retry/DLT 可靠接管。使用显式对象可以避免在工作线程里提前修改批次 ACK 状态。
*/
private static final class PreparedNormalization {
private final PreparedMessage message;
private final LogNormalProcessor.NormalizationResult result;
private final SyslogFailureRouter.TerminalState terminalState;
private PreparedNormalization(PreparedMessage message,
LogNormalProcessor.NormalizationResult result,
SyslogFailureRouter.TerminalState terminalState) {
this.message = message;
this.result = result;
this.terminalState = terminalState;
}
}
/** 单批终态计数,仅用于聚合日志,不参与 ACK 决策。 */
static final class BatchMetrics {
private final AtomicInteger success = new AtomicInteger();
private final AtomicInteger filtered = new AtomicInteger();
private final AtomicInteger retry = new AtomicInteger();
private final AtomicInteger dlt = new AtomicInteger();
private final AtomicInteger normalizedRows = new AtomicInteger();
private final AtomicInteger dmSuccessfulRows = new AtomicInteger();
private final AtomicInteger dmFailedMessages = new AtomicInteger();
private final AtomicLong ruleComputeMs = new AtomicLong();
private final AtomicLong ruleSnapshotMs = new AtomicLong();
private final AtomicLong dmWriteMs = new AtomicLong();
private volatile RuleExecutionMetrics ruleExecutionMetrics = new RuleExecutionMetrics();
private volatile int deviceUnique;
private volatile int collectTaskUnique;
void record(SyslogFailureRouter.TerminalState state) {
if (state == SyslogFailureRouter.TerminalState.SUCCESS) {
success.incrementAndGet();
} else if (state == SyslogFailureRouter.TerminalState.FILTERED) {
filtered.incrementAndGet();
} else if (state == SyslogFailureRouter.TerminalState.RETRY_PUBLISHED) {
retry.incrementAndGet();
} else if (state == SyslogFailureRouter.TerminalState.DLT_PUBLISHED) {
dlt.incrementAndGet();
} else {
throw new IllegalStateException("未到可靠终态的消息不能计入批次完成: " + state);
}
}
int successCount() {
return success.get();
}
long ruleComputeMs() {
return ruleComputeMs.get();
}
long dmWriteMs() {
return dmWriteMs.get();
}
long ruleSnapshotMs() { return ruleSnapshotMs.get(); }
long messageParseMs() { return ruleExecutionMetrics.getMessageParseMs(); }
long fieldMappingMs() { return ruleExecutionMetrics.getFieldMappingMs(); }
long filterCompleteMs() { return ruleExecutionMetrics.getFilterCompleteMs(); }
long enrichmentMs() { return ruleExecutionMetrics.getEnrichmentMs(); }
long ruleAssembleMs() { return ruleExecutionMetrics.getAssembleMs(); }
long compiledRuleHits() { return ruleExecutionMetrics.getCompiledHits(); }
long compiledRuleMisses() { return ruleExecutionMetrics.getCompiledMisses(); }
long messageParseHits() { return ruleExecutionMetrics.getParseHits(); }
long messageParseMisses() { return ruleExecutionMetrics.getParseMisses(); }
long shadowCompared() { return ruleExecutionMetrics.getShadowCompared(); }
long shadowDifferences() { return ruleExecutionMetrics.getShadowDifferences(); }
int deviceUnique() { return deviceUnique; }
int collectTaskUnique() { return collectTaskUnique; }
void recordRuleComputeMs(long elapsedMs) {
ruleComputeMs.set(Math.max(0L, elapsedMs));
}
void recordRuleSnapshotMs(long elapsedMs) {
ruleSnapshotMs.set(Math.max(0L, elapsedMs));
}
void recordRuleExecution(RuleExecutionMetrics executionMetrics,
BatchEnrichmentContext enrichmentContext) {
this.ruleExecutionMetrics = executionMetrics;
if (enrichmentContext != null) {
this.deviceUnique = enrichmentContext.deviceUniqueCount();
this.collectTaskUnique = enrichmentContext.collectTaskUniqueCount();
}
}
void recordDmWriteMs(long elapsedMs) {
dmWriteMs.set(Math.max(0L, elapsedMs));
}
/** 仅用于批次汇总观测,不参与 ACK 判断;ACK 仍由每条消息的可靠终态决定。 */
void recordDmWrite(int rows, int successfulRows, int failedMessages) {
this.normalizedRows.addAndGet(rows);
this.dmSuccessfulRows.addAndGet(successfulRows);
this.dmFailedMessages.addAndGet(failedMessages);
}
}
private String topicPartitions(List<ConsumerRecord<String, String>> records) {
Set<String> partitions = new HashSet<>();
for (ConsumerRecord<String, String> record : records) {
partitions.add(record.topic() + "-" + record.partition());
}
return partitions.toString();
}
/**
* 为 InfluxDB 选择跨 Kafka Retry/Replay 稳定的时间戳。
* 优先使用接入端 receive_time;旧消息缺失或格式异常时,回退到首次 Kafka 接收时间。
*/
static ResolvedInfluxTime resolveInfluxTime(String receiveTime, Instant ignoredFallbackTime) {
ReceiveTimeParser.ParsedReceiveTime parsed = ReceiveTimeParser.parse(receiveTime);
return new ResolvedInfluxTime(
parsed.getEpochMillis(), parsed.getMeasurementDate(), parsed.getLocalDateTime());
}
/** InfluxDB 点的时间戳和 measurement 日期必须由同一个接收时间计算。 */
static final class ResolvedInfluxTime {
private final long timestampMs;
private final String measurementDate;
private final LocalDateTime receivedAt;
private ResolvedInfluxTime(long timestampMs, String measurementDate, LocalDateTime receivedAt) {
this.timestampMs = timestampMs;
this.measurementDate = measurementDate;
this.receivedAt = receivedAt;
}
long getTimestampMs() {
return timestampMs;
}
String getMeasurementDate() {
return measurementDate;
}
LocalDateTime getReceivedAt() {
return receivedAt;
}
}
@@ -331,6 +831,7 @@ public class SysLogProcessor {
} catch (Exception e) {
log.error("批次处理异常: {}", batchIndex, e);
throw new IllegalStateException("批次处理失败", e);
}
}
@@ -398,19 +899,4 @@ public class SysLogProcessor {
public int getCurrentBatchCount() { return currentBatchCount; }
}
/**
* 获取日志信息UUID,格式: yyyyMMddxxxxxxxx
* @return
*/
private static String getSysLogUUID ()
{
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 定义格式 (yyyyMMdd)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// 格式化日期
String formattedDate = currentDate.format(formatter);
return formattedDate +"-"+ UUID.randomUUID() ;
}
}
@@ -0,0 +1,355 @@
package com.Modules.NormalData;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.common.service.DmStandardizedBatchWriter;
import com.influxdb.exceptions.BadRequestException;
import com.influxdb.exceptions.InfluxException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.KafkaException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import java.net.ConnectException;
import java.net.SocketException;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Kafka 异常消息可靠路由器。
*
* <p>该组件把“业务处理失败”与“原 offset 是否允许推进”放在同一个同步边界内:
* Retry/DLT 的 send Future 只有取得 Broker ACK 后才返回终态;发布失败或结果未知一律抛出,
* 让原消息保持未提交。这样不会用一次异步方法调用冒充消息已经被补偿链路接管。</p>
*/
@Slf4j
@Component
public class SyslogFailureRouter {
static final String ENVELOPE_TYPE = "SYSLOG_RELIABILITY_V1";
private static final ZoneId EVENT_ID_ZONE = ZoneId.of("Asia/Shanghai");
private static final DateTimeFormatter EVENT_ID_DATE = DateTimeFormatter.ofPattern("yyyyMMdd");
private final KafkaTemplate<String, String> kafkaTemplate;
private final ObjectMapper objectMapper;
@Value("${syslog.reliability.retry-1m-topic:${spring.kafka.consumer.topic}.RETRY.1M}")
private String retry1mTopic;
@Value("${syslog.reliability.retry-10m-topic:${spring.kafka.consumer.topic}.RETRY.10M}")
private String retry10mTopic;
@Value("${syslog.reliability.retry-1h-topic:${spring.kafka.consumer.topic}.RETRY.1H}")
private String retry1hTopic;
@Value("${syslog.reliability.dlt-topic:${spring.kafka.consumer.topic}.DLT}")
private String dltTopic;
@Value("${syslog.reliability.publish-timeout-ms:30000}")
private long publishTimeoutMs;
@Value("${syslog.reliability.consumer-version:unknown}")
private String consumerVersion;
public SyslogFailureRouter(KafkaTemplate<String, String> kafkaTemplate, ObjectMapper objectMapper) {
this.kafkaTemplate = kafkaTemplate;
// Retry/DLT 是跨服务、跨版本协议,不能复用 Redis 缓存启用了默认类型信息的 Mapper。
// 否则消息会携带 Java 专用的 @class,且安全白名单可能拒绝 FailureEnvelope,
// 导致重试信封被误当成原始密文再次处理。副本保留 JavaTimeModule,但关闭类型信息;
// FailureEnvelope 上的 ignoreUnknown 同时兼容现场已经产生的带 @class 历史消息。
this.objectMapper = objectMapper.copy();
this.objectMapper.deactivateDefaultTyping();
}
/** 解析原始、Retry 或 Replay 消息,重试时始终返回最初的加密载荷和 event_id。 */
public MessageContext resolve(ConsumerRecord<String, String> record) {
FailureEnvelope envelope = readEnvelope(record.value());
if (envelope == null) {
Instant firstReceivedAt = receivedAt(record);
return new MessageContext(record.value(), fallbackEventId(record, firstReceivedAt),
0, null, firstReceivedAt, record.topic(), null);
}
return new MessageContext(envelope.getOriginalPayload(), envelope.getEventId(),
envelope.getAttemptCount(), envelope.getNextRetryAt(),
envelope.getFirstReceivedAt() == null ? receivedAt(record) : envelope.getFirstReceivedAt(),
envelope.getOriginalTopic(), envelope.getPointTimeNs());
}
/**
* 旧 Kafka 消息没有入口 event_id 时,使用首次 Kafka 接收日期和 topic-partition-offset
* 生成带日期前缀的稳定 ID。重投同一原始 offset 始终得到同一个值,且不会再把随机 UUID
* 前 8 位误当作 InfluxDB measurement 日期。
*/
static String fallbackEventId(ConsumerRecord<String, String> record, Instant firstReceivedAt) {
String deterministicId = UUID.nameUUIDFromBytes(
(record.topic() + ":" + record.partition() + ":" + record.offset())
.getBytes(java.nio.charset.StandardCharsets.UTF_8)
).toString();
String datePrefix = EVENT_ID_DATE.format(firstReceivedAt.atZone(EVENT_ID_ZONE));
return datePrefix + "-" + deterministicId;
}
/**
* Retry Topic 本身不提供延时语义。未到期时短暂退避并拒绝 ACK,避免提前执行;
* 部署时可用支持定时投递的 Kafka 网关替换此兼容实现。
*/
public void ensureRetryDue(MessageContext context) {
if (context.getNextRetryAt() == null) {
return;
}
long remaining = context.getNextRetryAt().toEpochMilli() - System.currentTimeMillis();
if (remaining <= 0) {
return;
}
try {
Thread.sleep(Math.min(remaining, 1000L));
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
}
if (Instant.now().isBefore(context.getNextRetryAt())) {
throw new RetryNotDueException("Retry 消息尚未到达 next_retry_at");
}
}
/** 根据异常影响范围决定暂停原消息,或在 Broker ACK 后返回可提交终态。 */
public TerminalState route(ConsumerRecord<String, String> record, MessageContext context, Throwable failure) {
FailureCategory category = classify(failure);
if (category == FailureCategory.SYSTEMIC) {
// [SYSLOG-ALARM][KACK][已修复] 系统性依赖故障不倾倒 DLT,也不提交原 offset,由容器背压并等待恢复。
return TerminalState.PAUSED;
}
int nextAttempt = Math.max(0, context.getAttemptCount()) + 1;
boolean permanent = category == FailureCategory.PERMANENT_DATA;
boolean exhausted = nextAttempt > 3;
String targetTopic = (permanent || exhausted) ? dltTopic : retryTopic(nextAttempt);
Instant nextRetryAt = (permanent || exhausted) ? null : Instant.now().plusSeconds(delaySeconds(nextAttempt));
FailureEnvelope source = readEnvelope(record.value());
FailureEnvelope envelope = new FailureEnvelope();
envelope.setEnvelopeType(ENVELOPE_TYPE);
envelope.setEventId(context.getEventId());
envelope.setDeviceId(context.getDeviceId() != null ? context.getDeviceId()
: (source == null ? null : source.getDeviceId()));
envelope.setOriginalTopic(source == null ? record.topic() : source.getOriginalTopic());
envelope.setOriginalPartition(source == null ? record.partition() : source.getOriginalPartition());
envelope.setOriginalOffset(source == null ? record.offset() : source.getOriginalOffset());
envelope.setCurrentTopic(record.topic());
// [SYSLOG-ALARM][ETIME][已修复] Retry/DLT/Replay 始终携带首次 Kafka 接收时间,
// 不在每次转投时重置,供 InfluxDB 幂等时间戳和全链路对账使用。
envelope.setFirstReceivedAt(context.getFirstReceivedAt());
envelope.setFailedStage(detectStage(failure));
envelope.setErrorCode(errorCode(category));
envelope.setErrorType(rootCause(failure).getClass().getName());
envelope.setErrorMessage(sanitize(rootCause(failure).getMessage()));
envelope.setAttemptCount(nextAttempt);
envelope.setNextRetryAt(nextRetryAt);
envelope.setRuleId(source == null ? null : source.getRuleId());
envelope.setRuleVersion(source == null ? null : source.getRuleVersion());
envelope.setConsumerVersion(consumerVersion);
envelope.setOriginalPayload(context.getPayload());
envelope.setPointTimeNs(context.getPointTimeNs());
publishAndAwait(targetTopic, context.getEventId(), envelope);
TerminalState state = (permanent || exhausted)
? TerminalState.DLT_PUBLISHED : TerminalState.RETRY_PUBLISHED;
log.warn("异常消息已由 Kafka 可靠接管: eventId={}, state={}, target={}, errorCode={}, attempt={}",
context.getEventId(), state, targetTopic, envelope.getErrorCode(), nextAttempt);
return state;
}
private void publishAndAwait(String topic, String key, FailureEnvelope envelope) {
try {
String body = objectMapper.writeValueAsString(envelope);
// [流程节点4.3] 必须等待 Broker ACK;仅调用 send() 不构成 RETRY_PUBLISHED/DLT_PUBLISHED。
kafkaTemplate.send(topic, key, body).get(publishTimeoutMs, TimeUnit.MILLISECONDS);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw new FailurePublishException("发布异常消息时线程被中断,原 offset 不得提交", interrupted);
} catch (ExecutionException | TimeoutException publishFailure) {
throw new FailurePublishException("Retry/DLT 未取得 Broker ACK,原 offset 不得提交", publishFailure);
} catch (Exception serializationFailure) {
throw new FailurePublishException("异常消息序列化失败,原 offset 不得提交", serializationFailure);
}
}
FailureCategory classify(Throwable failure) {
// 批写器已经把失败范围缩小到单条并完成了达梦错误判别。必须先识别这些包装异常,
// 不能继续让内部 SQLException 的粗粒度规则把一条永久坏数据误判成整库故障,
// 否则该 Kafka 分区会永久停在同一 offset。
if (containsType(failure, DmStandardizedBatchWriter.PermanentRowWriteException.class)) {
return FailureCategory.PERMANENT_DATA;
}
if (containsType(failure, DmStandardizedBatchWriter.RetryableRowWriteException.class)) {
return FailureCategory.TRANSIENT;
}
Throwable root = rootCause(failure);
// InfluxDB 400 数据格式错误只影响当前消息,可直接隔离;
// 连接、超时、5xx 等 InfluxDB 故障属于系统性依赖故障,必须保留原 offset,避免 DLT 风暴。
if (root instanceof BadRequestException) {
return FailureCategory.PERMANENT_DATA;
}
if (root instanceof InfluxException) {
return FailureCategory.SYSTEMIC;
}
if (root instanceof DataAccessResourceFailureException || root instanceof SQLException
|| root instanceof ConnectException || root instanceof KafkaException
|| root instanceof RejectedExecutionException) {
return FailureCategory.SYSTEMIC;
}
if (root instanceof SocketException || root instanceof TimeoutException) {
return FailureCategory.TRANSIENT;
}
if (containsInChain(failure, "规则", "rule_content", "configuration", "配置")) {
return FailureCategory.CONFIGURATION_RULE;
}
if (root instanceof IllegalArgumentException || root instanceof SecurityException) {
return FailureCategory.PERMANENT_DATA;
}
return FailureCategory.UNKNOWN;
}
private boolean containsType(Throwable failure, Class<? extends Throwable> expectedType) {
Throwable current = failure;
while (current != null) {
if (expectedType.isInstance(current)) {
return true;
}
current = current.getCause();
}
return false;
}
private boolean containsInChain(Throwable failure, String... tokens) {
Throwable current = failure;
while (current != null) {
String message = String.valueOf(current.getMessage()).toLowerCase(Locale.ROOT);
for (String token : tokens) {
if (message.contains(token.toLowerCase(Locale.ROOT))) return true;
}
current = current.getCause();
}
return false;
}
private FailureEnvelope readEnvelope(String value) {
if (value == null || value.length() < 2 || value.charAt(0) != '{') {
return null;
}
try {
FailureEnvelope envelope = objectMapper.readValue(value, FailureEnvelope.class);
return envelope != null && ENVELOPE_TYPE.equals(envelope.getEnvelopeType()) ? envelope : null;
} catch (Exception ignored) {
return null;
}
}
/** Kafka 时间戳是旧消息缺失 receive_time 时的稳定回退值。 */
private Instant receivedAt(ConsumerRecord<String, String> record) {
return record.timestamp() > 0 ? Instant.ofEpochMilli(record.timestamp()) : Instant.now();
}
private String retryTopic(int attempt) {
if (attempt == 1) return retry1mTopic;
if (attempt == 2) return retry10mTopic;
return retry1hTopic;
}
private long delaySeconds(int attempt) {
if (attempt == 1) return 60L;
if (attempt == 2) return 600L;
return 3600L;
}
private String errorCode(FailureCategory category) {
return "SYSLOG_" + category.name();
}
private String detectStage(Throwable failure) {
String text = (failure.getMessage() + " " + rootCause(failure).getClass().getSimpleName()).toLowerCase(Locale.ROOT);
if (text.contains("decrypt") || text.contains("sm4") || text.contains("解密")) return "DECRYPT";
if (text.contains("sql") || text.contains("database") || text.contains("入库")) return "DB_WRITE";
if (text.contains("parse") || text.contains("解析")) return "PARSE";
return "NORMALIZE";
}
private Throwable rootCause(Throwable failure) {
Throwable current = failure;
while (current.getCause() != null && current.getCause() != current) current = current.getCause();
return current;
}
private String sanitize(String message) {
if (message == null) return "unknown";
String sanitized = message.replaceAll("(?i)(password|token|secret|key)\\s*[=:]\\s*[^,;\\s]+", "$1=***")
.replaceAll("[\\r\\n\\t]+", " ");
return sanitized.length() <= 2048 ? sanitized : sanitized.substring(0, 2048);
}
enum FailureCategory { PERMANENT_DATA, CONFIGURATION_RULE, TRANSIENT, SYSTEMIC, UNKNOWN }
public enum TerminalState { SUCCESS, FILTERED, RETRY_PUBLISHED, DLT_PUBLISHED, PAUSED }
@Data
public static class MessageContext {
private final String payload;
private final String eventId;
private final int attemptCount;
private final Instant nextRetryAt;
private final Instant firstReceivedAt;
private final String originalTopic;
private Long pointTimeNs;
private String deviceId;
public MessageContext(String payload, String eventId, int attemptCount,
Instant nextRetryAt, Instant firstReceivedAt) {
this(payload, eventId, attemptCount, nextRetryAt, firstReceivedAt, null, null);
}
public MessageContext(String payload, String eventId, int attemptCount,
Instant nextRetryAt, Instant firstReceivedAt,
String originalTopic, Long pointTimeNs) {
this.payload = payload; this.eventId = eventId; this.attemptCount = attemptCount;
this.nextRetryAt = nextRetryAt; this.firstReceivedAt = firstReceivedAt;
this.originalTopic = originalTopic; this.pointTimeNs = pointTimeNs;
}
}
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class FailureEnvelope {
private String envelopeType;
private String eventId;
private String deviceId;
private String originalTopic;
private Integer originalPartition;
private Long originalOffset;
private String currentTopic;
private Instant firstReceivedAt;
private String failedStage;
private String errorCode;
private String errorType;
private String errorMessage;
private int attemptCount;
private Instant nextRetryAt;
private String ruleId;
private String ruleVersion;
private String consumerVersion;
private String originalPayload;
private Long pointTimeNs;
}
public static class FailurePublishException extends RuntimeException {
public FailurePublishException(String message, Throwable cause) { super(message, cause); }
}
public static class RetryNotDueException extends RuntimeException {
public RetryNotDueException(String message) { super(message); }
}
}
@@ -0,0 +1,284 @@
package com.Modules.NormalData.cache;
import com.Modules.NormalData.compiled.NormalizationCacheNames;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheConfig;
import com.alicp.jetcache.CacheManager;
import com.alicp.jetcache.CacheResult;
import com.alicp.jetcache.MultiLevelCacheConfig;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.support.Fastjson2KeyConvertor;
import com.alicp.jetcache.support.Kryo5ValueDecoder;
import com.alicp.jetcache.support.Kryo5ValueEncoder;
import com.alicp.jetcache.template.QuickConfig;
import com.common.entity.DeviceCollectTask;
import com.common.entity.DeviceDevice;
import com.config.NormalizationOptimizationProperties;
import com.Modules.NormalData.metrics.NormalizationRuleMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
/** 新 provider:只访问 JetCache BOTH,绝不嵌套 Spring Cache 或 SafeCacheUtil。 */
@Component
public class JetCacheLookupProvider {
private static final Logger log = LoggerFactory.getLogger(JetCacheLookupProvider.class);
private final NormalizationLookupDataLoader loader;
private final NormalizationOptimizationProperties properties;
private final NormalizationRuleMetrics metrics;
private final ConcurrentHashMap<Long, FutureTask<RuleSourceSnapshot>> ruleLoads = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Integer, FutureTask<DeviceDevice>> deviceLoads = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Integer, FutureTask<DeviceCollectTask>> collectTaskLoads =
new ConcurrentHashMap<>();
private final Set<String> loggedCacheFailures = ConcurrentHashMap.newKeySet();
private final Cache<Integer, DeviceDevice> deviceCache;
private final Cache<Integer, DeviceCollectTask> collectTaskCache;
private final Cache<Long, RuleSourceSnapshot> rulesCache;
@Autowired
public JetCacheLookupProvider(NormalizationLookupDataLoader loader,
NormalizationOptimizationProperties properties,
NormalizationRuleMetrics metrics,
CacheManager cacheManager) {
this(loader, properties, metrics,
cacheManager.<Integer, DeviceDevice>getOrCreateCache(quickConfig(
NormalizationCacheNames.DEVICE_BY_ID,
properties.getEnrichmentCache().getRemoteTtlSeconds(),
properties.getEnrichmentCache().getLocalTtlSeconds())),
cacheManager.<Integer, DeviceCollectTask>getOrCreateCache(quickConfig(
NormalizationCacheNames.COLLECT_TASK_BY_ID,
properties.getEnrichmentCache().getRemoteTtlSeconds(),
properties.getEnrichmentCache().getLocalTtlSeconds())),
cacheManager.<Long, RuleSourceSnapshot>getOrCreateCache(quickConfig(
NormalizationCacheNames.RULES_BY_DEVICE,
properties.getRuleSourceCache().getRemoteTtlSeconds(),
properties.getRuleSourceCache().getLocalTtlSeconds())));
}
JetCacheLookupProvider(NormalizationLookupDataLoader loader,
NormalizationOptimizationProperties properties,
NormalizationRuleMetrics metrics,
Cache<Integer, DeviceDevice> deviceCache,
Cache<Integer, DeviceCollectTask> collectTaskCache,
Cache<Long, RuleSourceSnapshot> rulesCache) {
this.loader = loader;
this.properties = properties;
this.metrics = metrics;
this.deviceCache = deviceCache;
this.collectTaskCache = collectTaskCache;
this.rulesCache = rulesCache;
}
static QuickConfig quickConfig(String name, int remoteTtlSeconds, int localTtlSeconds) {
return QuickConfig.newBuilder(name)
.cacheType(CacheType.BOTH)
.expire(Duration.ofSeconds(remoteTtlSeconds))
.localExpire(Duration.ofSeconds(localTtlSeconds))
.localLimit(10_000)
.syncLocal(true)
.penetrationProtect(true)
.keyConvertor(Fastjson2KeyConvertor.INSTANCE)
.valueEncoder(Kryo5ValueEncoder.INSTANCE)
.valueDecoder(Kryo5ValueDecoder.INSTANCE)
.build();
}
public DeviceDevice device(Integer id) {
DeviceDevice cached = safeGet(deviceCache, id, "device");
if (cached != null) return cached;
FutureTask<DeviceDevice> candidate = new FutureTask<>(() -> loader.loadDevice(id));
FutureTask<DeviceDevice> task = deviceLoads.putIfAbsent(id, candidate);
if (task == null) {
task = candidate;
metrics.enrichment("device", "load");
} else {
metrics.enrichment("device", "hit");
}
return awaitAndCache(task, deviceLoads, id, deviceCache, "device");
}
public DeviceCollectTask collectTask(Integer id) {
DeviceCollectTask cached = safeGet(collectTaskCache, id, "collect_task");
if (cached != null) return cached;
FutureTask<DeviceCollectTask> candidate = new FutureTask<>(() -> loader.loadCollectTask(id));
FutureTask<DeviceCollectTask> task = collectTaskLoads.putIfAbsent(id, candidate);
if (task == null) {
task = candidate;
metrics.enrichment("collect_task", "load");
} else {
metrics.enrichment("collect_task", "hit");
}
return awaitAndCache(task, collectTaskLoads, id, collectTaskCache, "collect_task");
}
/** 动态 put 让空快照只存 10 秒;FutureTask 保证本 JVM 同 key 并发只访问一次达梦。 */
public RuleSourceSnapshot rules(Long deviceId) {
RuleSourceSnapshot cached = cachedRuleSnapshot(deviceId);
if (cached != null) {
return cached;
}
FutureTask<RuleSourceSnapshot> candidate = new FutureTask<>(() -> loader.loadRules(deviceId));
FutureTask<RuleSourceSnapshot> task = ruleLoads.putIfAbsent(deviceId, candidate);
if (task == null) {
task = candidate;
metrics.ruleSource("load");
} else {
metrics.ruleSource("suppressed");
}
task.run();
try {
RuleSourceSnapshot loaded = task.get();
int remoteTtl = loaded.isEmpty()
? properties.getRuleSourceCache().getEmptyTtlSeconds()
: properties.getRuleSourceCache().getRemoteTtlSeconds();
try {
putRuleSnapshot(deviceId, loaded, remoteTtl);
} catch (RuntimeException cacheFailure) {
metrics.ruleSource("failure");
logCacheFailure("rule-source", "put", cacheFailure);
}
return loaded;
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw new IllegalStateException("加载设备规则快照被中断: " + deviceId, interrupted);
} catch (ExecutionException failure) {
metrics.ruleSource("failure");
throw new IllegalStateException("加载设备规则快照失败: " + deviceId, failure.getCause());
} finally {
ruleLoads.remove(deviceId, task);
}
}
@SuppressWarnings("unchecked")
private RuleSourceSnapshot cachedRuleSnapshot(Long deviceId) {
try {
List<Cache<Long, RuleSourceSnapshot>> layers = cacheLayers(rulesCache);
if (layers.size() >= 2) {
RuleSourceSnapshot local = layers.get(0).get(deviceId);
if (local != null) {
metrics.ruleSource("l1_hit");
return local;
}
}
RuleSourceSnapshot value = rulesCache.get(deviceId);
if (value != null) metrics.ruleSource("l2_hit");
return value;
} catch (RuntimeException cacheFailure) {
metrics.ruleSource("failure");
logCacheFailure("rule-source", "get", cacheFailure);
return null;
}
}
private void putRuleSnapshot(Long deviceId, RuleSourceSnapshot loaded, int remoteTtl) {
List<Cache<Long, RuleSourceSnapshot>> layers = cacheLayers(rulesCache);
if (layers.size() < 2) {
putChecked(rulesCache, deviceId, loaded, remoteTtl);
return;
}
int localTtl = loaded.isEmpty()
? Math.min(properties.getRuleSourceCache().getEmptyTtlSeconds(),
properties.getRuleSourceCache().getLocalTtlSeconds())
: properties.getRuleSourceCache().getLocalTtlSeconds();
putChecked(layers.get(0), deviceId, loaded, localTtl);
putChecked(layers.get(1), deviceId, loaded, remoteTtl);
}
private <T> T safeGet(Cache<Integer, T> cache, Integer id, String entity) {
try {
T value = cache.get(id);
if (value != null) metrics.enrichment(entity, "hit");
return value;
} catch (RuntimeException cacheFailure) {
metrics.enrichment(entity, "failure");
logCacheFailure(entity, "get", cacheFailure);
return null;
}
}
private <T> T awaitAndCache(FutureTask<T> task,
ConcurrentHashMap<Integer, FutureTask<T>> flights,
Integer id, Cache<Integer, T> cache, String entity) {
task.run();
try {
T value = task.get();
if (value != null) putEnrichment(cache, id, value, entity);
return value;
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
metrics.enrichment(entity, "failure");
throw new IllegalStateException(entity + " 缓存加载被中断: " + id, interrupted);
} catch (ExecutionException failure) {
metrics.enrichment(entity, "failure");
throw new IllegalStateException(entity + " 缓存加载失败: " + id, failure.getCause());
} finally {
flights.remove(id, task);
}
}
private <T> void putEnrichment(Cache<Integer, T> cache, Integer id, T value, String entity) {
try {
List<Cache<Integer, T>> layers = cacheLayers(cache);
int localTtl = properties.getEnrichmentCache().getLocalTtlSeconds();
int remoteTtl = properties.getEnrichmentCache().getRemoteTtlSeconds();
if (layers.size() >= 2) {
putChecked(layers.get(0), id, value, localTtl);
putChecked(layers.get(1), id, value, remoteTtl);
} else {
putChecked(cache, id, value, remoteTtl);
}
} catch (RuntimeException cacheFailure) {
// 缓存故障降级为已成功的 mapper 事实;下次调用可以重试缓存。
metrics.enrichment(entity, "failure");
logCacheFailure(entity, "put", cacheFailure);
}
}
/**
* JetCache 2.7.8 的 MultiLevelCache.unwrap(MultiLevelCache.class) 会遍历子缓存,
* 不会返回 MultiLevelCache 自身,因此必须从配置中取得分层缓存。
*/
@SuppressWarnings("unchecked")
static <K, V> List<Cache<K, V>> cacheLayers(Cache<K, V> cache) {
if (cache == null) return Collections.emptyList();
CacheConfig<K, V> config = cache.config();
if (config instanceof MultiLevelCacheConfig) {
List<Cache<K, V>> layers = ((MultiLevelCacheConfig<K, V>) config).getCaches();
if (layers != null && !layers.isEmpty()) return layers;
}
return Collections.singletonList(cache);
}
private static <K, V> void putChecked(Cache<K, V> cache, K key, V value, int ttlSeconds) {
CacheResult result = cache.PUT(key, value, ttlSeconds, TimeUnit.SECONDS);
if (!result.isSuccess()) {
throw new IllegalStateException("JetCache PUT 失败: code=" + result.getResultCode()
+ ", reason=" + safeReason(result.getMessage()));
}
}
private void logCacheFailure(String cache, String operation, RuntimeException failure) {
String key = cache + ':' + operation;
if (loggedCacheFailures.add(key)) {
log.warn("JetCache 访问失败,已降级数据库: cache={}, operation={}, error={}, reason={}",
cache, operation, failure.getClass().getSimpleName(), safeReason(failure.getMessage()));
}
}
private static String safeReason(String reason) {
if (reason == null || reason.trim().isEmpty()) return "unknown";
String oneLine = reason.replace('\r', ' ').replace('\n', ' ').trim();
return oneLine.length() <= 240 ? oneLine : oneLine.substring(0, 240);
}
}
@@ -0,0 +1,31 @@
package com.Modules.NormalData.cache;
import com.common.entity.DeviceCollectTask;
import com.common.entity.DeviceDevice;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
/** 回滚 provider:每个查询只经过既有 Spring Redis Cache 一层。 */
@Component
public class LegacySpringCacheLookupProvider {
private final NormalizationLookupDataLoader loader;
public LegacySpringCacheLookupProvider(NormalizationLookupDataLoader loader) {
this.loader = loader;
}
@Cacheable(cacheNames = "device", key = "'device:id:' + #id", sync = true)
public DeviceDevice device(Integer id) {
return loader.loadDevice(id);
}
@Cacheable(cacheNames = "collectTask", key = "'id:' + #id", sync = true)
public DeviceCollectTask collectTask(Integer id) {
return loader.loadCollectTask(id);
}
@Cacheable(cacheNames = "normalizeRule", key = "'deviceId:' + #deviceId", sync = true)
public RuleSourceSnapshot rules(Long deviceId) {
return loader.loadRules(deviceId);
}
}
@@ -0,0 +1,70 @@
package com.Modules.NormalData.cache;
import com.Modules.NormalData.compiled.NormalizationCacheInvalidator;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
import com.Modules.NormalData.metrics.NormalizationRuleMetrics;
import java.util.Collection;
/** 同时覆盖当前活动 provider 的统一精确失效入口;单级失败不覆盖数据库提交结果。 */
@Component
public class NormalizationCacheInvalidationService {
private final CacheManager springCacheManager;
private final NormalizationCacheInvalidator jetCacheInvalidator;
private final NormalizationRuleMetrics metrics;
public NormalizationCacheInvalidationService(CacheManager springCacheManager,
NormalizationCacheInvalidator jetCacheInvalidator,
NormalizationRuleMetrics metrics) {
this.springCacheManager = springCacheManager;
this.jetCacheInvalidator = jetCacheInvalidator;
this.metrics = metrics;
}
public void invalidateDevice(Integer id) {
evictSpring("device", "device:id:" + id);
metrics.invalidation("local");
jetCacheInvalidator.device(id);
metrics.invalidation("remote");
}
public void invalidateCollectTask(Integer id) {
evictSpring("collectTask", "id:" + id);
metrics.invalidation("local");
jetCacheInvalidator.collectTask(id);
metrics.invalidation("remote");
}
public void invalidateCollectTaskViews(Integer... deviceIds) {
evictSpring("collectTask", "all");
if (deviceIds == null) return;
for (Integer deviceId : deviceIds) {
if (deviceId != null) evictSpring("collectTask", "device:" + deviceId);
}
}
public void invalidateRulesForDevice(Long deviceId) {
evictSpring("normalizeRule", "deviceId:" + deviceId);
metrics.invalidation("local");
jetCacheInvalidator.rulesForDevice(deviceId);
metrics.invalidation("remote");
}
public void invalidateRuleEntity(Long ruleId) {
evictSpring("normalizeRule", "id:" + ruleId);
}
public void invalidateRulesForDevices(Collection<Long> deviceIds) {
if (deviceIds == null) return;
for (Long deviceId : deviceIds) {
if (deviceId != null) invalidateRulesForDevice(deviceId);
}
}
private void evictSpring(String cacheName, Object key) {
Cache cache = springCacheManager.getCache(cacheName);
if (cache != null) cache.evict(key);
}
}
@@ -0,0 +1,73 @@
package com.Modules.NormalData.cache;
import com.Modules.NormalData.compiled.RuleSource;
import com.common.entity.DeviceCollectTask;
import com.common.entity.DeviceDevice;
import com.common.mapper.DeviceCollectTaskMapper;
import com.common.mapper.DeviceDeviceMapper;
import com.common.mapper.DmNormalizeRuleMapper;
import org.springframework.stereotype.Component;
import java.sql.Clob;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** 三个 provider 共用的无缓存数据加载器,保证 CLOB 在 mapper 返回后立即脱离 JDBC 生命周期。 */
@Component
public class NormalizationLookupDataLoader {
private final DeviceDeviceMapper deviceMapper;
private final DeviceCollectTaskMapper collectTaskMapper;
private final DmNormalizeRuleMapper ruleMapper;
public NormalizationLookupDataLoader(DeviceDeviceMapper deviceMapper,
DeviceCollectTaskMapper collectTaskMapper,
DmNormalizeRuleMapper ruleMapper) {
this.deviceMapper = deviceMapper;
this.collectTaskMapper = collectTaskMapper;
this.ruleMapper = ruleMapper;
}
public DeviceDevice loadDevice(Integer id) {
return deviceMapper.selectById(id);
}
public DeviceCollectTask loadCollectTask(Integer id) {
return collectTaskMapper.selectById(id);
}
public RuleSourceSnapshot loadRules(Long deviceId) {
List<Map<String, Object>> rows = ruleMapper.selectByDeviceId(deviceId);
if (rows == null || rows.isEmpty()) {
return new RuleSourceSnapshot(deviceId, Collections.emptyList(), Instant.now());
}
List<RuleSource> rules = new ArrayList<>(rows.size());
for (Map<String, Object> row : rows) {
if (row == null) continue;
Map<String, Object> detached = new LinkedHashMap<>(row.size());
for (Map.Entry<String, Object> entry : row.entrySet()) {
detached.put(entry.getKey(), detach(entry.getValue()));
}
rules.add(RuleSource.fromMap(detached));
}
return new RuleSourceSnapshot(deviceId, rules, Instant.now());
}
private static Object detach(Object value) {
if (value == null) return null;
if (value instanceof Clob) {
try {
Clob clob = (Clob) value;
long length = clob.length();
return length == 0 ? "" : clob.getSubString(1, Math.toIntExact(length));
} catch (Exception failure) {
throw new IllegalStateException("规则 CLOB 转 String 失败", failure);
}
}
if (value.getClass().getName().startsWith("dm.jdbc.")) return value.toString();
return value;
}
}
@@ -0,0 +1,35 @@
package com.Modules.NormalData.cache;
import com.common.entity.DeviceCollectTask;
import com.common.entity.DeviceDevice;
import com.config.NormalizationOptimizationProperties;
import org.springframework.stereotype.Component;
/** 唯一热查询路由;一次调用严格只进入一个 provider。 */
@Component
public class NormalizationLookupFacade {
private final LegacySpringCacheLookupProvider legacy;
private final JetCacheLookupProvider jetCache;
private final NormalizationOptimizationProperties properties;
public NormalizationLookupFacade(LegacySpringCacheLookupProvider legacy,
JetCacheLookupProvider jetCache,
NormalizationOptimizationProperties properties) {
this.legacy = legacy;
this.jetCache = jetCache;
this.properties = properties;
}
public DeviceDevice device(Integer id) {
return properties.isJetcacheEnabled() ? jetCache.device(id) : legacy.device(id);
}
public DeviceCollectTask collectTask(Integer id) {
return properties.isJetcacheEnabled() ? jetCache.collectTask(id) : legacy.collectTask(id);
}
public RuleSourceSnapshot rules(Long deviceId) {
return properties.isJetcacheEnabled() && properties.getRuleSourceCache().isEnabled()
? jetCache.rules(deviceId) : legacy.rules(deviceId);
}
}
@@ -0,0 +1,56 @@
package com.Modules.NormalData.cache;
import com.Modules.NormalData.compiled.CompiledRuleCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
import com.Modules.NormalData.metrics.NormalizationRuleMetrics;
/** 只有数据库事务真正提交后才清除 raw/compiled 缓存并广播 L1 失效。 */
@Component
public class NormalizeRuleCacheInvalidationListener {
private static final Logger log = LoggerFactory.getLogger(NormalizeRuleCacheInvalidationListener.class);
private final NormalizationCacheInvalidationService invalidationService;
private final CompiledRuleCache compiledRuleCache;
private final NormalizationRuleMetrics metrics;
public NormalizeRuleCacheInvalidationListener(NormalizationCacheInvalidationService invalidationService,
CompiledRuleCache compiledRuleCache,
NormalizationRuleMetrics metrics) {
this.invalidationService = invalidationService;
this.compiledRuleCache = compiledRuleCache;
this.metrics = metrics;
}
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void afterCommit(NormalizeRuleChangedEvent event) {
metrics.invalidation("after_commit");
// 先移除跨 JVM 的规则事实,再清除本 JVM 所有历史 fingerprint 编译对象。
for (Long deviceId : event.getDeviceIds()) {
try {
invalidationService.invalidateRulesForDevice(deviceId);
} catch (RuntimeException cacheFailure) {
metrics.invalidation("failure");
log.error("规则提交后 raw cache 失效失败: ruleId={}, deviceId={}, operation={}",
event.getRuleId(), deviceId, event.getOperation(), cacheFailure);
}
}
try {
compiledRuleCache.invalidateRule(event.getRuleId());
} catch (RuntimeException cacheFailure) {
metrics.invalidation("failure");
// 数据库事实已提交;raw rules 的短 TTL 是最终一致性兜底。
log.error("规则提交后 compiled cache 失效失败: ruleId={}, operation={}",
event.getRuleId(), event.getOperation(), cacheFailure);
}
try {
invalidationService.invalidateRuleEntity(event.getRuleId());
} catch (RuntimeException cacheFailure) {
metrics.invalidation("failure");
log.error("规则提交后实体缓存失效失败: ruleId={}", event.getRuleId(), cacheFailure);
}
}
}
@@ -0,0 +1,23 @@
package com.Modules.NormalData.cache;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** 事务内发布的低敏领域事件,不携带规则正文。 */
public final class NormalizeRuleChangedEvent {
private final long ruleId;
private final List<Long> deviceIds;
private final String operation;
public NormalizeRuleChangedEvent(long ruleId, List<Long> deviceIds, String operation) {
this.ruleId = ruleId;
this.deviceIds = deviceIds == null ? Collections.emptyList()
: Collections.unmodifiableList(new ArrayList<>(deviceIds));
this.operation = operation;
}
public long getRuleId() { return ruleId; }
public List<Long> getDeviceIds() { return deviceIds; }
public String getOperation() { return operation; }
}
@@ -0,0 +1,34 @@
package com.Modules.NormalData.cache;
import com.Modules.NormalData.compiled.RuleSource;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** 可安全跨 JVM 缓存的设备规则事实快照,不持有 JDBC/MyBatis 对象。 */
public final class RuleSourceSnapshot implements Serializable {
private static final long serialVersionUID = 1L;
private final long deviceId;
private final List<RuleSource> rules;
private final Instant loadedAt;
@SuppressWarnings("unused")
private RuleSourceSnapshot() {
this(0L, Collections.emptyList(), Instant.EPOCH);
}
public RuleSourceSnapshot(long deviceId, List<RuleSource> rules, Instant loadedAt) {
this.deviceId = deviceId;
this.rules = new ArrayList<>(rules);
this.loadedAt = loadedAt;
}
public long getDeviceId() { return deviceId; }
public List<RuleSource> getRules() { return Collections.unmodifiableList(rules); }
public Instant getLoadedAt() { return loadedAt; }
public boolean isEmpty() { return rules.isEmpty(); }
}
@@ -0,0 +1,107 @@
package com.Modules.NormalData.compiled;
import com.common.entity.DeviceCollectTask;
import com.common.entity.DeviceDevice;
import com.common.service.DeviceCollectTaskService;
import com.common.service.DeviceDeviceService;
import com.Modules.NormalData.metrics.NormalizationRuleMetrics;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* 批次内按 ID single-flight。FutureTask 允许缓存 null(通过 Optional)并把 loader 异常传播到 ACK 边界。
*/
public final class BatchEnrichmentContext {
private final boolean enabled;
private final DeviceCollectTaskService collectTaskService;
private final DeviceDeviceService deviceService;
private final NormalizationRuleMetrics metrics;
private final ConcurrentHashMap<Integer, FutureTask<Optional<DeviceSnapshot>>> devices =
new ConcurrentHashMap<>();
private final ConcurrentHashMap<Integer, FutureTask<Optional<CollectTaskSnapshot>>> collectTasks =
new ConcurrentHashMap<>();
public BatchEnrichmentContext(boolean enabled, DeviceCollectTaskService collectTaskService,
DeviceDeviceService deviceService) {
this(enabled, collectTaskService, deviceService, null);
}
public BatchEnrichmentContext(boolean enabled, DeviceCollectTaskService collectTaskService,
DeviceDeviceService deviceService, NormalizationRuleMetrics metrics) {
this.enabled = enabled;
this.collectTaskService = collectTaskService;
this.deviceService = deviceService;
this.metrics = metrics;
}
public DeviceSnapshot device(int id) {
if (!enabled) return loadDevice(id);
FutureTask<Optional<DeviceSnapshot>> candidate = new FutureTask<>(() ->
Optional.ofNullable(loadDevice(id)));
FutureTask<Optional<DeviceSnapshot>> task = devices.putIfAbsent(id, candidate);
if (task == null) {
task = candidate;
metric("device", "load");
} else {
metric("device", "hit");
}
return await(task, "device", id).orElse(null);
}
public CollectTaskSnapshot collectTask(int id) {
if (!enabled) return loadCollectTask(id);
FutureTask<Optional<CollectTaskSnapshot>> candidate = new FutureTask<>(() ->
Optional.ofNullable(loadCollectTask(id)));
FutureTask<Optional<CollectTaskSnapshot>> task = collectTasks.putIfAbsent(id, candidate);
if (task == null) {
task = candidate;
metric("collect_task", "load");
} else {
metric("collect_task", "hit");
}
return await(task, "collectTask", id).orElse(null);
}
private DeviceSnapshot loadDevice(int id) {
try {
DeviceDevice loaded = deviceService.getByIdSafely(id);
return DeviceSnapshot.from(loaded);
} catch (RuntimeException failure) {
metric("device", "failure");
throw failure;
}
}
private CollectTaskSnapshot loadCollectTask(int id) {
try {
DeviceCollectTask loaded = collectTaskService.getById(id);
return CollectTaskSnapshot.from(loaded);
} catch (RuntimeException failure) {
metric("collect_task", "failure");
throw failure;
}
}
private void metric(String entity, String outcome) {
if (metrics != null) metrics.enrichment(entity, outcome);
}
private static <T> Optional<T> await(FutureTask<Optional<T>> task, String entity, int id) {
task.run();
try {
return task.get();
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw new IllegalStateException(entity + " 批次补全被中断: " + id, interrupted);
} catch (ExecutionException failure) {
throw new IllegalStateException(entity + " 批次补全加载失败: " + id, failure.getCause());
}
}
public boolean isEnabled() { return enabled; }
public int deviceUniqueCount() { return devices.size(); }
public int collectTaskUniqueCount() { return collectTasks.size(); }
}
@@ -0,0 +1,30 @@
package com.Modules.NormalData.compiled;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** 一个 Kafka poll 固定使用的原始规则快照。 */
public final class BatchRuleSnapshot {
private final Map<Long, List<RuleSource>> byDevice;
private final long capturedAtMillis;
public BatchRuleSnapshot(Map<Long, List<RuleSource>> byDevice, long capturedAtMillis) {
Map<Long, List<RuleSource>> copy = new LinkedHashMap<>();
for (Map.Entry<Long, List<RuleSource>> entry : byDevice.entrySet()) {
copy.put(entry.getKey(), Collections.unmodifiableList(new ArrayList<>(entry.getValue())));
}
this.byDevice = Collections.unmodifiableMap(copy);
this.capturedAtMillis = capturedAtMillis;
}
public List<RuleSource> rulesFor(long deviceId) {
List<RuleSource> rules = byDevice.get(deviceId);
return rules == null ? Collections.emptyList() : rules;
}
public long getCapturedAtMillis() { return capturedAtMillis; }
public int getDeviceCount() { return byDevice.size(); }
}
@@ -0,0 +1,21 @@
package com.Modules.NormalData.compiled;
import com.common.entity.DeviceCollectTask;
/** 批次内不可变的采集任务补全数据。 */
public final class CollectTaskSnapshot {
private final String deviceIp;
private final String taskName;
private CollectTaskSnapshot(String deviceIp, String taskName) {
this.deviceIp = deviceIp;
this.taskName = taskName;
}
public static CollectTaskSnapshot from(DeviceCollectTask task) {
return task == null ? null : new CollectTaskSnapshot(task.getDeviceIp(), task.getTaskName());
}
public String getDeviceIp() { return deviceIp; }
public String getTaskName() { return taskName; }
}
@@ -0,0 +1,62 @@
package com.Modules.NormalData.compiled;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public final class CompiledCompletion {
private final String columnName;
private final Object value;
private final boolean orRelation;
private final List<CompiledPredicate> conditions;
public CompiledCompletion(String columnName, Object value, boolean orRelation,
List<CompiledPredicate> conditions) {
this.columnName = columnName;
this.value = value;
this.orRelation = orRelation;
this.conditions = Collections.unmodifiableList(new ArrayList<>(conditions));
}
public boolean apply(Map<String, Object> fields) {
if (fields.containsKey(columnName) && fields.get(columnName) != null) return false;
boolean matches;
if (conditions.isEmpty()) {
matches = true;
} else if (orRelation) {
matches = false;
for (CompiledPredicate condition : conditions) {
if (condition.evaluate(fields.get(condition.getColumnName()), true)) {
matches = true;
break;
}
}
} else {
matches = true;
for (CompiledPredicate condition : conditions) {
if (!condition.evaluate(fields.get(condition.getColumnName()), true)) {
matches = false;
break;
}
}
}
if (matches) fields.put(columnName, value);
return matches;
}
public String getColumnName() { return columnName; }
public Object getValue() { return value; }
public boolean isOrRelation() { return orRelation; }
public List<CompiledPredicate> getConditions() { return conditions; }
@Override
public String toString() {
return "CompiledCompletion{" +
"columnName='" + columnName + '\'' +
", value=" + value +
", orRelation=" + orRelation +
", conditions=" + conditions +
'}';
}
}
@@ -0,0 +1,42 @@
package com.Modules.NormalData.compiled;
import com.common.entity.RuleContent.Cropper_paramsType;
/** 不可变的正文截取配置。 */
public final class CompiledCropper {
private final String headKey;
private final int headOffset;
private final String tailKey;
private final int tailOffset;
public CompiledCropper(String headKey, int headOffset, String tailKey, int tailOffset) {
this.headKey = headKey;
this.headOffset = headOffset;
this.tailKey = tailKey;
this.tailOffset = tailOffset;
}
public Cropper_paramsType toLegacyParams() {
Cropper_paramsType params = new Cropper_paramsType();
params.sethead_key(headKey);
params.sethead_offset(headOffset);
params.settail_key(tailKey);
params.settail_offset(tailOffset);
return params;
}
public String getHeadKey() { return headKey; }
public int getHeadOffset() { return headOffset; }
public String getTailKey() { return tailKey; }
public int getTailOffset() { return tailOffset; }
@Override
public String toString() {
return "CompiledCropper{" +
"headKey='" + headKey + '\'' +
", headOffset=" + headOffset +
", tailKey='" + tailKey + '\'' +
", tailOffset=" + tailOffset +
'}';
}
}
@@ -0,0 +1,41 @@
package com.Modules.NormalData.compiled;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public final class CompiledFilterGroup {
private final boolean orRelation;
private final List<CompiledPredicate> predicates;
public CompiledFilterGroup(boolean orRelation, List<CompiledPredicate> predicates) {
this.orRelation = orRelation;
this.predicates = Collections.unmodifiableList(new ArrayList<>(predicates));
}
public boolean matches(Map<String, Object> fields) {
if (predicates.isEmpty()) return false;
if (orRelation) {
for (CompiledPredicate predicate : predicates) {
if (predicate.evaluate(fields.get(predicate.getColumnName()), false)) return true;
}
return false;
}
for (CompiledPredicate predicate : predicates) {
if (!predicate.evaluate(fields.get(predicate.getColumnName()), false)) return false;
}
return true;
}
public boolean isOrRelation() { return orRelation; }
public List<CompiledPredicate> getPredicates() { return predicates; }
@Override
public String toString() {
return "CompiledFilterGroup{" +
"orRelation=" + orRelation +
", predicates=" + predicates +
'}';
}
}
@@ -0,0 +1,74 @@
package com.Modules.NormalData.compiled;
import com.common.util.TimeConversionUtils;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/** 单个字段映射的不可变执行计划。 */
public final class CompiledMapper {
public enum Action { EQUAL, MAPPING, TIME, LEGACY_NULL }
private final String originField;
private final String destinationField;
private final Action action;
private final Map<String, Object> mapping;
private final String timezone;
public CompiledMapper(String originField, String destinationField, Action action,
Map<String, Object> mapping, String timezone) {
this.originField = originField;
this.destinationField = destinationField;
this.action = action;
this.mapping = mapping == null ? Collections.emptyMap()
: Collections.unmodifiableMap(new LinkedHashMap<>(mapping));
this.timezone = timezone;
}
public Object map(Object sourceValue) {
switch (action) {
case EQUAL:
return sanitize(sourceValue);
case MAPPING:
// 保持旧执行器语义:源值为 null 时规则执行失败,而不是静默映射成字符串 "null"。
return sanitize(mapping.get(sourceValue.toString()));
case TIME:
try {
return TimeConversionUtils.convertToMillis(sourceValue.toString(), timezone);
} catch (Exception conversionFailure) {
// 旧执行器在时间转换失败时仍写入目标字段,只是字段值为 null。
return null;
}
case LEGACY_NULL:
// 历史规则 action.type 为空时,旧执行器仍生成目标字段,但字段值保持 null。
return null;
default:
throw new IllegalStateException("不支持的映射动作: " + action);
}
}
private static Object sanitize(Object value) {
if (value instanceof String && ((String) value).contains("\u0000")) {
return ((String) value).replace("\u0000", "");
}
return value;
}
public String getOriginField() { return originField; }
public String getDestinationField() { return destinationField; }
public Action getAction() { return action; }
public Map<String, Object> getMapping() { return mapping; }
public String getTimezone() { return timezone; }
@Override
public String toString() {
return "CompiledMapper{" +
"originField='" + originField + '\'' +
", destinationField='" + destinationField + '\'' +
", action=" + action +
", mapping=" + mapping +
", timezone='" + timezone + '\'' +
'}';
}
}
@@ -0,0 +1,71 @@
package com.Modules.NormalData.compiled;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
/** 过滤和补全共用的预编译条件。 */
public final class CompiledPredicate {
public enum Operation { EQUAL, NOT_EQUAL, IN, ZERO_LEN, CONTAIN, REGEXP }
private final String columnName;
private final Operation operation;
private final Object value;
private final List<Object> values;
private final Pattern pattern;
public CompiledPredicate(String columnName, Operation operation, Object value,
List<Object> values, Pattern pattern) {
this.columnName = columnName;
this.operation = operation;
this.value = value;
this.values = values == null ? Collections.emptyList()
: Collections.unmodifiableList(new ArrayList<>(values));
this.pattern = pattern;
}
public boolean evaluate(Object fieldValue, boolean completionSemantics) {
String actual = fieldValue == null ? "" : fieldValue.toString();
switch (operation) {
case EQUAL:
return value != null && actual.equals(value.toString());
case NOT_EQUAL:
// 两个旧服务对 null 的唯一差别:补全 != null 为 true,过滤为 false。
return value == null ? completionSemantics : !actual.equals(value.toString());
case IN:
for (Object candidate : values) {
if (candidate != null && actual.equals(candidate.toString())) return true;
}
return false;
case ZERO_LEN:
return value != null
&& (!StringUtils.hasText(actual) == Boolean.parseBoolean(value.toString()));
case CONTAIN:
return value != null && actual.contains(value.toString());
case REGEXP:
return pattern != null && pattern.matcher(actual).matches();
default:
throw new IllegalStateException("不支持的条件操作: " + operation);
}
}
public String getColumnName() { return columnName; }
public Operation getOperation() { return operation; }
public Object getValue() { return value; }
public List<Object> getValues() { return values; }
public Pattern getPattern() { return pattern; }
@Override
public String toString() {
return "CompiledPredicate{" +
"columnName='" + columnName + '\'' +
", operation=" + operation +
", value=" + value +
", values=" + values +
", pattern=" + (pattern == null ? "null" : "'" + pattern.pattern() + "'") +
'}';
}
}
@@ -0,0 +1,232 @@
package com.Modules.NormalData.compiled;
import com.Modules.NormalData.logNormalData;
import com.common.util.NestedJsonParserUtil;
import com.common.util.NestedJsonUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 规则文本的一次性编译结果。该对象及其集合均不可变;消息动态数据永远不写入本对象。
*/
public final class CompiledRule {
private final long ruleId;
private final String ruleName;
private final String displayName;
private final String dataType;
private final String fingerprint;
private final CompiledCropper cropper;
private final Map<String, List<CompiledMapper>> mapperByOriginField;
private final List<CompiledFilterGroup> filters;
private final List<CompiledCompletion> completions;
private final MessageParserSignature parserSignature;
private final String kvLeftTrim;
private final String kvRightTrim;
private final Pattern kvInternalPattern;
private final Pattern kvExternalPattern;
private final Pattern kvLeftTrimPattern;
private final Pattern kvRightTrimPattern;
private final Pattern separatorPattern;
private final Pattern messageRegexPattern;
public CompiledRule(long ruleId, String ruleName, String displayName, String dataType,
String fingerprint, CompiledCropper cropper,
Map<String, List<CompiledMapper>> mapperByOriginField,
List<CompiledFilterGroup> filters, List<CompiledCompletion> completions,
MessageParserSignature parserSignature,
String kvInternal, String kvExternal, String kvLeftTrim, String kvRightTrim,
String separator, Pattern messageRegexPattern) {
this.ruleId = ruleId;
this.ruleName = ruleName;
this.displayName = displayName;
this.dataType = dataType;
this.fingerprint = fingerprint;
this.cropper = cropper;
Map<String, List<CompiledMapper>> immutableIndex = new LinkedHashMap<>();
for (Map.Entry<String, List<CompiledMapper>> entry : mapperByOriginField.entrySet()) {
immutableIndex.put(entry.getKey(), Collections.unmodifiableList(new ArrayList<>(entry.getValue())));
}
this.mapperByOriginField = Collections.unmodifiableMap(immutableIndex);
this.filters = Collections.unmodifiableList(new ArrayList<>(filters));
this.completions = Collections.unmodifiableList(new ArrayList<>(completions));
this.parserSignature = parserSignature;
this.kvLeftTrim = kvLeftTrim;
this.kvRightTrim = kvRightTrim;
this.kvInternalPattern = kvInternal == null ? null : Pattern.compile(Pattern.quote(kvInternal));
this.kvExternalPattern = kvExternal == null ? null : Pattern.compile(Pattern.quote(kvExternal));
this.kvLeftTrimPattern = empty(kvLeftTrim) ? null
: Pattern.compile("^[" + Pattern.quote(kvLeftTrim) + "]+");
this.kvRightTrimPattern = empty(kvRightTrim) ? null
: Pattern.compile("[" + Pattern.quote(kvRightTrim) + "]+$");
this.separatorPattern = separator == null ? null : Pattern.compile(separator);
this.messageRegexPattern = messageRegexPattern;
}
public ParsedMessage parse(String message) {
String cropped = logNormalData.ParserMessageJsonType(message,
cropper == null ? null : cropper.toLegacyParams());
LinkedHashMap<String, Object> parsed;
switch (dataType) {
case "json":
LinkedHashMap<String, Object> nested = NestedJsonParserUtil.safeParseJson(cropped);
parsed = nested == null ? null : NestedJsonUtils.flattenNestedJson(nested);
break;
case "kv":
parsed = parseKeyValue(cropped);
break;
case "sep":
parsed = parseSeparated(cropped);
break;
case "regex":
parsed = parseRegex(cropped);
break;
case "xml":
parsed = new LinkedHashMap<>();
break;
default:
throw new IllegalStateException("不支持的数据类型: " + dataType);
}
return parsed == null ? null : new ParsedMessage(parsed);
}
public Map<String, Object> map(ParsedMessage message) {
Map<String, Object> destination = new LinkedHashMap<>();
for (Map.Entry<String, Object> source : message.getFields().entrySet()) {
List<CompiledMapper> mappers = mapperByOriginField.get(source.getKey());
if (mappers == null) continue;
for (CompiledMapper mapper : mappers) {
destination.put(mapper.getDestinationField(), mapper.map(source.getValue()));
}
}
return destination;
}
public boolean isFiltered(Map<String, Object> destination) {
for (CompiledFilterGroup group : filters) {
if (group.matches(destination)) return true;
}
return false;
}
public void complete(Map<String, Object> destination) {
for (CompiledCompletion completion : completions) {
completion.apply(destination);
}
}
private LinkedHashMap<String, Object> parseKeyValue(String text) {
LinkedHashMap<String, Object> result = new LinkedHashMap<>();
if (empty(text)) return result;
if (kvExternalPattern == null || kvInternalPattern == null) {
throw new IllegalStateException("KV 规则缺少分隔参数");
}
String[] pairs = kvExternalPattern.split(text, 0);
for (String pair : pairs) {
if (empty(pair)) continue;
String[] parts = kvInternalPattern.split(pair, 2);
if (parts.length == 2) {
String key = trim(parts[0].trim());
String value = removeQuotes(trim(parts[1].trim()));
result.put(key, value);
} else if (parts.length == 1) {
result.put(trim(parts[0].trim()), "");
}
}
return result;
}
private LinkedHashMap<String, Object> parseSeparated(String text) {
LinkedHashMap<String, Object> result = new LinkedHashMap<>();
if (empty(text)) return result;
if (separatorPattern == null || separatorPattern.pattern().isEmpty()) {
result.put("1", text.trim());
return result;
}
String[] values = separatorPattern.split(text, -1);
for (int i = 0; i < values.length; i++) result.put(String.valueOf(i + 1), values[i].trim());
return result;
}
private LinkedHashMap<String, Object> parseRegex(String text) {
LinkedHashMap<String, Object> result = new LinkedHashMap<>();
if (empty(text)) return result;
Matcher matcher = messageRegexPattern.matcher(text);
if (!matcher.find()) return result;
if (matcher.groupCount() == 0) {
result.put("1", matcher.group(0));
} else {
for (int i = 1; i <= matcher.groupCount(); i++) {
String value = matcher.group(i);
result.put(String.valueOf(i), value == null ? "" : value.trim());
}
}
return result;
}
private String trim(String value) {
String result = value;
if (kvLeftTrimPattern != null) result = kvLeftTrimPattern.matcher(result).replaceAll("");
if (kvRightTrimPattern != null) result = kvRightTrimPattern.matcher(result).replaceAll("");
return result;
}
private static String removeQuotes(String value) {
if (value == null || value.length() < 2) return value;
if ((value.startsWith("\"") && value.endsWith("\""))
|| (value.startsWith("'") && value.endsWith("'"))) {
return value.substring(1, value.length() - 1);
}
return value;
}
private static boolean empty(String value) {
return value == null || value.trim().isEmpty();
}
public long getRuleId() { return ruleId; }
public String getRuleName() { return ruleName; }
public String getDisplayName() { return displayName; }
public String getDataType() { return dataType; }
public String getFingerprint() { return fingerprint; }
public CompiledCropper getCropper() { return cropper; }
public Map<String, List<CompiledMapper>> getMapperByOriginField() { return mapperByOriginField; }
public List<CompiledFilterGroup> getFilters() { return filters; }
public List<CompiledCompletion> getCompletions() { return completions; }
public MessageParserSignature getParserSignature() { return parserSignature; }
public String getKvLeftTrim() { return kvLeftTrim; }
public String getKvRightTrim() { return kvRightTrim; }
@Override
public String toString() {
return "CompiledRule{" +
"ruleId=" + ruleId +
", ruleName='" + ruleName + '\'' +
", displayName='" + displayName + '\'' +
", dataType='" + dataType + '\'' +
", fingerprint='" + fingerprint + '\'' +
", cropper=" + cropper +
", mapperByOriginField=" + mapperByOriginField +
", filters=" + filters +
", completions=" + completions +
", parserSignature=" + parserSignature +
", kvLeftTrim='" + kvLeftTrim + '\'' +
", kvRightTrim='" + kvRightTrim + '\'' +
", kvInternalPattern=" + pattern(kvInternalPattern) +
", kvExternalPattern=" + pattern(kvExternalPattern) +
", kvLeftTrimPattern=" + pattern(kvLeftTrimPattern) +
", kvRightTrimPattern=" + pattern(kvRightTrimPattern) +
", separatorPattern=" + pattern(separatorPattern) +
", messageRegexPattern=" + pattern(messageRegexPattern) +
'}';
}
private static String pattern(Pattern value) {
return value == null ? "null" : "'" + value.pattern() + "'";
}
}
@@ -0,0 +1,208 @@
package com.Modules.NormalData.compiled;
import com.config.NormalizationOptimizationProperties;
import com.Modules.NormalData.metrics.NormalizationRuleMetrics;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;
import java.util.Set;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
/** JVM 本地、有界、single-flight 的编译规则缓存。 */
@Component
public class CompiledRuleCache {
private static final Logger log = LoggerFactory.getLogger(CompiledRuleCache.class);
private static final ObjectMapper DIAGNOSTIC_JSON = new ObjectMapper();
private static final List<String> EXECUTION_RULE_FIELDS = Arrays.asList(
"cropper_params", "kv_params", "sep", "regexp", "mappers",
"filters_params", "complete_columns");
private final CompiledRuleCompiler compiler;
private final Cache<String, CompiledRule> compiled;
private final Cache<String, FailureRecord> failures;
private final Cache<String, Boolean> loggedFailures;
private final Cache<Long, Set<String>> keysByRule;
private final NormalizationRuleMetrics metrics;
private final boolean ruleDetailLogEnabled;
private final LongAdder compileSuccess = new LongAdder();
private final LongAdder compileFailure = new LongAdder();
private final LongAdder evictions = new LongAdder();
public CompiledRuleCache(CompiledRuleCompiler compiler,
NormalizationOptimizationProperties properties) {
this(compiler, properties, new NormalizationRuleMetrics());
}
@Autowired
public CompiledRuleCache(CompiledRuleCompiler compiler,
NormalizationOptimizationProperties properties,
NormalizationRuleMetrics metrics) {
this.compiler = compiler;
this.metrics = metrics;
NormalizationOptimizationProperties.CompiledRule config = properties.getCompiledRule();
this.ruleDetailLogEnabled = config.isRuleDetailLogEnabled();
this.keysByRule = Caffeine.newBuilder()
.maximumSize(config.getLocalLimit())
.expireAfterAccess(config.getExpireAfterAccessMinutes(), TimeUnit.MINUTES)
.build();
this.compiled = Caffeine.newBuilder()
.maximumSize(config.getLocalLimit())
.expireAfterAccess(config.getExpireAfterAccessMinutes(), TimeUnit.MINUTES)
.removalListener((key, value, cause) -> {
if (cause.wasEvicted()) evictions.increment();
if (key != null && value != null) unlink(((CompiledRule) value).getRuleId(), key.toString());
})
.build();
this.failures = Caffeine.newBuilder()
.maximumSize(Math.min(config.getLocalLimit(), 1_000))
.expireAfterWrite(config.getFailureCacheSeconds(), TimeUnit.SECONDS)
.build();
this.loggedFailures = Caffeine.newBuilder()
.maximumSize(Math.min(config.getLocalLimit(), 1_000))
.expireAfterAccess(config.getExpireAfterAccessMinutes(), TimeUnit.MINUTES)
.build();
}
public CompiledRule get(RuleSource source, RuleExecutionMetrics metrics) {
String key = compiler.fingerprint(source);
CompiledRule cached = compiled.getIfPresent(key);
if (cached != null) {
if (metrics != null) metrics.compiledHit();
this.metrics.compiledGet("hit");
return cached;
}
if (metrics != null) metrics.compiledMiss();
FailureRecord failed = failures.getIfPresent(key);
if (failed != null) {
this.metrics.compiledGet("failure_hit");
throw failed.toException(source.getId());
}
this.metrics.compiledGet("miss");
// Caffeine Cache.get 对同 key 的并发加载只执行一次,其他线程等待同一结果。
return compiled.get(key, ignored -> {
FailureRecord recentFailure = failures.getIfPresent(key);
if (recentFailure != null) {
this.metrics.compiledGet("failure_hit");
throw recentFailure.toException(source.getId());
}
long started = System.nanoTime();
try {
this.metrics.ruleJsonParse();
CompiledRule value = compiler.compile(source);
keysByRule.asMap().computeIfAbsent(source.getId(), ruleId ->
Collections.newSetFromMap(new ConcurrentHashMap<>())).add(key);
compileSuccess.increment();
this.metrics.compiled("success", System.nanoTime() - started);
this.metrics.compiledCacheGauge(compiled.estimatedSize() + 1L, evictions.sum());
if (ruleDetailLogEnabled) {
log.info("标准化规则文本编译成功: ruleId={}, fingerprint={}, ruleText={}, compiledRule={}",
source.getId(), shortFingerprint(key), diagnosticRuleText(source.getRuleContent()), value);
}
return value;
} catch (RuntimeException failure) {
compileFailure.increment();
this.metrics.compiled("failure", System.nanoTime() - started);
failures.put(key, new FailureRecord(failure));
if (loggedFailures.asMap().putIfAbsent(key, Boolean.TRUE) == null) {
if (ruleDetailLogEnabled) {
log.warn("标准化规则编译失败,将对此规则使用旧执行器: ruleId={}, fingerprint={}, "
+ "error={}, reason={}, ruleText={}", source.getId(), shortFingerprint(key),
failure.getClass().getSimpleName(), safeReason(failure),
diagnosticRuleText(source.getRuleContent()));
} else {
log.warn("标准化规则编译失败,将对此规则使用旧执行器: ruleId={}, fingerprint={}, "
+ "error={}, reason={}", source.getId(), shortFingerprint(key),
failure.getClass().getSimpleName(), safeReason(failure));
}
}
throw failure;
}
});
}
public void invalidateRule(long ruleId) {
Set<String> keys = keysByRule.asMap().remove(ruleId);
if (keys != null) compiled.invalidateAll(keys);
for (String key : failures.asMap().keySet()) {
if (key.startsWith(ruleId + ":")) failures.invalidate(key);
}
for (String key : loggedFailures.asMap().keySet()) {
if (key.startsWith(ruleId + ":")) loggedFailures.invalidate(key);
}
metrics.compiledCacheGauge(compiled.estimatedSize(), evictions.sum());
}
public long estimatedSize() { return compiled.estimatedSize(); }
public long compileSuccessCount() { return compileSuccess.sum(); }
public long compileFailureCount() { return compileFailure.sum(); }
public long evictionCount() { return evictions.sum(); }
private static String shortFingerprint(String key) {
int colon = key.indexOf(':');
int end = Math.min(key.length(), colon < 0 ? 12 : colon + 13);
return key.substring(0, end);
}
private static String safeReason(RuntimeException failure) {
String message = failure.getMessage();
if (message == null || message.trim().isEmpty()) {
return failure.getClass().getSimpleName();
}
String oneLine = message.replace('\r', ' ').replace('\n', ' ').trim();
return oneLine.length() <= 240 ? oneLine : oneLine.substring(0, 240);
}
static String diagnosticRuleText(String ruleText) {
if (ruleText == null) return "null";
try {
JsonNode root = DIAGNOSTIC_JSON.readTree(ruleText);
if (root == null || !root.isObject()) {
return "<非对象规则正文,长度=" + ruleText.length() + ">";
}
ObjectNode diagnostic = DIAGNOSTIC_JSON.createObjectNode();
for (String field : EXECUTION_RULE_FIELDS) {
if (root.has(field)) diagnostic.set(field, root.get(field));
}
return DIAGNOSTIC_JSON.writeValueAsString(diagnostic)
.replace("\r", "\\r").replace("\n", "\\n");
} catch (Exception invalidJson) {
// 无效 JSON 可能仍携带样例事件或凭据,失败时只记录长度,不回显原文。
return "<规则正文无法安全解析,长度=" + ruleText.length() + ">";
}
}
private void unlink(long ruleId, String key) {
Set<String> keys = keysByRule.getIfPresent(ruleId);
if (keys == null) return;
keys.remove(key);
if (keys.isEmpty()) keysByRule.invalidate(ruleId);
}
private static final class FailureRecord {
private final String type;
private final String reason;
private FailureRecord(RuntimeException failure) {
this.type = failure.getClass().getSimpleName();
this.reason = safeReason(failure);
}
private RuntimeException toException(long ruleId) {
return new CompiledRuleCompiler.RuleCompilationException(ruleId,
"规则处于短失败缓存: " + type + ", " + reason);
}
}
}
@@ -0,0 +1,305 @@
package com.Modules.NormalData.compiled;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
/** 将原始规则 JSON 编译成不可变执行计划;本类不访问任何外部系统。 */
@Component
public class CompiledRuleCompiler {
private final ObjectMapper objectMapper;
public CompiledRuleCompiler(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public CompiledRule compile(RuleSource source) {
if (source == null) throw new IllegalArgumentException("规则源不能为空");
if (blank(source.getRuleContent())) throw invalid(source, "rule_content 不能为空");
try {
JsonNode root = objectMapper.readTree(source.getRuleContent());
if (root == null || !root.isObject()) throw invalid(source, "rule_content 必须是 JSON 对象");
String dataType = required(source.getDataType(), source, "data_type").toLowerCase(Locale.ROOT);
if (!("json".equals(dataType) || "kv".equals(dataType) || "sep".equals(dataType)
|| "regex".equals(dataType) || "xml".equals(dataType))) {
throw invalid(source, "未知 data_type: " + dataType);
}
CompiledCropper cropper = compileCropper(root.get("cropper_params"), source);
KvConfig kv = compileKv(root.get("kv_params"), dataType, source);
String separator = text(root.get("sep"));
String regex = text(root.get("regexp"));
Pattern messageRegex = null;
if ("regex".equals(dataType)) {
if (blank(regex)) throw invalid(source, "regex 规则缺少 regexp");
messageRegex = Pattern.compile(regex);
}
if ("sep".equals(dataType) && separator != null && !separator.isEmpty()) {
Pattern.compile(separator);
}
Map<String, List<CompiledMapper>> mappers = compileMappers(root.get("mappers"), source);
List<CompiledFilterGroup> filters = compileFilters(root.get("filters_params"), source);
List<CompiledCompletion> completions = compileCompletions(root.get("complete_columns"), source);
MessageParserSignature signature = new MessageParserSignature(dataType, cropper,
kv.internal, kv.external, kv.leftTrim, kv.rightTrim, separator, regex);
return new CompiledRule(source.getId(), required(source.getName(), source, "name"),
source.getDisplayName(), dataType, fingerprint(source), cropper, mappers,
filters, completions, signature, kv.internal, kv.external, kv.leftTrim,
kv.rightTrim, separator, messageRegex);
} catch (RuleCompilationException known) {
throw known;
} catch (Exception failure) {
throw new RuleCompilationException(source.getId(),
"规则编译失败: " + failure.getClass().getSimpleName(), failure);
}
}
public String fingerprint(RuleSource source) {
return source.getId() + ":" + source.getFingerprint();
}
private CompiledCropper compileCropper(JsonNode node, RuleSource source) {
if (node == null || node.isNull()) return null;
if (!node.isObject()) throw invalid(source, "cropper_params 必须是对象");
return new CompiledCropper(textOrEmpty(node.get("head_key")), integer(node, "head_offset", source),
textOrEmpty(node.get("tail_key")), integer(node, "tail_offset", source));
}
private KvConfig compileKv(JsonNode node, String dataType, RuleSource source) {
if (!"kv".equals(dataType)) {
return node != null && node.isObject()
? new KvConfig(text(node.get("internal")), text(node.get("external")),
text(node.get("l_trim")), text(node.get("r_trim"))) : KvConfig.EMPTY;
}
if (node == null || !node.isObject()) throw invalid(source, "kv 规则缺少 kv_params");
return new KvConfig(requiredSeparator(text(node.get("internal")), source, "kv_params.internal"),
requiredSeparator(text(node.get("external")), source, "kv_params.external"),
textOrEmpty(node.get("l_trim")), textOrEmpty(node.get("r_trim")));
}
private Map<String, List<CompiledMapper>> compileMappers(JsonNode node, RuleSource source) {
node = decodeEmbeddedJson(node, source, "mappers");
if (node == null || !node.isArray()) throw invalid(source, "mappers 必须是数组");
Map<String, List<CompiledMapper>> index = new LinkedHashMap<>();
for (JsonNode mapper : node) {
String origin = required(text(mapper.get("origin_field")), source, "mappers.origin_field");
String destination = required(text(mapper.get("dest_field")), source, "mappers.dest_field");
JsonNode actionNode = mapper.get("action");
if (actionNode == null || !actionNode.isObject()) throw invalid(source, "mapper.action 必须是对象");
String type = text(actionNode.get("type"));
JsonNode param = actionNode.get("param");
CompiledMapper compiled;
if (blank(type)) {
// 旧执行器不会读取 mapper 外层的 type;action.type 为空时仍写入目标字段 null。
compiled = new CompiledMapper(origin, destination,
CompiledMapper.Action.LEGACY_NULL, null, null);
} else if ("equal".equals(type)) {
compiled = new CompiledMapper(origin, destination, CompiledMapper.Action.EQUAL, null, null);
} else if ("mapping".equals(type)) {
JsonNode mappingNode = param == null ? null : param.get("mapping");
if (mappingNode == null || !mappingNode.isObject()) {
throw invalid(source, "mapping action 缺少 param.mapping");
}
Map<String, Object> mapping = new LinkedHashMap<>();
Iterator<Map.Entry<String, JsonNode>> fields = mappingNode.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
mapping.put(entry.getKey(), freeze(objectMapper.convertValue(entry.getValue(), Object.class)));
}
compiled = new CompiledMapper(origin, destination, CompiledMapper.Action.MAPPING, mapping, null);
} else if ("time".equals(type)) {
String timezone = param == null ? null : text(param.get("timezone"));
compiled = new CompiledMapper(origin, destination, CompiledMapper.Action.TIME,
null, required(timezone, source, "time action param.timezone"));
} else {
throw invalid(source, "未知 mapper action: " + type);
}
index.computeIfAbsent(origin, ignored -> new ArrayList<>()).add(compiled);
}
return index;
}
private List<CompiledFilterGroup> compileFilters(JsonNode node, RuleSource source) {
if (node == null || node.isNull()) return Collections.emptyList();
// 旧执行器遇到 []、空字符串等非对象结构时捕获异常并按“无过滤条件”继续。
// 字符串即使内容是 JSON 对象也不会被旧执行器二次解析,必须同样忽略。
// 编译路径必须保持相同语义,不能让历史脏格式阻塞整条消息。
if (!node.isObject()) return Collections.emptyList();
JsonNode groups = node.get("expressions");
if (groups == null || groups.isNull()) return Collections.emptyList();
if (!groups.isArray()) throw invalid(source, "filters_params.expressions 必须是数组");
List<CompiledFilterGroup> result = new ArrayList<>();
for (JsonNode group : groups) {
JsonNode expressions = group.get("expressions");
List<CompiledPredicate> predicates = compilePredicates(expressions, source);
if (!predicates.isEmpty()) {
result.add(new CompiledFilterGroup("or".equalsIgnoreCase(text(group.get("filters_relation"))),
predicates));
}
}
return result;
}
private List<CompiledCompletion> compileCompletions(JsonNode node, RuleSource source) {
if (node == null || node.isNull()) return Collections.emptyList();
// 旧补全服务对 {}、空字符串等非数组结构捕获异常后按“无补全配置”继续。
// 字符串即使内容是 JSON 数组也不会被旧执行器二次解析,必须同样忽略。
if (!node.isArray()) return Collections.emptyList();
List<CompiledCompletion> result = new ArrayList<>();
for (JsonNode completion : node) {
String column = required(text(completion.get("column_name")), source,
"complete_columns.column_name");
Object value = freeze(objectMapper.convertValue(completion.get("value"), Object.class));
List<CompiledPredicate> conditions = compilePredicates(completion.get("conditions"), source);
result.add(new CompiledCompletion(column, value,
"or".equalsIgnoreCase(text(completion.get("filters_relation"))), conditions));
}
return result;
}
private List<CompiledPredicate> compilePredicates(JsonNode node, RuleSource source) {
if (node == null || node.isNull()) return Collections.emptyList();
if (!node.isArray()) throw invalid(source, "条件 expressions/conditions 必须是数组");
List<CompiledPredicate> result = new ArrayList<>();
for (JsonNode expression : node) {
String column = required(text(expression.get("column_name")), source, "condition.column_name");
String op = required(text(expression.get("op")), source, "condition.op");
Object value = expression.has("value") && !expression.get("value").isNull()
? freeze(objectMapper.convertValue(expression.get("value"), Object.class)) : null;
List<Object> values = value instanceof List ? castList(value) : Collections.emptyList();
Pattern pattern = null;
CompiledPredicate.Operation operation;
switch (op) {
case "=": operation = CompiledPredicate.Operation.EQUAL; break;
case "!=": operation = CompiledPredicate.Operation.NOT_EQUAL; break;
case "in": operation = CompiledPredicate.Operation.IN; break;
case "zero_len": operation = CompiledPredicate.Operation.ZERO_LEN; break;
case "contain": operation = CompiledPredicate.Operation.CONTAIN; break;
case "regexp":
operation = CompiledPredicate.Operation.REGEXP;
if (value != null) pattern = Pattern.compile(value.toString());
break;
default: throw invalid(source, "未知条件操作符: " + op);
}
result.add(new CompiledPredicate(column, operation, value, values, pattern));
}
return result;
}
@SuppressWarnings("unchecked")
private static List<Object> castList(Object value) {
return (List<Object>) value;
}
private static Object freeze(Object value) {
if (value instanceof Map) {
Map<Object, Object> copy = new LinkedHashMap<>();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
copy.put(entry.getKey(), freeze(entry.getValue()));
}
return Collections.unmodifiableMap(copy);
}
if (value instanceof List) {
List<Object> copy = new ArrayList<>();
for (Object item : (List<?>) value) copy.add(freeze(item));
return Collections.unmodifiableList(copy);
}
return value;
}
private static int integer(JsonNode parent, String name, RuleSource source) {
JsonNode value = parent.get(name);
if (value == null) throw invalid(source, name + " 必须是整数");
if (value.isIntegralNumber() && value.canConvertToInt()) return value.intValue();
// 达梦历史规则中 offset 可能以 JSON 字符串保存;旧执行器使用 Integer.parseInt 接受该格式。
if (value.isTextual()) {
try {
return Integer.parseInt(value.textValue().trim());
} catch (NumberFormatException ignored) {
// 统一转换为不包含规则正文的结构化编译错误。
}
}
throw invalid(source, name + " 必须是整数");
}
private static String required(String value, RuleSource source, String name) {
if (blank(value)) throw invalid(source, "缺少必需字段: " + name);
return value;
}
private static String requiredSeparator(String value, RuleSource source, String name) {
// 空格本身是合法且常用的 KV 外部分隔符,不能按 blank 处理。
if (value == null || value.isEmpty()) throw invalid(source, "缺少必需字段: " + name);
return value;
}
private static String text(JsonNode node) {
return node == null || node.isNull() ? null : node.asText();
}
private static String textOrEmpty(JsonNode node) {
String value = text(node);
return value == null ? "" : value;
}
private static boolean blank(String value) {
return value == null || value.trim().isEmpty();
}
private static RuleCompilationException invalid(RuleSource source, String message) {
return new RuleCompilationException(source.getId(), message);
}
private JsonNode decodeEmbeddedJson(JsonNode node, RuleSource source, String name) {
if (node == null || !node.isTextual()) return node;
String text = node.textValue();
if (text == null || text.trim().isEmpty()) return node;
try {
return objectMapper.readTree(text);
} catch (Exception invalidEmbeddedJson) {
throw invalid(source, name + " 内嵌 JSON 无效");
}
}
private static final class KvConfig {
private static final KvConfig EMPTY = new KvConfig(null, null, null, null);
private final String internal;
private final String external;
private final String leftTrim;
private final String rightTrim;
private KvConfig(String internal, String external, String leftTrim, String rightTrim) {
this.internal = internal;
this.external = external;
this.leftTrim = leftTrim;
this.rightTrim = rightTrim;
}
}
public static class RuleCompilationException extends IllegalArgumentException {
private final long ruleId;
public RuleCompilationException(long ruleId, String message) {
super(message);
this.ruleId = ruleId;
}
public RuleCompilationException(long ruleId, String message, Throwable cause) {
super(message, cause);
this.ruleId = ruleId;
}
public long getRuleId() { return ruleId; }
}
}
@@ -0,0 +1,24 @@
package com.Modules.NormalData.compiled;
import com.common.entity.DeviceDevice;
/** 批次内不可变的设备补全数据。 */
public final class DeviceSnapshot {
private final String ip;
private final String vendor;
private final String name;
private DeviceSnapshot(String ip, String vendor, String name) {
this.ip = ip;
this.vendor = vendor;
this.name = name;
}
public static DeviceSnapshot from(DeviceDevice device) {
return device == null ? null : new DeviceSnapshot(device.getIp(), device.getVendor(), device.getName());
}
public String getIp() { return ip; }
public String getVendor() { return vendor; }
public String getName() { return name; }
}
@@ -0,0 +1,45 @@
package com.Modules.NormalData.compiled;
import java.util.HashMap;
import java.util.Map;
import com.Modules.NormalData.metrics.NormalizationRuleMetrics;
/** 单条原始消息内按解析签名复用解析结果。 */
public final class MessageParseContext {
private final boolean enabled;
private final Map<MessageParserSignature, ParsedMessage> parsed = new HashMap<>();
private final NormalizationRuleMetrics globalMetrics;
public MessageParseContext(boolean enabled) {
this(enabled, null);
}
public MessageParseContext(boolean enabled, NormalizationRuleMetrics globalMetrics) {
this.enabled = enabled;
this.globalMetrics = globalMetrics;
}
public ParsedMessage parse(CompiledRule rule, String message, RuleExecutionMetrics metrics) {
long started = System.nanoTime();
try {
if (!enabled) {
if (metrics != null) metrics.parseMiss();
if (globalMetrics != null) globalMetrics.messageParse("miss");
return rule.parse(message);
}
ParsedMessage existing = parsed.get(rule.getParserSignature());
if (existing != null) {
if (metrics != null) metrics.parseHit();
if (globalMetrics != null) globalMetrics.messageParse("hit");
return existing;
}
if (metrics != null) metrics.parseMiss();
if (globalMetrics != null) globalMetrics.messageParse("miss");
ParsedMessage loaded = rule.parse(message);
if (loaded != null) parsed.put(rule.getParserSignature(), loaded);
return loaded;
} finally {
if (metrics != null) metrics.addMessageParseNanos(System.nanoTime() - started);
}
}
}
@@ -0,0 +1,79 @@
package com.Modules.NormalData.compiled;
import java.util.Objects;
/** 只有所有解析参数一致时,同一条消息的解析结果才允许复用。 */
public final class MessageParserSignature {
private static final int FLATTEN_STRATEGY_VERSION = 1;
private final String dataType;
private final String headKey;
private final int headOffset;
private final String tailKey;
private final int tailOffset;
private final String kvInternal;
private final String kvExternal;
private final String kvLeftTrim;
private final String kvRightTrim;
private final String separator;
private final String regex;
public MessageParserSignature(String dataType, CompiledCropper cropper,
String kvInternal, String kvExternal,
String kvLeftTrim, String kvRightTrim,
String separator, String regex) {
this.dataType = dataType;
this.headKey = cropper == null ? null : cropper.getHeadKey();
this.headOffset = cropper == null ? 0 : cropper.getHeadOffset();
this.tailKey = cropper == null ? null : cropper.getTailKey();
this.tailOffset = cropper == null ? 0 : cropper.getTailOffset();
this.kvInternal = kvInternal;
this.kvExternal = kvExternal;
this.kvLeftTrim = kvLeftTrim;
this.kvRightTrim = kvRightTrim;
this.separator = separator;
this.regex = regex;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof MessageParserSignature)) return false;
MessageParserSignature that = (MessageParserSignature) other;
return headOffset == that.headOffset && tailOffset == that.tailOffset
&& Objects.equals(dataType, that.dataType)
&& Objects.equals(headKey, that.headKey)
&& Objects.equals(tailKey, that.tailKey)
&& Objects.equals(kvInternal, that.kvInternal)
&& Objects.equals(kvExternal, that.kvExternal)
&& Objects.equals(kvLeftTrim, that.kvLeftTrim)
&& Objects.equals(kvRightTrim, that.kvRightTrim)
&& Objects.equals(separator, that.separator)
&& Objects.equals(regex, that.regex);
}
@Override
public int hashCode() {
return Objects.hash(dataType, headKey, headOffset, tailKey, tailOffset,
kvInternal, kvExternal, kvLeftTrim, kvRightTrim, separator, regex,
FLATTEN_STRATEGY_VERSION);
}
@Override
public String toString() {
return "MessageParserSignature{" +
"dataType='" + dataType + '\'' +
", headKey='" + headKey + '\'' +
", headOffset=" + headOffset +
", tailKey='" + tailKey + '\'' +
", tailOffset=" + tailOffset +
", kvInternal='" + kvInternal + '\'' +
", kvExternal='" + kvExternal + '\'' +
", kvLeftTrim='" + kvLeftTrim + '\'' +
", kvRightTrim='" + kvRightTrim + '\'' +
", separator='" + separator + '\'' +
", regex='" + regex + '\'' +
", flattenStrategyVersion=" + FLATTEN_STRATEGY_VERSION +
'}';
}
}
@@ -0,0 +1,24 @@
package com.Modules.NormalData.compiled;
import com.alicp.jetcache.anno.CacheInvalidate;
import org.springframework.stereotype.Component;
/** JetCache BOTH 的统一失效入口;syncLocal 会通过独立广播通道清理其他 JVM 的 L1。 */
@Component
public class NormalizationCacheInvalidator {
@CacheInvalidate(name = NormalizationCacheNames.DEVICE_BY_ID, key = "#id")
public void device(Integer id) {
// 失效由 JetCache AOP 完成。
}
@CacheInvalidate(name = NormalizationCacheNames.COLLECT_TASK_BY_ID, key = "#id")
public void collectTask(Integer id) {
// 失效由 JetCache AOP 完成。
}
@CacheInvalidate(name = NormalizationCacheNames.RULES_BY_DEVICE, key = "#deviceId")
public void rulesForDevice(Long deviceId) {
// 失效由 JetCache AOP 完成。
}
}
@@ -0,0 +1,9 @@
package com.Modules.NormalData.compiled;
public final class NormalizationCacheNames {
public static final String DEVICE_BY_ID = "xdr:norm:v1:device:";
public static final String COLLECT_TASK_BY_ID = "xdr:norm:v1:collect-task:";
public static final String RULES_BY_DEVICE = "xdr:norm:v1:rule-source:device:";
private NormalizationCacheNames() {}
}
@@ -0,0 +1,154 @@
package com.Modules.NormalData.compiled;
import com.Modules.NormalData.LogNormalProcessor;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/** 影子结果的确定性深比较器。null、缺失和不同类型不会被折叠。 */
@Component
public class NormalizationShadowComparator {
public Diff compare(LogNormalProcessor.NormalizationResult legacy,
LogNormalProcessor.NormalizationResult candidate) {
if (legacy.getOutcome() != candidate.getOutcome()) {
return Diff.mismatch("outcome", Collections.singleton("$outcome"));
}
if (legacy.getRows().size() != candidate.getRows().size()) {
return Diff.mismatch("row_count", Collections.singleton("$rowCount"));
}
List<Map<String, Object>> leftRows = sortedRows(legacy.getRows());
List<Map<String, Object>> rightRows = sortedRows(candidate.getRows());
Set<String> differing = new TreeSet<>();
String type = "value";
for (int i = 0; i < leftRows.size(); i++) {
Map<String, Object> left = leftRows.get(i);
Map<String, Object> right = rightRows.get(i);
if (!left.keySet().equals(right.keySet())) {
type = "field_set";
Set<String> keys = new TreeSet<>(left.keySet());
keys.addAll(right.keySet());
for (String key : keys) {
if (left.containsKey(key) != right.containsKey(key)) differing.add(key);
}
}
Set<String> common = new TreeSet<>(left.keySet());
common.retainAll(right.keySet());
for (String key : common) {
if (!canonical(left.get(key)).equals(canonical(right.get(key)))) differing.add(key);
}
}
return differing.isEmpty() ? Diff.match() : Diff.mismatch(type, differing);
}
public boolean sampled(String eventId, double rate) {
if (rate <= 0.0d || eventId == null) return false;
if (rate >= 1.0d) return true;
byte[] hash = sha256(eventId);
long value = 0L;
for (int i = 0; i < 8; i++) value = (value << 8) | (hash[i] & 0xffL);
double unit = (double) (value >>> 1) / (double) Long.MAX_VALUE;
return unit < rate;
}
public String eventHash(String eventId) {
StringBuilder value = new StringBuilder(12);
byte[] hash = sha256(eventId == null ? "" : eventId);
for (int i = 0; i < 6; i++) value.append(String.format("%02x", hash[i] & 0xff));
return value.toString();
}
private static List<Map<String, Object>> sortedRows(List<Map<String, Object>> rows) {
List<Map<String, Object>> sorted = new ArrayList<>(rows);
sorted.sort(Comparator.comparing(NormalizationShadowComparator::rowKey));
return sorted;
}
private static String rowKey(Map<String, Object> row) {
Object id = row.get("id");
if (id == null) id = row.get("event_id");
if (id != null) return "0:" + canonical(id);
Object ruleId = row.get("normalize_rule_id");
return "1:" + canonical(ruleId) + ':' + hex(sha256(canonical(row)));
}
private static String canonical(Object value) {
if (value == null) return "N:null";
if (value instanceof Number) {
return "D:" + new BigDecimal(value.toString()).stripTrailingZeros().toPlainString();
}
if (value instanceof LocalDateTime) {
return "L:" + DateTimeFormatter.ISO_LOCAL_DATE_TIME.format((LocalDateTime) value);
}
if (value instanceof Instant) return "I:" + ((Instant) value).toEpochMilli();
if (value instanceof Date) return "I:" + ((Date) value).getTime();
if (value instanceof byte[]) return "B:" + Base64.getEncoder().encodeToString((byte[]) value);
if (value instanceof Map) {
Map<String, Object> sorted = new java.util.TreeMap<>();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
sorted.put(String.valueOf(entry.getKey()), entry.getValue());
}
StringBuilder text = new StringBuilder("M:");
for (Map.Entry<String, Object> entry : sorted.entrySet()) {
text.append(entry.getKey()).append('=').append(canonical(entry.getValue())).append(';');
}
return text.toString();
}
if (value instanceof List) {
StringBuilder text = new StringBuilder("A:");
for (Object item : (List<?>) value) text.append(canonical(item)).append(';');
return text.toString();
}
if (value instanceof Boolean) return "Z:" + value;
return "S:" + value;
}
private static byte[] sha256(String value) {
try {
return MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8));
} catch (Exception impossible) {
throw new IllegalStateException("JVM 不支持 SHA-256", impossible);
}
}
private static String hex(byte[] bytes) {
StringBuilder result = new StringBuilder(bytes.length * 2);
for (byte value : bytes) result.append(String.format("%02x", value & 0xff));
return result.toString();
}
public static final class Diff {
private final boolean matches;
private final String type;
private final Set<String> fields;
private Diff(boolean matches, String type, Set<String> fields) {
this.matches = matches;
this.type = type;
this.fields = Collections.unmodifiableSet(new TreeSet<>(fields));
}
public static Diff match() { return new Diff(true, "match", Collections.emptySet()); }
public static Diff mismatch(String type, Set<String> fields) {
return new Diff(false, type, fields);
}
public boolean isMatches() { return matches; }
public String getType() { return type; }
public Set<String> getFields() { return fields; }
}
}
@@ -0,0 +1,36 @@
package com.Modules.NormalData.compiled;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/** 影子计算使用独立有界线程池,队列满时只跳过影子,不反压 Kafka 主路径。 */
@Component
public class NormalizationShadowExecutor {
private final ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 30L, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(100), new ShadowThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
public <T> Future<T> submit(java.util.concurrent.Callable<T> task) {
return executor.submit(task);
}
@PreDestroy
public void close() {
executor.shutdownNow();
}
private static final class ShadowThreadFactory implements ThreadFactory {
private final AtomicInteger sequence = new AtomicInteger();
@Override public Thread newThread(Runnable task) {
Thread thread = new Thread(task, "normalization-shadow-" + sequence.incrementAndGet());
thread.setDaemon(true);
return thread;
}
}
}
@@ -0,0 +1,18 @@
package com.Modules.NormalData.compiled;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/** 规则间共享但绝不允许被规则修改的消息解析结果。 */
public final class ParsedMessage {
private final Map<String, Object> fields;
public ParsedMessage(Map<String, Object> fields) {
this.fields = Collections.unmodifiableMap(new LinkedHashMap<>(fields));
}
public Map<String, Object> getFields() {
return fields;
}
}
@@ -0,0 +1,47 @@
package com.Modules.NormalData.compiled;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/** Kafka listener 在线程切换前完成 envelope 解析后生成的不可变输入。 */
public final class PreparedNormalizationInput {
private final String eventId;
private final String topic;
private final LocalDateTime receivedAt;
private final Long deviceId;
private final Long deviceCollectId;
private final String dataType;
private final String syslogBody;
private final String fullMessage;
private final String deviceHeader;
private final Map<String, String> deviceInfo;
public PreparedNormalizationInput(String eventId, String topic, LocalDateTime receivedAt,
Long deviceId, Long deviceCollectId, String dataType,
String syslogBody, String fullMessage, String deviceHeader,
Map<String, String> deviceInfo) {
this.eventId = eventId;
this.topic = topic;
this.receivedAt = receivedAt;
this.deviceId = deviceId;
this.deviceCollectId = deviceCollectId;
this.dataType = dataType;
this.syslogBody = syslogBody;
this.fullMessage = fullMessage;
this.deviceHeader = deviceHeader;
this.deviceInfo = Collections.unmodifiableMap(new LinkedHashMap<>(deviceInfo));
}
public String getEventId() { return eventId; }
public String getTopic() { return topic; }
public LocalDateTime getReceivedAt() { return receivedAt; }
public Long getDeviceId() { return deviceId; }
public Long getDeviceCollectId() { return deviceCollectId; }
public String getDataType() { return dataType; }
public String getSyslogBody() { return syslogBody; }
public String getFullMessage() { return fullMessage; }
public String getDeviceHeader() { return deviceHeader; }
public Map<String, String> getDeviceInfo() { return deviceInfo; }
}
@@ -0,0 +1,48 @@
package com.Modules.NormalData.compiled;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
/** 单个 Kafka 批次内共享的低基数聚合指标。 */
public final class RuleExecutionMetrics {
private final LongAdder messageParseNanos = new LongAdder();
private final LongAdder fieldMappingNanos = new LongAdder();
private final LongAdder filterCompleteNanos = new LongAdder();
private final LongAdder enrichmentNanos = new LongAdder();
private final LongAdder assembleNanos = new LongAdder();
private final LongAdder compiledHits = new LongAdder();
private final LongAdder compiledMisses = new LongAdder();
private final LongAdder parseHits = new LongAdder();
private final LongAdder parseMisses = new LongAdder();
private final LongAdder shadowCompared = new LongAdder();
private final LongAdder shadowDifferences = new LongAdder();
public void addMessageParseNanos(long value) { messageParseNanos.add(nonNegative(value)); }
public void addFieldMappingNanos(long value) { fieldMappingNanos.add(nonNegative(value)); }
public void addFilterCompleteNanos(long value) { filterCompleteNanos.add(nonNegative(value)); }
public void addEnrichmentNanos(long value) { enrichmentNanos.add(nonNegative(value)); }
public void addAssembleNanos(long value) { assembleNanos.add(nonNegative(value)); }
public void compiledHit() { compiledHits.increment(); }
public void compiledMiss() { compiledMisses.increment(); }
public void parseHit() { parseHits.increment(); }
public void parseMiss() { parseMisses.increment(); }
public void shadowCompared(boolean different) {
shadowCompared.increment();
if (different) shadowDifferences.increment();
}
public long getMessageParseMs() { return millis(messageParseNanos.sum()); }
public long getFieldMappingMs() { return millis(fieldMappingNanos.sum()); }
public long getFilterCompleteMs() { return millis(filterCompleteNanos.sum()); }
public long getEnrichmentMs() { return millis(enrichmentNanos.sum()); }
public long getAssembleMs() { return millis(assembleNanos.sum()); }
public long getCompiledHits() { return compiledHits.sum(); }
public long getCompiledMisses() { return compiledMisses.sum(); }
public long getParseHits() { return parseHits.sum(); }
public long getParseMisses() { return parseMisses.sum(); }
public long getShadowCompared() { return shadowCompared.sum(); }
public long getShadowDifferences() { return shadowDifferences.sum(); }
private static long millis(long nanos) { return TimeUnit.NANOSECONDS.toMillis(nanos); }
private static long nonNegative(long value) { return Math.max(0L, value); }
}
@@ -0,0 +1,118 @@
package com.Modules.NormalData.compiled;
import java.io.Serializable;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Collections;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/** 脱离 JDBC/CLOB 生命周期的原始规则事实。 */
public final class RuleSource implements Serializable {
private static final long serialVersionUID = 1L;
private final long id;
private final String name;
private final String displayName;
private final String dataType;
private final String ruleContent;
private final Boolean running;
private final String delFlag;
private final String fingerprint;
/** 仅供 Kryo5 反序列化;业务代码只能通过完整构造器创建不可变快照。 */
@SuppressWarnings("unused")
private RuleSource() {
this(0L, null, null, null, null, null, null);
}
public RuleSource(long id, String name, String displayName, String dataType,
String ruleContent, Boolean running, String delFlag) {
this.id = id;
this.name = name;
this.displayName = displayName;
this.dataType = dataType;
this.ruleContent = ruleContent;
this.running = running;
this.delFlag = delFlag;
this.fingerprint = fingerprint(id, name, dataType, ruleContent, running, delFlag);
}
public static RuleSource fromMap(Map<String, Object> source) {
if (source == null) {
throw new IllegalArgumentException("规则不能为空");
}
return new RuleSource(requiredLong(source, "id"), string(source.get("name")),
string(source.get("display_name")), string(source.get("data_type")),
string(source.get("rule_content")), booleanValue(source.get("is_running")),
string(source.get("del_flag")));
}
public Map<String, Object> asLegacyMap() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", id);
map.put("name", name);
map.put("display_name", displayName);
map.put("data_type", dataType);
map.put("rule_content", ruleContent);
map.put("is_running", running);
map.put("del_flag", delFlag);
return Collections.unmodifiableMap(map);
}
private static long requiredLong(Map<String, Object> source, String key) {
Object value = source.get(key);
if (value == null) {
throw new IllegalArgumentException("规则缺少字段: " + key);
}
return Long.parseLong(value.toString());
}
private static String string(Object value) {
return value == null ? null : value.toString();
}
private static Boolean booleanValue(Object value) {
if (value == null) return null;
String text = value.toString().trim();
if ("1".equals(text) || "y".equalsIgnoreCase(text) || "yes".equalsIgnoreCase(text)) return true;
if ("0".equals(text) || "n".equalsIgnoreCase(text) || "no".equalsIgnoreCase(text)) return false;
return Boolean.valueOf(text);
}
public long getId() { return id; }
public long getRuleId() { return id; }
public String getName() { return name; }
public String getDisplayName() { return displayName; }
public String getDataType() { return dataType; }
public String getRuleContent() { return ruleContent; }
public Boolean getRunning() { return running; }
public String getDelFlag() { return delFlag; }
public String getFingerprint() { return fingerprint; }
private static String fingerprint(long id, String name, String dataType, String content,
Boolean running, String delFlag) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
update(digest, Long.toString(id));
update(digest, name);
update(digest, dataType);
update(digest, content);
update(digest, running == null ? null : running.toString());
update(digest, delFlag);
StringBuilder hex = new StringBuilder(64);
for (byte value : digest.digest()) hex.append(String.format("%02x", value & 0xff));
return hex.toString();
} catch (NoSuchAlgorithmException impossible) {
throw new IllegalStateException("JVM 不支持 SHA-256", impossible);
}
}
private static void update(MessageDigest digest, String value) {
byte[] bytes = value == null ? new byte[0] : value.getBytes(StandardCharsets.UTF_8);
digest.update(ByteBuffer.allocate(4).putInt(value == null ? -1 : bytes.length).array());
if (value != null) digest.update(bytes);
}
}
@@ -44,10 +44,7 @@ public class logNormalData {
{
//解析SyslogMessage
SyslogMessage msg = SyslogParser.parse(MsgContent);
System.out.println("解析结果-log content: " + msg.getMessage().toString());
Map<String, Object> flatMap = JsonParser.parseJsonToFlatMap(msg.getMessage().toString());
flatMap.forEach((key, value) -> System.out.println(key + " = " + value));
List<Map<String, Object>> rulelst=dmNormalizeRuleService.selectByDeviceId((long)1);
return flatMap;
}
@@ -55,10 +52,6 @@ public class logNormalData {
public List<Map<String, Object>> getRuleList( long device_id)
{
List<Map<String, Object>> rulelst=dmNormalizeRuleService.selectByDeviceId((long)1);
if (rulelst!=null)
{
System.out.println("rulelst: " + rulelst);
}
return rulelst;
}
@@ -70,14 +63,13 @@ public class logNormalData {
*/
public static String ParserMessageJsonType(String MsgContent , Cropper_paramsType cropperParams)
{
System.out.println("ParserMessageJsonType MsgContent:" +MsgContent );
logger.trace("准备截取JSON内容,日志长度={}", MsgContent == null ? 0 : MsgContent.length());
if(cropperParams ==null) {
return removeBOM(JsonParser.extractJsonFromLogMessage(MsgContent));
}
String strJson ="";
if(cropperParams.gettail_key().equals("") && cropperParams.gettail_offset()==0 ) {
strJson = StringExtractorUtil.extractFromStartToEnd(MsgContent, cropperParams.gethead_key(), cropperParams.gethead_offset());
System.out.println("ParserMessageJsonType extractFromStartToEnd strJson" +strJson );
}
else {
strJson= StringExtractorUtil.extract(MsgContent,cropperParams.gethead_key(),cropperParams.gethead_offset(),cropperParams.gettail_key(),cropperParams.gettail_offset() );
@@ -159,5 +151,3 @@ public class logNormalData {
}
}
@@ -0,0 +1,100 @@
package com.Modules.NormalData.metrics;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
/** 固定维度的进程内规则指标;不以 rule/device/event 作为长期标签。 */
@Component
public class NormalizationRuleMetrics {
private final Counters compiledGet = new Counters("hit", "miss", "failure_hit");
private final Counters compiledCompile = new Counters("success", "failure");
private final Counters shadow = new Counters("match", "mismatch", "error", "timeout", "skipped");
private final Counters ruleSource = new Counters("l1_hit", "l2_hit", "load", "suppressed", "failure");
private final Counters enrichment = new Counters("device_hit", "device_load", "device_failure",
"collect_task_hit", "collect_task_load", "collect_task_failure");
private final Counters invalidation = new Counters("local", "remote", "after_commit", "failure");
private final Counters messageParse = new Counters("hit", "miss");
private final LongAdder ruleJsonParse = new LongAdder();
private final LongAdder compileDurationTotalNanos = new LongAdder();
private final LongAdder compileDurationCount = new LongAdder();
private final AtomicLong compileDurationMaxNanos = new AtomicLong();
private final AtomicLong compiledCacheSize = new AtomicLong();
private final AtomicLong compiledEvictions = new AtomicLong();
public void compiledGet(String outcome) { compiledGet.increment(outcome); }
public void compiled(String outcome, long nanos) {
compiledCompile.increment(outcome);
compileDurationTotalNanos.add(Math.max(0L, nanos));
compileDurationCount.increment();
updateMax(compileDurationMaxNanos, nanos);
}
public void shadow(String outcome) { shadow.increment(outcome); }
public void ruleSource(String outcome) { ruleSource.increment(outcome); }
public void enrichment(String entity, String outcome) {
enrichment.increment(("collect_task".equals(entity) ? "collect_task" : "device") + "_" + outcome);
}
public void invalidation(String outcome) { invalidation.increment(outcome); }
public void messageParse(String outcome) { messageParse.increment(outcome); }
public void ruleJsonParse() { ruleJsonParse.increment(); }
public void compiledCacheGauge(long size, long evictions) {
compiledCacheSize.set(Math.max(0L, size));
compiledEvictions.set(Math.max(0L, evictions));
}
public Map<String, Object> snapshot() {
Map<String, Object> result = new LinkedHashMap<>();
result.put("compiled_rule_cache_get_total", compiledGet.snapshot());
result.put("compiled_rule_compile_total", compiledCompile.snapshot());
result.put("compiled_rule_compile_duration_ms_total",
nanosToMillis(compileDurationTotalNanos.sum()));
result.put("compiled_rule_compile_duration_ms_count", compileDurationCount.sum());
result.put("compiled_rule_compile_duration_ms_max",
nanosToMillis(compileDurationMaxNanos.get()));
result.put("compiled_rule_cache_size", compiledCacheSize.get());
result.put("compiled_rule_cache_eviction_total", compiledEvictions.get());
result.put("shadow_compare_total", shadow.snapshot());
result.put("rule_source_cache_get_total", ruleSource.snapshot());
result.put("enrichment_cache_get_total", enrichment.snapshot());
result.put("rule_invalidation_total", invalidation.snapshot());
result.put("message_parse_cache_get_total", messageParse.snapshot());
result.put("rule_json_parse_total", ruleJsonParse.sum());
return result;
}
private static void updateMax(AtomicLong target, long value) {
long nonNegative = Math.max(0L, value);
long current;
do {
current = target.get();
if (current >= nonNegative) return;
} while (!target.compareAndSet(current, nonNegative));
}
private static long nanosToMillis(long nanos) { return nanos / 1_000_000L; }
private static final class Counters {
private final Map<String, LongAdder> values = new LinkedHashMap<>();
private Counters(String... names) {
for (String name : names) values.put(name, new LongAdder());
}
private void increment(String name) {
LongAdder counter = values.get(name);
if (counter == null) throw new IllegalArgumentException("未知指标维度: " + name);
counter.increment();
}
private Map<String, Long> snapshot() {
Map<String, Long> copy = new LinkedHashMap<>();
for (Map.Entry<String, LongAdder> entry : values.entrySet()) {
copy.put(entry.getKey(), entry.getValue().sum());
}
return copy;
}
}
}
@@ -74,5 +74,10 @@ public class Alarm {
private String[] httpReqBody;
private String[] httpRespHeader;
private String[] httpRespBody;
/** 规则告警幂等与追踪元数据。 */
private String ruleId;
private String ruleVersion;
private LocalDateTime windowStart;
private String groupKeyHash;
}
}
@@ -79,4 +79,9 @@ public class AlarmVisit {
private String dtype;
private String originField;
private String originLog;
}
/** 算法告警幂等与追踪元数据。 */
private String algorithmId;
private String algorithmVersion;
private String eventId;
private String resultType;
}
@@ -2,6 +2,7 @@ package com.common.entity;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.OffsetDateTime;
public class DeviceDevice {
public Integer id;
@@ -38,6 +39,7 @@ public class DeviceDevice {
public String managerName;
public Integer todayParseCount;
public Integer todayNonLogCount;
public LocalDate todayStatsDate;
public Long createDept;
public Integer deviceCollectId;
@@ -315,6 +317,14 @@ public class DeviceDevice {
this.todayNonLogCount = todayNonLogCount;
}
public LocalDate getTodayStatsDate() {
return todayStatsDate;
}
public void setTodayStatsDate(LocalDate todayStatsDate) {
this.todayStatsDate = todayStatsDate;
}
public Long getCreateDept() {
return createDept;
}
@@ -368,9 +378,10 @@ public class DeviceDevice {
", managerName='" + managerName + '\'' +
", todayParseCount=" + todayParseCount +
", todayNonLogCount=" + todayNonLogCount +
", todayStatsDate=" + todayStatsDate +
", createDept=" + createDept +
", deviceCollectId=" + deviceCollectId +
'}';
}
}
}
@@ -0,0 +1,24 @@
package com.common.entity;
import java.time.LocalDateTime;
/** Kafka 入口元数据形成的轻量接收状态事件,不包含原始 Syslog 正文。 */
public final class IngressStatsEvent {
private final String eventId;
private final Long deviceId;
private final Long deviceCollectId;
private final LocalDateTime receiveTime;
public IngressStatsEvent(String eventId, Long deviceId, Long deviceCollectId,
LocalDateTime receiveTime) {
this.eventId = eventId;
this.deviceId = deviceId;
this.deviceCollectId = deviceCollectId;
this.receiveTime = receiveTime;
}
public String getEventId() { return eventId; }
public Long getDeviceId() { return deviceId; }
public Long getDeviceCollectId() { return deviceCollectId; }
public LocalDateTime getReceiveTime() { return receiveTime; }
}
@@ -0,0 +1,33 @@
package com.common.entity;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
/** 统计投影所需的最小事实变化,不包含原始日志正文。 */
@Data
@Builder
public class StatsChangeRecord {
private Long changeSeq;
private String changeId;
private String sourceType;
private String sourceRowId;
private String operationType;
private LocalDate oldStatDate;
private LocalDate newStatDate;
private Long oldDeviceId;
private Long newDeviceId;
private Long oldCollectId;
private Long newCollectId;
private Long oldRuleId;
private Long newRuleId;
private LocalDateTime oldEventTime;
private LocalDateTime newEventTime;
private LocalDateTime oldCollectTime;
private LocalDateTime newCollectTime;
private Integer oldPushSuccess;
private Integer newPushSuccess;
private LocalDateTime committedAt;
}
@@ -376,8 +376,9 @@ public class SyslogNormalAlarm {
private String destinationSystem;
private String etlHost;
private Integer normalizeRuleId;
private String ruleVersion;
private String normalizeRuleName;
private String syslogUuid;
private String syslogTopic;
}
}
@@ -375,7 +375,8 @@ public class SyslogNormalData {
private String destinationSystem;
private String etlHost;
private Integer normalizeRuleId;
private String ruleVersion;
private String normalizeRuleName;
private String syslogUuid;
private String syslogTopic;
}
}
@@ -8,6 +8,12 @@ import java.util.List;
@Mapper
public interface AlarmMapper {
@Select({"<script>",
"SELECT id FROM alarm WHERE id IN",
"<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>",
"</script>"})
List<String> selectExistingIds(@Param("ids") List<String> ids);
/**
* 批量插入告警数据
*/
@@ -19,7 +25,8 @@ public interface AlarmMapper {
"attack_port, victim_port, attack_method, etl_time, log_count, ",
"attack_chain_phase, disposition_advice, attack_direction, ",
"judged_state, disposed_state, attack_result, fall, payload, dns_info, engine_type, " ,
"http_req_header , http_req_body,http_resp_header , http_resp_body ",
"http_req_header , http_req_body,http_resp_header , http_resp_body, ",
"rule_id, rule_version, window_start, group_key_hash ",
") VALUES ",
"<foreach collection='list' item='item' separator=','>",
"( #{item.id}, #{item.createdAt}, #{item.alarmName}, #{item.alarmLevel}, ",
@@ -41,7 +48,8 @@ public interface AlarmMapper {
"#{item.httpReqHeader, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, ",
"#{item.httpReqBody, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, ",
"#{item.httpRespHeader, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, ",
"#{item.httpRespBody, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler})",
"#{item.httpRespBody, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, ",
"#{item.ruleId}, #{item.ruleVersion}, #{item.windowStart}, #{item.groupKeyHash})",
"</foreach>",
"</script>"})
void batchInsert(@Param("list") List<Alarm> alarmList);
@@ -56,7 +64,8 @@ public interface AlarmMapper {
"attack_port, victim_port, attack_method, etl_time, log_count, " +
"attack_chain_phase, disposition_advice, attack_direction, " +
"judged_state, disposed_state, attack_result, fall, payload, dns_info, engine_type, " +
"http_req_header , http_req_body,http_resp_header , http_resp_body " +
"http_req_header , http_req_body,http_resp_header , http_resp_body, " +
"rule_id, rule_version, window_start, group_key_hash " +
") VALUES (" +
"#{id}, #{createdAt}, #{alarmName}, #{alarmLevel}, " +
"#{alarmType}, #{alarmMajorType}, #{alarmMinorType}, #{alarmAreaId}, " +
@@ -76,7 +85,8 @@ public interface AlarmMapper {
"#{httpReqHeader, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, " +
"#{httpReqBody, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, " +
"#{httpRespHeader, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, " +
"#{httpRespBody, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler} " +
"#{httpRespBody, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, " +
"#{ruleId}, #{ruleVersion}, #{windowStart}, #{groupKeyHash} " +
")")
void insert(Alarm alarm);
}
}

Some files were not shown because too many files have changed in this diff Show More