From b7a7f9b3635b0b63f89744543408d50dd44e4fd4 Mon Sep 17 00:00:00 2001 From: ludj Date: Tue, 7 Feb 2023 11:25:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=BE=E8=B7=AF=E8=BF=BD=E8=B8=AA=E5=92=8Cel?= =?UTF-8?q?asticsearch=E6=95=B4=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nladmin-system/nlsso-server/pom.xml | 47 +++--- .../org/nl/ElasticSearchClientConfig.java | 68 +++++++++ .../main/java/org/nl/common/LogMdcFilter.java | 45 ++++++ .../common/annotation/InterfaceLogType.java | 29 ++++ .../java/org/nl/common/annotation/Log.java | 30 ++++ .../java/org/nl/common/aspect/LogAspect.java | 137 ++++++++++++++++++ .../param/impl/SysParamServiceImpl.java | 1 + .../src/main/resources/banner.txt | 12 +- .../src/main/resources/config/application.yml | 13 +- .../src/main/resources/logback-spring.xml | 75 ++++++++-- 10 files changed, 410 insertions(+), 47 deletions(-) create mode 100644 nladmin-system/nlsso-server/src/main/java/org/nl/ElasticSearchClientConfig.java create mode 100644 nladmin-system/nlsso-server/src/main/java/org/nl/common/LogMdcFilter.java create mode 100644 nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/InterfaceLogType.java create mode 100644 nladmin-system/nlsso-server/src/main/java/org/nl/common/aspect/LogAspect.java diff --git a/nladmin-system/nlsso-server/pom.xml b/nladmin-system/nlsso-server/pom.xml index 8f5154e..a84931e 100644 --- a/nladmin-system/nlsso-server/pom.xml +++ b/nladmin-system/nlsso-server/pom.xml @@ -33,6 +33,23 @@ + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.6.1 + + + org.elasticsearch.client + elasticsearch-rest-client + 7.6.1 + + + org.elasticsearch + elasticsearch + 7.6.1 + + + cn.dynamictp dynamic-tp-spring-boot-starter-common @@ -96,25 +113,6 @@ - - - com.plumelog - plumelog-lite-spring-boot-starter - 3.5.3 - - - plumelog-core - com.plumelog - - - - - - com.plumelog - plumelog-trace - 3.5.2 - - org.redisson redisson-spring-boot-starter @@ -167,11 +165,12 @@ mybatis-plus-generator 3.4.0 - - - - - + + com.internetitem + logback-elasticsearch-appender + 1.6 + + cn.dev33 diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/ElasticSearchClientConfig.java b/nladmin-system/nlsso-server/src/main/java/org/nl/ElasticSearchClientConfig.java new file mode 100644 index 0000000..c4974de --- /dev/null +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/ElasticSearchClientConfig.java @@ -0,0 +1,68 @@ +package org.nl; + +import org.apache.http.HttpHost; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.IOException; +import java.util.Map; + +/** + * @author ldjun + * @version 1.0 + * @date 2023年02月06日 18:49 + * @desc desc + */ +@Configuration +public class ElasticSearchClientConfig { + + + //配置RestHighLevelClient依赖到spring容器中待用 + @Bean + public RestHighLevelClient restHighLevelClient() { + RestHighLevelClient client = new RestHighLevelClient( + RestClient.builder( + //绑定本机,端口,协议,如果是ES集群,就配置多个 + new HttpHost("127.0.0.1", 9200, "http"))); + + return client; + } + + public static void main(String[] args) throws IOException { + // 指定ip 端口 + HttpHost[] httpHosts = {new HttpHost("47.111.78.178", 27017, "http")}; + RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHosts)); + + SearchRequest searchRequest = new SearchRequest("logs-2023-02-06"); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); + sourceBuilder.query(QueryBuilders.matchQuery("_id", "HzAeJoYBlkwLvExN1Vg4")); + searchRequest.source(sourceBuilder); + SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); + RestStatus restStatus = searchResponse.status(); + System.out.println(restStatus); + if (restStatus == RestStatus.OK) { + SearchHits searchHits = searchResponse.getHits(); + for (SearchHit searchHit: searchHits) { + System.out.println("id:" + searchHit.getId()); + System.out.println("index:" + searchHit.getIndex()); + System.out.println("score:" + searchHit.getScore()); + Map map = searchHit.getSourceAsMap(); + System.out.println("name:" + (String) map.get("name")); + System.out.println("city:" + (String) map.get("city")); + System.out.println("price:" + (Double) map.get("price")); + } + } + restHighLevelClient.close(); + } +} + diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/common/LogMdcFilter.java b/nladmin-system/nlsso-server/src/main/java/org/nl/common/LogMdcFilter.java new file mode 100644 index 0000000..bf33136 --- /dev/null +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/common/LogMdcFilter.java @@ -0,0 +1,45 @@ +package org.nl.common; + +/** + * @author ldjun + * @version 1.0 + * @date 2023年02月07日 11:05 + * @desc desc + */ + +import org.nl.common.utils.IdUtil; +import org.slf4j.MDC; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import java.io.IOException; +@WebFilter(urlPatterns = "/*", filterName = "logMdcFilter") +public class LogMdcFilter implements Filter { + private static final String UNIQUE_ID = "traceId"; + + @Override + public void init(FilterConfig filterConfig) { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + boolean bInsertMDC = insertMDC(); + try { + chain.doFilter(request, response); + } finally { + if(bInsertMDC) { + MDC.remove(UNIQUE_ID); + } + } + } + + @Override + public void destroy() { + } + + private boolean insertMDC() { + String uniqueId = IdUtil.getStringId(); + MDC.put(UNIQUE_ID, uniqueId); + return true; + } +} diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/InterfaceLogType.java b/nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/InterfaceLogType.java new file mode 100644 index 0000000..cfada7f --- /dev/null +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/InterfaceLogType.java @@ -0,0 +1,29 @@ +package org.nl.common.annotation; + +/** + * @author ldjun + * @version 1.0 + * @date 2023年02月07日 10:27 + * @desc desc + */ +public enum InterfaceLogType { + DEFAULT("默认"), + LMS_TO_MES("LMS请求MES"), + MES_TO_LMS("MES请求LMS"), + LMS_TO_CRM("LMS请求CRM"), + CRM_TO_LMS("CRM请求LMS"), + LMS_TO_SAP("LMS请求SAP"), + SAP_TO_LMS("SAP请求LMS"), + LMS_TO_ACS("LMS请求ACS"), + ACS_TO_LMS("ACS请求LMS"); + + private String desc; + + InterfaceLogType(String desc) { + this.desc=desc; + } + + public String getDesc() { + return desc; + } +} diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/Log.java b/nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/Log.java index a6e8280..2794c2c 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/Log.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/Log.java @@ -28,4 +28,34 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) public @interface Log { String value() default ""; + + /** + * 是否打印到日志文件 + * + * @return + */ + boolean isPrintToLogFile() default false; + + + /** + * 是否插入操作日志表 + * + * @return + */ + boolean isAddLogTable() default true; + + /** + * 是否接口日志 + * + * @return + */ + boolean isInterfaceLog() default false; + + /** + * 接口日志类型 + * + * @return + */ + InterfaceLogType interfaceLogType() default InterfaceLogType.DEFAULT; } + diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/common/aspect/LogAspect.java b/nladmin-system/nlsso-server/src/main/java/org/nl/common/aspect/LogAspect.java new file mode 100644 index 0000000..dae0b4c --- /dev/null +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/common/aspect/LogAspect.java @@ -0,0 +1,137 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.nl.common.aspect; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.nl.common.utils.SecurityUtils; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; + +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.*; + +/** + * @author Zheng Jie + * @date 2018-11-24 + */ +@Component +@Aspect +@Slf4j +public class LogAspect { + + + ThreadLocal currentTime = new ThreadLocal<>(); + + + /** + * 配置切入点 + */ + @Pointcut("@annotation(org.nl.common.annotation.Log)") + public void logPointcut() { + // 该方法无方法体,主要为了让同类中其他方法使用此切入点 + } + + /** + * 配置环绕通知,使用在方法logPointcut()上注册的切入点 + * + * @param joinPoint join point for advice + */ + @Around("logPointcut()") + public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { + String trackId = UUID.randomUUID().toString(); + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + // 方法路径 + String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; + String params = getParameter(method, joinPoint.getArgs()); + + org.nl.common.annotation.Log logInfo = method.getAnnotation(org.nl.common.annotation.Log.class); + + //是否输出到日志文件 + if (logInfo.isPrintToLogFile()) { + log.info("track_id:{},请求方法:{},请求方法参数:{}",trackId,methodName,params); + } + Object result; + currentTime.set(System.currentTimeMillis()); + try { + result = joinPoint.proceed(); +// log.info("返回结果:"+JSONObject.parse(((ResponseEntity) result).getBody().toString())); + }catch (Exception ex){ + log.error("track_id:{},error:{}",trackId,ex.getMessage()); + throw ex; + } + return result; + } + + /** + * 根据方法和传入的参数获取请求参数 + */ + private String getParameter(Method method, Object[] args) { + List argList = new ArrayList<>(); + Parameter[] parameters = method.getParameters(); + for (int i = 0; i < parameters.length; i++) { + //将RequestBody注解修饰的参数作为请求参数 + RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); + if (requestBody != null) { + argList.add(args[i]); + } + //将RequestParam注解修饰的参数作为请求参数 + RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); + if (requestParam != null) { + Map map = new HashMap<>(); + String key = parameters[i].getName(); + if (!StrUtil.isEmpty(requestParam.value())) { + key = requestParam.value(); + } + map.put(key, args[i]); + argList.add(map); + } + } + if (argList.size() == 0) { + return ""; + } + return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList); + } + + /** + * 配置异常通知 + * + * @param joinPoint join point for advice + * @param e exception + */ +// @AfterThrowing(pointcut = "logPointcut()", throwing = "e") + public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { +// logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log); + } + + public String getUsername() { + try { + return SecurityUtils.getCurrentUsername(); + } catch (Exception e) { + return ""; + } + } +} diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/param/impl/SysParamServiceImpl.java b/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/param/impl/SysParamServiceImpl.java index db4f2ff..cefd376 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/param/impl/SysParamServiceImpl.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/param/impl/SysParamServiceImpl.java @@ -39,6 +39,7 @@ public class SysParamServiceImpl extends ServiceImpl impl @Override public Page queryPage(Map whereJson, PageQuery page) { + log.info("111"); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.orderBy(true, true, "create_time"); Page paramPage = paramMapper.selectPage(page.build(), queryWrapper); diff --git a/nladmin-system/nlsso-server/src/main/resources/banner.txt b/nladmin-system/nlsso-server/src/main/resources/banner.txt index 8be4ead..bec72fe 100644 --- a/nladmin-system/nlsso-server/src/main/resources/banner.txt +++ b/nladmin-system/nlsso-server/src/main/resources/banner.txt @@ -1,8 +1,8 @@ - _ _ ___________ _ _____ _ ___________ _____ -| \ | | _ | ___ \ | | ___| | | ___| ___|_ _| -| \| | | | | |_/ / | | |__ | | | |__ | |_ | | -| . ` | | | | ___ \ | | __|| | | __|| _| | | -| |\ \ \_/ / |_/ / |____| |___| |____| |___| | | | -\_| \_/\___/\____/\_____/\____/\_____/\____/\_| \_/ + _ _ ___________ _ _____ _ ___________ _____ +| \ | | _ | ___ \ | | ___| | |_ _| ___|_ _| +| \| | | | | |_/ / | | |__ | | | | | |_ | | +| . ` | | | | ___ \ | | __|| | | | | _| | | +| |\ \ \_/ / |_/ / |____| |___| |_____| |_| | | | +\_| \_/\___/\____/\_____/\____/\_____/\___/\_| \_/ :: Spring Boot :: (v2.1.0.RELEASE) \ No newline at end of file diff --git a/nladmin-system/nlsso-server/src/main/resources/config/application.yml b/nladmin-system/nlsso-server/src/main/resources/config/application.yml index 2b953ef..6dae405 100644 --- a/nladmin-system/nlsso-server/src/main/resources/config/application.yml +++ b/nladmin-system/nlsso-server/src/main/resources/config/application.yml @@ -60,7 +60,6 @@ security: - /api/localStorage/pictures # 参数 - /api/param/getValueByCode - - /plumelog/** mybatis-plus: configuration: map-underscore-to-camel-case: true @@ -70,17 +69,15 @@ mybatis-plus: global-config: db-config: id-type: INPUT -plumelog: - model: redis #值为4种 redis,kafka,rest,restServer,lite - lite: - log: - path: /lucene2 - + banner: false arthas: agent-id: hsehdfsfghhwertyfad tunnel-server: ws://127.0.0.1:7777/ws enable-detail-pages: true ip: 127.0.0.1 telnetPort: 7777 - +management: + health: + elasticsearch: + enabled: false #取消对elasticsearch的检查 https://www.codeleading.com/article/60643988608/ diff --git a/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml b/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml index 30f4ff5..ee587ee 100644 --- a/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml +++ b/nladmin-system/nlsso-server/src/main/resources/logback-spring.xml @@ -12,7 +12,7 @@ https://juejin.cn/post/6844903775631572999 nlAdmin + value="%black(%contextName-) %X{traceId} %red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %gray(%msg%n)"/> @@ -55,24 +55,81 @@ https://juejin.cn/post/6844903775631572999 - - plumelog - - /plumelog/lite - - 30 - sleuth + + http://47.111.78.178:27017/_bulk + logs-%date{yyyy-MM-dd} + tester + es-logger + es-error-logger + 30000 + false + false + false + 104857600 + 3 + 30000 + 250 + false + false + 5 + + + + host + ${HOSTNAME} + false + + + severity + %level + + + thread + %thread + + + stacktrace + %ex + + + logger + %logger + + + traceId + %X{traceId} + + + +
+ Content-Type + application/json +
+
+ - + + + + + + + + + + + + +