From a0b9b58291ca46ccf4fb23ab773b8f39281bf6e2 Mon Sep 17 00:00:00 2001 From: wuweihua <88888888> Date: Thu, 19 Nov 2026 10:36:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=93=E5=8D=95=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=88=86=E5=8C=BA=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bms/price/task/EsBizLogCleanupTask.java | 159 ++++++++++++++++++ bms-back/sql/price/es_biz_log_cleanup_job.sql | 24 +++ 2 files changed, 183 insertions(+) create mode 100644 bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java create mode 100644 bms-back/sql/price/es_biz_log_cleanup_job.sql diff --git a/bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java b/bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java new file mode 100644 index 00000000..0f76a58d --- /dev/null +++ b/bms-back/bms-system/src/main/java/com/bms/price/task/EsBizLogCleanupTask.java @@ -0,0 +1,159 @@ +package com.bms.price.task; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.bms.common.utils.StringUtils; +import com.bms.price.search.ElasticsearchProperties; + +/** + * 清理 ES 业务日志(bms_biz_log)中超期文档。 + * + *

纳入「系统监控 - 定时任务」: + * 调用目标 {@code esBizLogCleanupTask.cleanupOlderThanMonths(3)}, + * 表示删除 3 个月前的数据;也可 {@code cleanup()} 默认 3 个月。

+ */ +@Component("esBizLogCleanupTask") +public class EsBizLogCleanupTask +{ + private static final Logger log = LoggerFactory.getLogger(EsBizLogCleanupTask.class); + + private static final int DEFAULT_MONTHS = 3; + private static final int MIN_MONTHS = 1; + private static final int MAX_MONTHS = 24; + + @Autowired + private ElasticsearchProperties properties; + + /** 默认清理 3 个月前数据。 */ + public void cleanup() + { + cleanupOlderThanMonths(Integer.valueOf(DEFAULT_MONTHS)); + } + + /** + * 删除早于(今天 - months 个月)的 ES 日志。 + * Quartz:esBizLogCleanupTask.cleanupOlderThanMonths(3) + */ + public void cleanupOlderThanMonths(Integer months) + { + if (properties == null || !properties.isEnabled()) + { + log.info("ES 未启用,跳过日志清理"); + return; + } + int keepMonths = months == null ? DEFAULT_MONTHS : months.intValue(); + if (keepMonths < MIN_MONTHS) + { + keepMonths = MIN_MONTHS; + } + if (keepMonths > MAX_MONTHS) + { + keepMonths = MAX_MONTHS; + } + String cutoff = cutoffTime(keepMonths); + String index = StringUtils.isEmpty(properties.getIndex()) ? "bms_biz_log" : properties.getIndex().trim(); + log.info("开始清理 ES 日志 index={} cutoff(<)={} keepMonths={}", index, cutoff, keepMonths); + + RestTemplate rest = buildRestTemplate(); + long deleted = deleteOlderThan(rest, index, cutoff); + log.info("ES 日志清理完成 index={} deleted={}", index, deleted); + } + + private long deleteOlderThan(RestTemplate rest, String index, String cutoff) + { + JSONObject body = new JSONObject(); + body.put("query", buildOlderQuery(cutoff)); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity entity = new HttpEntity(body.toJSONString(), headers); + String url = baseUrl() + "/" + index + "/_delete_by_query?conflicts=proceed&refresh=true"; + ResponseEntity resp = rest.exchange(url, HttpMethod.POST, entity, String.class); + JSONObject root = JSON.parseObject(resp.getBody()); + if (root == null) + { + return 0L; + } + return root.getLongValue("deleted"); + } + + private JSONObject buildOlderQuery(String cutoff) + { + JSONArray should = new JSONArray(); + for (String field : new String[] { "updateTime.keyword", "updatedAt.keyword", "updateTime", "updatedAt" }) + { + JSONObject rangeInner = new JSONObject(); + JSONObject lt = new JSONObject(); + lt.put("lt", cutoff); + rangeInner.put(field, lt); + JSONObject range = new JSONObject(); + range.put("range", rangeInner); + should.add(range); + } + JSONObject bool = new JSONObject(); + bool.put("should", should); + bool.put("minimum_should_match", Integer.valueOf(1)); + JSONObject query = new JSONObject(); + query.put("bool", bool); + return query; + } + + private static String cutoffTime(int months) + { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.MONTH, -months); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime()); + } + + private RestTemplate buildRestTemplate() + { + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); + factory.setConnectTimeout(10000); + // delete_by_query 可能较久 + factory.setReadTimeout(600000); + RestTemplate template = new RestTemplate(factory); + if (StringUtils.isNotEmpty(properties.getUsername())) + { + String user = properties.getUsername(); + String pass = properties.getPassword() == null ? "" : properties.getPassword(); + template.getInterceptors().add((request, body, execution) -> { + request.getHeaders().setBasicAuth(user, pass); + return execution.execute(request, body); + }); + } + return template; + } + + private String baseUrl() + { + String base = properties.getUris(); + if (StringUtils.isEmpty(base)) + { + base = "http://127.0.0.1:9200"; + } + if (base.endsWith("/")) + { + base = base.substring(0, base.length() - 1); + } + return base; + } +} diff --git a/bms-back/sql/price/es_biz_log_cleanup_job.sql b/bms-back/sql/price/es_biz_log_cleanup_job.sql new file mode 100644 index 00000000..77472ddd --- /dev/null +++ b/bms-back/sql/price/es_biz_log_cleanup_job.sql @@ -0,0 +1,24 @@ +-- Optional: register ES biz-log cleanup job (keep last 3 months) +-- Invoke: esBizLogCleanupTask.cleanupOlderThanMonths(3) +-- Cron: 03:00 on the 1st of every month + +INSERT INTO sys_job ( + job_name, job_group, invoke_target, cron_expression, + misfire_policy, concurrent, status, create_by, create_time, remark +) +SELECT + 'ES业务日志清理', + 'DEFAULT', + 'esBizLogCleanupTask.cleanupOlderThanMonths(3)', + '0 0 3 1 * ?', + '3', + '1', + '0', + 'admin', + NOW(), + '删除 bms_biz_log 中 3 个月前的文档;ES 需在线' +FROM DUAL +WHERE NOT EXISTS ( + SELECT 1 FROM sys_job + WHERE invoke_target = 'esBizLogCleanupTask.cleanupOlderThanMonths(3)' +);