管理员
2 years ago
10 changed files with 410 additions and 47 deletions
@ -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(); |
||||
|
} |
||||
|
} |
||||
|
|
@ -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; |
||||
|
} |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
} |
@ -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,8 +1,8 @@ |
|||||
_ _ ___________ _ _____ _ ___________ _____ |
_ _ ___________ _ _____ _ ___________ _____ |
||||
| \ | | _ | ___ \ | | ___| | | ___| ___|_ _| |
| \ | | _ | ___ \ | | ___| | |_ _| ___|_ _| |
||||
| \| | | | | |_/ / | | |__ | | | |__ | |_ | | |
| \| | | | | |_/ / | | |__ | | | | | |_ | | |
||||
| . ` | | | | ___ \ | | __|| | | __|| _| | | |
| . ` | | | | ___ \ | | __|| | | | | _| | | |
||||
| |\ \ \_/ / |_/ / |____| |___| |____| |___| | | | |
| |\ \ \_/ / |_/ / |____| |___| |_____| |_| | | | |
||||
\_| \_/\___/\____/\_____/\____/\_____/\____/\_| \_/ |
\_| \_/\___/\____/\_____/\____/\_____/\___/\_| \_/ |
||||
|
|
||||
:: Spring Boot :: (v2.1.0.RELEASE) |
:: Spring Boot :: (v2.1.0.RELEASE) |
Loading…
Reference in new issue