From add93b3693930acbb7128732ad6f281afa13c3f0 Mon Sep 17 00:00:00 2001 From: "USER-20220102CG\\noblelift" <546428999@qq.com> Date: Wed, 13 Sep 2023 15:33:25 +0800 Subject: [PATCH] =?UTF-8?q?rev=20=20=E6=9B=B4=E6=96=B0lucene=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device_driver/driver/OpcDeviceDriver.java | 2 +- ...alletizingManipulatorSiteDeviceDriver.java | 11 +- .../main/java/org/nl/acs/ext/wms/LmsUtil.java | 6 +- .../nl/acs/opc/DeviceOpcProtocolRunable.java | 49 +++---- .../main/java/org/nl/acs/opc/OpcItemDto.java | 1 + .../lucene/common/LogMessageConstant.java | 44 +++++++ .../modules/lucene/common/LuceneAppender.java | 123 ++++++++++++++++++ .../lucene/common/LuceneIndexWriter.java | 79 ++++++----- .../nl/modules/lucene/common/Searcher.java | 3 +- .../impl/LuceneExecuteLogServiceImpl.java | 81 ++++++------ .../src/main/resources/log/Lucene.xml | 9 +- .../src/main/resources/logback-spring.xml | 13 +- 12 files changed, 308 insertions(+), 113 deletions(-) create mode 100644 acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LogMessageConstant.java create mode 100644 acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneAppender.java diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/OpcDeviceDriver.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/OpcDeviceDriver.java index 474dd7b..8e7bb1e 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/OpcDeviceDriver.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/driver/OpcDeviceDriver.java @@ -24,7 +24,7 @@ public interface OpcDeviceDriver extends DeviceDriver { //设备扩展表【acs_device_extra】 WQLObject extraTab = WQLObject.getWQLObject("acs_device_extra"); - JSONArray arr = extraTab.query("filed_type='02' and device_id = '" + this.getDevice().getDevice_id() + "'").getResultJSONArray(0); + JSONArray arr = extraTab.query(" (filed_type='02' or filed_type='03' )and device_id = '" + this.getDevice().getDevice_id() + "'").getResultJSONArray(0); for (int i = 0; i < arr.size(); i++) { JSONObject json = arr.getJSONObject(i); OpcItemDto dto = new OpcItemDto(); diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/lnsh_palletizing_manipulator_site/LnshPalletizingManipulatorSiteDeviceDriver.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/lnsh_palletizing_manipulator_site/LnshPalletizingManipulatorSiteDeviceDriver.java index 12c6101..6e4c9c8 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/lnsh_palletizing_manipulator_site/LnshPalletizingManipulatorSiteDeviceDriver.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/lnsh_palletizing_manipulator_site/LnshPalletizingManipulatorSiteDeviceDriver.java @@ -37,6 +37,7 @@ import org.nl.acs.opc.DeviceAppService; import org.nl.acs.order.service.ProduceshiftorderService; import org.nl.acs.route.service.RouteLineService; import org.nl.acs.task.service.TaskService; +import org.nl.modules.lucene.enums.LogTypeEnum; import org.nl.modules.lucene.service.LuceneExecuteLogService; import org.nl.modules.lucene.service.dto.LuceneLogDto; import org.nl.modules.wql.util.SpringContextHolder; @@ -898,8 +899,15 @@ public class LnshPalletizingManipulatorSiteDeviceDriver extends AbstractOpcDevic ApplyTaskResponse resp = JSON.toJavaObject(JSONObject.parseObject(acsToWmsService.applyTask(request)),ApplyTaskResponse.class); message = RequestMethodEnum.getName("apply_take_full_vehicle") + "apply_take_full_vehicle 接口请求LMS..."; - lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message + "参数:" + JSON.toJSONString(request))); + LuceneLogDto dto = new LuceneLogDto(); + dto.setDevice_code(this.device_code); + dto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); + dto.setRemark(JSON.toJSONString(request)); + lucene.deviceExecuteLog(dto); +// lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, message + "参数:" + JSON.toJSONString(request))); +// log.info("{}",JSON.toJSONString(dto)); +// log.info("{}",JSON.toJSONString(new LuceneLogDto(this.device_code, message + "参数:" + JSON.toJSONString(request)))); if (resp.getCode() == 200) { this.writing(200); this.setRequireSucess(true); @@ -1200,7 +1208,6 @@ public class LnshPalletizingManipulatorSiteDeviceDriver extends AbstractOpcDevic ReadUtil.write(itemMap, server); ReadUtil.write(itemMap, server); server.disconnect(); - logServer.deviceExecuteLog(this.device_code, "", "", "to_command 写入 " + command); lucene.deviceExecuteLog(new LuceneLogDto(this.device_code, "to_command 写入 " + command)); } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/LmsUtil.java b/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/LmsUtil.java index 84b8de7..57891f3 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/LmsUtil.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/ext/wms/LmsUtil.java @@ -35,19 +35,19 @@ public class LmsUtil { } String liKu_wcs_url = paramService.findByCode("wmsurl").getValue(); try { - log.info("请求LMS参数:{}", JSON.toJSONString(requestParam)); +// log.info("请求LMS参数:{}", JSON.toJSONString(requestParam)); String body = HttpRequest .post(liKu_wcs_url + api).setConnectionTimeout(3000) .body(JSON.toJSONString(requestParam)) .execute() .body(); - log.info("请求LMS参数返回参数:{}", body); +// log.info("请求LMS参数返回参数:{}", body); return body; } catch (Exception e) { JSONObject result = new JSONObject(); result.put("code", "500"); result.put("message", e.getMessage()); - log.info("请求LMS异常返回参数:{}", String.valueOf(result)); +// log.info("请求LMS异常返回参数:{}", String.valueOf(result)); return String.valueOf(result); } } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java index 5a10c40..97a5e25 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceOpcProtocolRunable.java @@ -3,10 +3,12 @@ package org.nl.acs.opc; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.nl.acs.udw.UnifiedDataAccessor; import org.nl.acs.udw.UnifiedDataAccessorFactory; import org.nl.acs.udw.UnifiedDataAppService; +import org.nl.modules.lucene.enums.LogTypeEnum; import org.nl.modules.lucene.service.LuceneExecuteLogService; import org.nl.modules.lucene.service.dto.LuceneLogDto; import org.nl.modules.wql.util.SpringContextHolder; @@ -118,7 +120,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC try { Item item = group.addItem(string); itemsMap.put(string, item); - log.trace("添加成功 {}", string); +// log.trace("添加成功 {}", string); } catch (Exception var26) { err_message.append(string + ":" + var26.getMessage()); if (!is_error) { @@ -130,7 +132,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC String tag; if (is_error) { tag = err_message.toString(); - log.warn("{}:{}", OpcConfig.resource_code, tag); +// log.warn("{}:{}", OpcConfig.resource_code, tag); } if (!OpcStartTag.is_run) { @@ -152,18 +154,18 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC while (DeviceOpcSynchronizeAutoRun.isRun) { long begin = System.currentTimeMillis(); if (log.isTraceEnabled()) { - log.trace("{} 开始记时{}", tag, DateUtil.now()); +// log.trace("{} 开始记时{}", tag, DateUtil.now()); } Map itemStatus = group.read(true, (Item[]) itemsMap.values().toArray(new Item[0])); long end = System.currentTimeMillis(); long duration = end - begin; if (log.isTraceEnabled()) { - log.trace("{} 读取耗时:{}", tag, duration); +// log.trace("{} 读取耗时:{}", tag, duration); } if (duration > 1000L) { if (!time_out) { - log.warn("{} 读取超时 : {}", tag, duration); +// log.warn("{} 读取超时 : {}", tag, duration); } time_out = true; @@ -186,7 +188,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC String itemId = item.getId(); Object his = accessor_value.getValue(itemId); if (!ObjectUtl.isEquals(itemState.getQuality(), QualityTypeValue.OPC_QUALITY_GOOD) && his != null) { - log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality()); +// log.warn("opc 值不健康 item: {}, 状态: {}", itemId, itemState.getQuality()); } if (!UnifiedDataAppService.isEquals(value, his)) { @@ -225,18 +227,17 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC log.warn(tag + "重新创建server"); log.warn("{} 所有内容都为空, all_null:{} ,暂定{}s", tag, all_null, 30000 + random); } -// ThreadUtl.sleep((long) (30000 + random)); ThreadUtl.sleep((long) ((new Random()).nextInt(3) + 1) * 1000); break start; } else if (this.all_null < 12) { if (log.isWarnEnabled()) { - log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random); +// log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, '\uea60' + random); } ThreadUtl.sleep((long) ('\uea60' + random)); } else { if (log.isWarnEnabled()) { - log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random); +// log.warn("{} 所有内容都为空, all_null:{} ,暂定{}ms", tag, all_null, 120000 + random); } ThreadUtl.sleep((long) (120000 + random)); @@ -351,7 +352,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC this.server = null; } - log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected"); +// log.warn("opc server {} {}", this.getOpcGroupID(), connected ? "connected" : "disconnected"); } private String getOpcGroupID() { @@ -381,7 +382,7 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC // } else if (log.isInfoEnabled()) { // log.info("Item {} new value: {}, Timestamp: {}, Quality: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime(), itemState.getQuality()}); // } - log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()}); +// log.trace("Item {} new value: {}, Timestamp: {}", new Object[]{itemId, itemState.getValue(), itemState.getTimestamp().getTime()}); OpcItemDto itemDto = this.getItem(itemId); // if (Boolean.TRUE.equals(itemDto.getNeed_log())) { @@ -397,6 +398,8 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC private void logItemChanged(String itemId, UnifiedDataAccessor accessor_value, Object value, OpcItemDto itemDto) { Object his = accessor_value.getValue(itemId); + itemDto.setHis_item_value(his); + itemDto.setItem_value(value); List relate_items = itemDto.getRelate_items(); if (relate_items != null && !relate_items.isEmpty()) { StringBuilder sb = new StringBuilder(); @@ -407,24 +410,22 @@ public class DeviceOpcProtocolRunable implements Runnable, DataCallback, ServerC Object obj = accessor_value.getValue(relate); sb.append("key:" + relate + "value:" + obj + ";"); } - log.info("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value, sb}); - // 存在上次点位值为null情况 则不记录日志 if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time")) { - if((ObjectUtil.equal(his,"null") || ObjectUtil.isEmpty(his)) || (ObjectUtil.equal(value,"null") || ObjectUtil.isEmpty(value)) ){ - //lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value))); - } else { - lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value))); - } + // 存在上次点位值为null情况 则不记录日志 + LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), + String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value())); + luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); + log.info("{}", JSON.toJSONString(luceneLogDto)); } } else { - log.info("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value}); +// log.info("设备:{}信号{}变更从{}->{};信号快照:{}", new Object[]{itemDto.getDevice_code(), itemId, his, value}); // 存在上次点位值为null情况 则不记录日志 if (!itemDto.getItem_code().endsWith("heartbeat") && !itemDto.getItem_code().endsWith("time")) { - if((ObjectUtil.equal(his,"null") || ObjectUtil.isEmpty(his)) || (ObjectUtil.equal(value,"null") || ObjectUtil.isEmpty(value)) ){ - //lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value))); - } else { - lucene.deviceExecuteLog(new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), String.valueOf(his), String.valueOf(value))); - } + LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), + String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value())); + luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); + log.info("{}", JSON.toJSONString(luceneLogDto)); + } } } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/OpcItemDto.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/OpcItemDto.java index 4563762..bd71a09 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/opc/OpcItemDto.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/OpcItemDto.java @@ -14,6 +14,7 @@ public class OpcItemDto { private String opc_plc_code; private String item_code; private Object item_value; + private Object his_item_value; private Boolean need_log = Boolean.valueOf(false); private List relate_items = new ArrayList(); diff --git a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LogMessageConstant.java b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LogMessageConstant.java new file mode 100644 index 0000000..ca7f941 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LogMessageConstant.java @@ -0,0 +1,44 @@ +package org.nl.modules.lucene.common; + +/** + * @Author: lyd + * @Description: 定义lucene相关常量 + * @Date: 2023/8/25 + */ +public class LogMessageConstant { + /** 级别 */ + public final static String FIELD_LEVEL = "level"; + /** 时间 */ + public final static String FIELD_TIMESTAMP = "timestamp"; + /** 类的限定名 */ + public final static String FIELD_CLASS_NAME = "logger"; + /** 线程名 */ + public final static String FIELD_THREAD = "thread"; + /** 日志内容 */ + public final static String FIELD_MESSAGE = "message"; + public final static String FIELD_TRACEID = "tlogTraceId"; + // 定义颜色值 + /** 文本颜色:黑色 */ + public final static String COLOR_BLACK = "\u001B[30m"; + /** 文本颜色:红色 */ + public final static String COLOR_RED = "\u001B[31m"; + /** 文本颜色:绿色 */ + public final static String COLOR_GREEN = "\u001B[32m"; + /** 文本颜色:黄色 */ + public final static String COLOR_YELLOW = "\u001B[33m"; + /** 文本颜色:蓝色 */ + public final static String COLOR_BLUE = "\u001B[34m"; + /** 文本颜色:品红色 */ + public final static String COLOR_MAGENTA = "\u001B[35m"; + /** 文本颜色:青色 */ + public final static String COLOR_CYAN = "\u001B[36m"; + /** 文本颜色:白色 */ + public final static String COLOR_WHITE = "\u001B[37m"; + /** 文本颜色重置 */ + public final static String COLOR_RESET = "\u001B[0m"; + /** 背景颜色:黄色 */ + public final static String BACKGROUND_YELLOW = "\u001B[43m"; + + /** 索引路径 */ + public final static String INDEX_DIR = "D:\\lucene\\index"; +} diff --git a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneAppender.java b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneAppender.java new file mode 100644 index 0000000..56dd6ca --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneAppender.java @@ -0,0 +1,123 @@ +package org.nl.modules.lucene.common; +/** + * @author ldjun + * @version 1.0 + * @date 2023年08月24日 13:00 + * @desc desc + */ + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.AppenderBase; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.document.*; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.nl.acs.opc.OpcItemDto; +import org.nl.modules.lucene.enums.LogTypeEnum; +import org.nl.modules.lucene.service.LuceneExecuteLogService; +import org.nl.modules.lucene.service.dto.LuceneLogDto; +import org.nl.modules.wql.util.SpringContextHolder; +import org.wltea.analyzer.lucene.IKAnalyzer; + +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Map; + +public class LuceneAppender extends AppenderBase { + + private Directory index; + private IndexWriter indexWriter; + + + @Override + public void start() { + super.start(); + try { + index = FSDirectory.open(Paths.get(LogMessageConstant.INDEX_DIR)); + } catch (IOException e) { + e.printStackTrace(); + } + + // 初始化 Lucene 索引 + Analyzer analyzer = new IKAnalyzer(); + IndexWriterConfig config = new IndexWriterConfig(analyzer); + try { + indexWriter = new IndexWriter(index, config); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + @Override + protected void append(ILoggingEvent event) { + String message = event.getFormattedMessage(); + try { + LuceneLogDto luceneLogDto = JSONObject.parseObject(message, LuceneLogDto.class); + +// LuceneLogDto luceneLogDto = new LuceneLogDto(itemDto.getOpc_server_code(), itemDto.getOpc_plc_code(), itemDto.getDevice_code(), itemDto.getItem_code().substring(itemDto.getItem_code().lastIndexOf(".") + 1), +// String.valueOf(itemDto.getHis_item_value()), String.valueOf(itemDto.getItem_value())); +// luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); + //IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter(); + //创建一个Document对象 + Document document = new Document(); + try { + //记录索引开始时间 + long startTime = System.currentTimeMillis(); + //向document对象中添加域。 + if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) { + document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); +// document.add(new TextField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getContent())) { + document.add(new StringField("fieldContent", luceneLogDto.getContent(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getMethod())) { + document.add(new StringField("method", luceneLogDto.getMethod(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getStatus_code())) { + document.add(new StringField("status_code", luceneLogDto.getStatus_code(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getRequestparam())) { + document.add(new StringField("requestparam", luceneLogDto.getRequestparam(), Field.Store.YES)); + } + if (ObjectUtil.isNotEmpty(luceneLogDto.getResponseparam())) { + document.add(new StringField("responseparam", luceneLogDto.getResponseparam(), Field.Store.YES)); + } + document.add(new StringField("logType", luceneLogDto.getLogType(), Field.Store.YES)); + document.add(new StringField("logTime", DateUtil.format(new DateTime(), "yyyy-MM-dd HH:mm:ss.SSS"), Field.Store.YES)); + document.add(new NumericDocValuesField("logTime",System.currentTimeMillis()));//排序 + //记录索引结束时间 + long endTime = System.currentTimeMillis(); + // log.info("建立索引共耗时{}毫秒", endTime - startTime); + + try { + indexWriter.addDocument(document); + indexWriter.commit(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (Exception e) { + return; + } + } catch (Exception e){ + return; + } + } + + @Override + public void stop() { + super.stop(); + try { + indexWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneIndexWriter.java b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneIndexWriter.java index df7c4eb..c37e2f5 100644 --- a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneIndexWriter.java +++ b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/LuceneIndexWriter.java @@ -16,45 +16,45 @@ import java.text.SimpleDateFormat; import java.util.Date; public class LuceneIndexWriter { - private static IndexWriter indexWriter; - - static { - try { - Directory directory = FSDirectory.open(new File(UrlConfig.luceneUrl).toPath()); - IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer()); - indexWriter = new IndexWriter(directory, config); - } catch (Exception e) { - e.printStackTrace(); - } - /**当当前线程结束时,自动关闭IndexWriter,使用Runtime对象*/ - Runtime.getRuntime().addShutdownHook(new Thread(){ - @Override - public void run() { - try { - closeIndexWriter(); - } catch (Exception e) { - e.printStackTrace(); - } - } - }); - } - /**在线程结束时,自动关闭IndexWriter*/ - public static IndexWriter getIndexWriter() { - return indexWriter; - } - /**关闭IndexWriter - * @throws IOException - * @throws CorruptIndexException */ - public static void closeIndexWriter() throws Exception { - if(indexWriter != null) { - indexWriter.close(); - } - } - - public static void main(String[] args) throws IOException { - indexWriter.deleteAll(); - } - +// private static IndexWriter indexWriter; +// +// static { +// try { +// Directory directory = FSDirectory.open(new File(UrlConfig.luceneUrl).toPath()); +// IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer()); +// indexWriter = new IndexWriter(directory, config); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// /**当当前线程结束时,自动关闭IndexWriter,使用Runtime对象*/ +// Runtime.getRuntime().addShutdownHook(new Thread(){ +// @Override +// public void run() { +// try { +// closeIndexWriter(); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// }); +// } +// /**在线程结束时,自动关闭IndexWriter*/ +// public static IndexWriter getIndexWriter() { +// return indexWriter; +// } +// /**关闭IndexWriter +// * @throws IOException +// * @throws CorruptIndexException */ +// public static void closeIndexWriter() throws Exception { +// if(indexWriter != null) { +// indexWriter.close(); +// } +// } +// +// public static void main(String[] args) throws IOException { +// indexWriter.deleteAll(); +// } +// public static String getDate(String timeString) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");//时间格式 Date date = sdf.parse(timeString); @@ -62,5 +62,4 @@ public class LuceneIndexWriter { return timeString; } - } diff --git a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/Searcher.java b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/Searcher.java index 19ce19f..9cd0af2 100644 --- a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/Searcher.java +++ b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/common/Searcher.java @@ -36,6 +36,7 @@ public class Searcher { //构建IndexSearcher IndexSearcher searcher = new IndexSearcher(reader); //标准分词器,会自动去掉空格啊,is a the等单词 + Analyzer analyzer = new IKAnalyzer(true); // Analyzer analyzer = new StandardAnalyzer(); // Analyzer analyzer = new IKAnalyzer(false); //查询解析器 @@ -49,7 +50,7 @@ public class Searcher { int start = pageNum * pageSize;// 当前页的起始条数 int end = start + pageSize;// 当前页的结束条数(不能包含) // 创建排序对象,需要排序字段SortField,参数:字段的名称、字段的类型、是否反转如果是false,升序。true降序 - Sort sort = new Sort(new SortField("logTime", SortField.Type.DOC,true)); + Sort sort = new Sort(new SortField("logTime", SortField.Type.LONG,true)); TopDocs docs = null; BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); diff --git a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/service/impl/LuceneExecuteLogServiceImpl.java b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/service/impl/LuceneExecuteLogServiceImpl.java index c97f22b..70619e6 100644 --- a/acs/nladmin-system/src/main/java/org/nl/modules/lucene/service/impl/LuceneExecuteLogServiceImpl.java +++ b/acs/nladmin-system/src/main/java/org/nl/modules/lucene/service/impl/LuceneExecuteLogServiceImpl.java @@ -3,6 +3,7 @@ package org.nl.modules.lucene.service.impl; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -35,57 +36,57 @@ public class LuceneExecuteLogServiceImpl implements LuceneExecuteLogService { String now = DateUtil.now(); } - @SneakyThrows @Override public void deviceExecuteLog(LuceneLogDto luceneLogDto) { luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc()); - addIndex(luceneLogDto); + log.info("{}", JSON.toJSONString(luceneLogDto)); } @Override public void interfaceExecuteLog(LuceneLogDto luceneLogDto) { luceneLogDto.setLogType(LogTypeEnum.INTERFACE_LOG.getDesc()); - addIndex(luceneLogDto); +// addIndex(luceneLogDto); + log.info("{}", JSON.toJSONString(luceneLogDto)); } private void addIndex(LuceneLogDto luceneLogDto) { - IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter(); - //创建一个Document对象 - Document document = new Document(); - try { - //记录索引开始时间 - long startTime = System.currentTimeMillis(); - //向document对象中添加域。 - if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) { - document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); -// document.add(new TextField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getContent())) { - document.add(new StringField("fieldContent", luceneLogDto.getContent(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getMethod())) { - document.add(new StringField("method", luceneLogDto.getMethod(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getStatus_code())) { - document.add(new StringField("status_code", luceneLogDto.getStatus_code(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getRequestparam())) { - document.add(new StringField("requestparam", luceneLogDto.getRequestparam(), Field.Store.YES)); - } - if (ObjectUtil.isNotEmpty(luceneLogDto.getResponseparam())) { - document.add(new StringField("responseparam", luceneLogDto.getResponseparam(), Field.Store.YES)); - } - document.add(new StringField("logType", luceneLogDto.getLogType(), Field.Store.YES)); - document.add(new StringField("logTime", DateUtil.format(new DateTime(), "yyyy-MM-dd HH:mm:ss.SSS"), Field.Store.YES)); - indexWriter.addDocument(document); - //记录索引结束时间 - long endTime = System.currentTimeMillis(); - log.info("建立索引共耗时{}毫秒", endTime - startTime); - indexWriter.commit(); - MDC.put("DEVICECODE", luceneLogDto.getDevice_code()); - } catch (Exception e) { - log.error(e.getMessage(), e); - } +// IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter(); +// //创建一个Document对象 +// Document document = new Document(); +// try { +// //记录索引开始时间 +// long startTime = System.currentTimeMillis(); +// //向document对象中添加域。 +// if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())) { +// document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); +//// document.add(new TextField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES)); +// } +// if (ObjectUtil.isNotEmpty(luceneLogDto.getContent())) { +// document.add(new StringField("fieldContent", luceneLogDto.getContent(), Field.Store.YES)); +// } +// if (ObjectUtil.isNotEmpty(luceneLogDto.getMethod())) { +// document.add(new StringField("method", luceneLogDto.getMethod(), Field.Store.YES)); +// } +// if (ObjectUtil.isNotEmpty(luceneLogDto.getStatus_code())) { +// document.add(new StringField("status_code", luceneLogDto.getStatus_code(), Field.Store.YES)); +// } +// if (ObjectUtil.isNotEmpty(luceneLogDto.getRequestparam())) { +// document.add(new StringField("requestparam", luceneLogDto.getRequestparam(), Field.Store.YES)); +// } +// if (ObjectUtil.isNotEmpty(luceneLogDto.getResponseparam())) { +// document.add(new StringField("responseparam", luceneLogDto.getResponseparam(), Field.Store.YES)); +// } +// document.add(new StringField("logType", luceneLogDto.getLogType(), Field.Store.YES)); +// document.add(new StringField("logTime", DateUtil.format(new DateTime(), "yyyy-MM-dd HH:mm:ss.SSS"), Field.Store.YES)); +// indexWriter.addDocument(document); +// //记录索引结束时间 +// long endTime = System.currentTimeMillis(); +// log.info("建立索引共耗时{}毫秒", endTime - startTime); +// indexWriter.commit(); +// MDC.put("DEVICECODE", luceneLogDto.getDevice_code()); +// } catch (Exception e) { +// log.error(e.getMessage(), e); +// } } @Override diff --git a/acs/nladmin-system/src/main/resources/log/Lucene.xml b/acs/nladmin-system/src/main/resources/log/Lucene.xml index b53961d..f00bdbd 100644 --- a/acs/nladmin-system/src/main/resources/log/Lucene.xml +++ b/acs/nladmin-system/src/main/resources/log/Lucene.xml @@ -23,12 +23,19 @@ + + + + + 512 + - + + diff --git a/acs/nladmin-system/src/main/resources/logback-spring.xml b/acs/nladmin-system/src/main/resources/logback-spring.xml index 85bbca8..a5017fc 100644 --- a/acs/nladmin-system/src/main/resources/logback-spring.xml +++ b/acs/nladmin-system/src/main/resources/logback-spring.xml @@ -57,10 +57,17 @@ https://juejin.cn/post/6844903775631572999 0 - 500 + 200 + + + + + 512 + + 1000 @@ -85,10 +92,12 @@ https://juejin.cn/post/6844903775631572999 + + @@ -137,10 +146,12 @@ https://juejin.cn/post/6844903775631572999 + +