feat:SpringBoot整合HighLevelClient实现登录统计

This commit is contained in:
haoxr 2021-03-06 23:56:44 +08:00
parent c218a10547
commit efdbf42214
7 changed files with 63 additions and 34 deletions

View File

@ -1,11 +1,16 @@
package com.youlai.admin;
import com.youlai.common.elasticsearch.service.ElasticSearchService;
import com.youlai.common.web.pojo.domain.LoginLog;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
@Slf4j
public class ElasticSearchTests {
@ -15,6 +20,7 @@ public class ElasticSearchTests {
@Test
public void search() {
elasticSearchService.search(null,null,"logstash-login-2021.03.05");
List<LoginLog> list = elasticSearchService.search(null, LoginLog.class, "youlai-auth-login-2021-03-06");
System.out.println(list.toString());
}
}

View File

@ -39,10 +39,11 @@
<artifactId>elasticsearch-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
</project>
</project>

View File

@ -4,8 +4,8 @@ package com.youlai.common.elasticsearch.constant;
* @author hxr
* @date 2021-03-05
*/
public interface ElasticSearchConstants {
public interface EsConstants {
int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
int DEFAULT_PAGE_SIZE = 10;
}

View File

@ -2,13 +2,15 @@ package com.youlai.common.elasticsearch.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.youlai.common.elasticsearch.constant.ElasticSearchConstants;
import com.youlai.common.elasticsearch.constant.EsConstants;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
@ -23,28 +25,40 @@ import java.util.List;
*/
@Service
@AllArgsConstructor
public class ElasticSearchService<T> {
public class ElasticSearchService{
private RestHighLevelClient client;
@SneakyThrows
public SearchResponse search(SearchRequest searchRequest) {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse;
public long count(QueryBuilder queryBuilder,String... indices){
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
CountRequest countRequest=new CountRequest(indices);
countRequest.source(searchSourceBuilder);
CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
long count = countResponse.getCount();
return count;
}
@SneakyThrows
public List search(QueryBuilder queryBuilder, Class<T> clazz, String... indexes) {
SearchRequest searchRequest = new SearchRequest(indexes);
@SneakyThrows
public <T> List<T> search(QueryBuilder queryBuilder, Class<T> clazz, String... indices) {
// 构造请求参数
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.from(0);
searchSourceBuilder.size(ElasticSearchConstants.DEFAULT_PAGE_SIZE);
searchSourceBuilder.size(EsConstants.DEFAULT_PAGE_SIZE);
SearchRequest searchRequest = new SearchRequest(indices);
searchRequest.source(searchSourceBuilder);
// 执行搜索请求
// 执行请求
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();

View File

@ -1,5 +1,6 @@
package com.youlai.common.web.aspect;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.youlai.common.web.pojo.domain.LoginLog;
@ -11,6 +12,7 @@ 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.slf4j.MDC;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
@ -26,7 +28,7 @@ import javax.servlet.http.HttpServletRequest;
@Component
@AllArgsConstructor
@Slf4j
@ConditionalOnProperty(value = "spring.application.name",havingValue = "youlai-auth")
@ConditionalOnProperty(value = "spring.application.name", havingValue = "youlai-auth")
public class LoginLogAspect {
@Pointcut("execution(public * com.youlai.auth.controller.AuthController.postAccessToken(..))")
@ -38,29 +40,29 @@ public class LoginLogAspect {
// 时间统计
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
// 获取方法签名
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String description = signature.getMethod().getAnnotation(ApiOperation.class).value();
// 获取请求信息
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String requestIp= ServletUtil.getClientIP(request);
String requestUrl=request.getRequestURL().toString();
String requestMethod=request.getMethod();
String clientIP = ServletUtil.getClientIP(request);
String requestUrl = request.getRequestURL().toString();
String method = request.getMethod();
MDC.put("startTime", StrUtil.toString(startTime));
MDC.put("elapsedTime", StrUtil.toString(elapsedTime));
MDC.put("description",description);
MDC.put("clientIP", clientIP);
MDC.put("url", requestUrl);
MDC.put("method", method);
log.info(StrUtil.toString(result)); // logstash收集必要打印
LoginLog loginLog = new LoginLog();
loginLog.setStartTime(startTime);
loginLog.setElapsedTime(elapsedTime);
loginLog.setDescription(description );
loginLog.setRequestIp(requestIp);
loginLog.setRequestUrl(requestUrl);
loginLog.setRequestMethod(requestMethod);
loginLog.setResult(result);
log.info(JSONUtil.toJsonStr(loginLog));
return result;
}
}

View File

@ -12,11 +12,11 @@ public class LoginLog {
private String description;
private String requestIp;
private String clientIP;
private String requestUrl;
private String url;
private String requestMethod;
private String method;
private long startTime;

View File

@ -14,7 +14,7 @@
</encoder>
</appender>
<!-- Logstash收集日志输出到ElasticSearch -->
<!-- Logstash收集登录日志输出到ElasticSearch -->
<appender name="LOGIN_LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>g.youlai.store:5044</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
@ -27,12 +27,18 @@
<pattern>
{
"project": "${APP_NAME}",
"action":"login",
"level": "%level",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"message": "%message",
"action": "login" <!-- 自定义字段 登录日志索引名 logstash-%{action}-yyyy.MM.dd -->
"startTime": "%X{startTime}",
"elapsedTime": "%X{elapsedTime}",
"description": "%X{description}",
"clientIP": "%X{clientIP}",
"url": "%X{url}",
"method": "%X{method}"
}
</pattern>
</pattern>