Browse Source

链路追踪和elasticsearch整合

master
管理员 2 years ago
parent
commit
b7a7f9b363
  1. 47
      nladmin-system/nlsso-server/pom.xml
  2. 68
      nladmin-system/nlsso-server/src/main/java/org/nl/ElasticSearchClientConfig.java
  3. 45
      nladmin-system/nlsso-server/src/main/java/org/nl/common/LogMdcFilter.java
  4. 29
      nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/InterfaceLogType.java
  5. 30
      nladmin-system/nlsso-server/src/main/java/org/nl/common/annotation/Log.java
  6. 137
      nladmin-system/nlsso-server/src/main/java/org/nl/common/aspect/LogAspect.java
  7. 1
      nladmin-system/nlsso-server/src/main/java/org/nl/system/service/param/impl/SysParamServiceImpl.java
  8. 10
      nladmin-system/nlsso-server/src/main/resources/banner.txt
  9. 13
      nladmin-system/nlsso-server/src/main/resources/config/application.yml
  10. 75
      nladmin-system/nlsso-server/src/main/resources/logback-spring.xml

47
nladmin-system/nlsso-server/pom.xml

@ -33,6 +33,23 @@
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.1</version>
</dependency>
<dependency>
<groupId>cn.dynamictp</groupId>
<artifactId>dynamic-tp-spring-boot-starter-common</artifactId>
@ -96,25 +113,6 @@
</exclusion>
</exclusions>
</dependency>
<!--日志管理-->
<dependency>
<groupId>com.plumelog</groupId>
<artifactId>plumelog-lite-spring-boot-starter</artifactId>
<version>3.5.3</version>
<exclusions>
<exclusion>
<artifactId>plumelog-core</artifactId>
<groupId>com.plumelog</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.plumelog</groupId>
<artifactId>plumelog-trace</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
@ -167,11 +165,12 @@
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<!--Spring boot 安全框架-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.internetitem</groupId>
<artifactId>logback-elasticsearch-appender</artifactId>
<version>1.6</version>
</dependency>
<!-- Sa-Token 权限认证 安全框架, 在线文档:http://sa-token.dev33.cn/ -->
<dependency>
<groupId>cn.dev33</groupId>

68
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<String, Object> 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();
}
}

45
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;
}
}

29
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;
}
}

30
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;
}

137
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<Long> 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<Object> 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<String, Object> 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 "";
}
}
}

1
nladmin-system/nlsso-server/src/main/java/org/nl/system/service/param/impl/SysParamServiceImpl.java

@ -39,6 +39,7 @@ public class SysParamServiceImpl extends ServiceImpl<SysParamMapper, Param> impl
@Override
public Page<Param> queryPage(Map whereJson, PageQuery page) {
log.info("111");
QueryWrapper<Param> queryWrapper = new QueryWrapper<>();
queryWrapper.orderBy(true, true, "create_time");
Page<Param> paramPage = paramMapper.selectPage(page.build(), queryWrapper);

10
nladmin-system/nlsso-server/src/main/resources/banner.txt

@ -1,8 +1,8 @@
_ _ ___________ _ _____ _ ___________ _____
| \ | | _ | ___ \ | | ___| | | ___| ___|_ _|
| \| | | | | |_/ / | | |__ | | | |__ | |_ | |
| . ` | | | | ___ \ | | __|| | | __|| _| | |
| |\ \ \_/ / |_/ / |____| |___| |____| |___| | | |
\_| \_/\___/\____/\_____/\____/\_____/\____/\_| \_/
| \ | | _ | ___ \ | | ___| | |_ _| ___|_ _|
| \| | | | | |_/ / | | |__ | | | | | |_ | |
| . ` | | | | ___ \ | | __|| | | | | _| | |
| |\ \ \_/ / |_/ / |____| |___| |_____| |_| | | |
\_| \_/\___/\____/\_____/\____/\_____/\___/\_| \_/
:: Spring Boot :: (v2.1.0.RELEASE)

13
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/

75
nladmin-system/nlsso-server/src/main/resources/logback-spring.xml

@ -12,7 +12,7 @@ https://juejin.cn/post/6844903775631572999
<contextName>nlAdmin</contextName>
<property name="log.charset" value="utf-8"/>
<property name="log.pattern"
value="%black(%contextName-) %red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %gray(%msg%n)"/>
value="%black(%contextName-) %X{traceId} %red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %gray(%msg%n)"/>
<springProperty scope="context" name="logPath" source="logging.file.path" defaultValue="logs"/>
<property name="SYSTEM_NAME" value="${systemName}"/>
<property name="LOG_HOME" value="${logPath}"/>
@ -55,24 +55,81 @@ https://juejin.cn/post/6844903775631572999
<appender-ref ref="FILE"/>
</appender>
<appender name="plumelog" class="com.plumelog.lite.logback.appender.LiteAppender">
<appName>plumelog</appName>
<!-- 日志存储位置 -->
<logPath>/plumelog/lite</logPath>
<!-- 日志保留天数 -->
<keepDay>30</keepDay>
<expand>sleuth</expand>
<appender name="plumelog" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
<url>http://47.111.78.178:27017/_bulk</url>
<index>logs-%date{yyyy-MM-dd}</index>
<type>tester</type>
<loggerName>es-logger</loggerName> <!-- optional -->
<errorLoggerName>es-error-logger</errorLoggerName> <!-- optional -->
<connectTimeout>30000</connectTimeout> <!-- optional (in ms, default 30000) -->
<errorsToStderr>false</errorsToStderr> <!-- optional (default false) -->
<includeCallerData>false</includeCallerData> <!-- optional (default false) -->
<logsToStderr>false</logsToStderr> <!-- optional (default false) -->
<maxQueueSize>104857600</maxQueueSize> <!-- optional (default 104857600) -->
<maxRetries>3</maxRetries> <!-- optional (default 3) -->
<readTimeout>30000</readTimeout> <!-- optional (in ms, default 30000) -->
<sleepTime>250</sleepTime> <!-- optional (in ms, default 250) -->
<rawJsonMessage>false</rawJsonMessage> <!-- optional (default false) -->
<includeMdc>false</includeMdc> <!-- optional (default false) -->
<maxMessageSize>5</maxMessageSize> <!-- optional (default -1 -->
<!-- <authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" /> &lt;!&ndash; optional &ndash;&gt;-->
<properties>
<property>
<name>host</name>
<value>${HOSTNAME}</value>
<allowEmpty>false</allowEmpty>
</property>
<property>
<name>severity</name>
<value>%level</value>
</property>
<property>
<name>thread</name>
<value>%thread</value>
</property>
<property>
<name>stacktrace</name>
<value>%ex</value>
</property>
<property>
<name>logger</name>
<value>%logger</value>
</property>
<property>
<name>traceId</name>
<value>%X{traceId}</value>
</property>
</properties>
<headers>
<header>
<name>Content-Type</name>
<value>application/json</value>
</header>
</headers>
</appender>
<!--开发环境:打印控制台-->
<springProfile name="dev">
<root level="debug">
<appender-ref ref="CONSOLE"/>
<!-- <appender-ref ref="plumelog" />-->
<appender-ref ref="plumelog" />
</root>
<logger name="org.springframework" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.mybatis" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="com.baomidou" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="com.dtp" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="es-logger" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.apache" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>

Loading…
Cancel
Save