add:es日志
This commit is contained in:
@@ -11,6 +11,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactor
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,8 @@ spring:
|
||||
#uris: 172.31.185.110:9200,172.31.154.9:9200 #内网
|
||||
# uris: 47.96.133.178:8200 #外网
|
||||
uris: http://10.1.3.90:9200 #外网
|
||||
# username: elastic
|
||||
# password: 123456
|
||||
# username: elastic
|
||||
# password: 123456
|
||||
datasource:
|
||||
druid:
|
||||
db-type: com.alibaba.druid.pool.DruidDataSource
|
||||
@@ -64,16 +64,8 @@ spring:
|
||||
enabled: true
|
||||
url-pattern: /druid/*
|
||||
reset-enable: false
|
||||
filter:
|
||||
stat:
|
||||
enabled: true
|
||||
# 记录慢SQL
|
||||
log-slow-sql: true
|
||||
slow-sql-millis: 1000
|
||||
merge-sql: true
|
||||
wall:
|
||||
config:
|
||||
multi-statement-allow: true
|
||||
filters:
|
||||
DruidFilter,stat
|
||||
redis:
|
||||
#数据库索引
|
||||
database: ${REDIS_DB:2}
|
||||
|
||||
@@ -41,16 +41,8 @@ spring:
|
||||
reset-enable: false
|
||||
login-username: admin
|
||||
login-password: 123456
|
||||
filter:
|
||||
stat:
|
||||
enabled: true
|
||||
# 记录慢SQL
|
||||
log-slow-sql: true
|
||||
slow-sql-millis: 1000
|
||||
merge-sql: true
|
||||
wall:
|
||||
config:
|
||||
multi-statement-allow: true
|
||||
filters:
|
||||
DruidFilter,stat
|
||||
redis:
|
||||
#数据库索引
|
||||
database: ${REDIS_DB:15}
|
||||
|
||||
@@ -60,6 +60,7 @@ security:
|
||||
- /api/localStorage/pictures
|
||||
# 参数
|
||||
- /api/param/getValueByCode
|
||||
- /api/esLog/**
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
|
||||
@@ -56,7 +56,7 @@ https://juejin.cn/post/6844903775631572999
|
||||
</appender>
|
||||
|
||||
<appender name="esLogAppender" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
|
||||
<url>http://47.96.133.178:8200/_bulk</url>
|
||||
<url>http://10.1.3.90:9200/_bulk</url>
|
||||
<index>lms_log</index>
|
||||
<type>lms_log</type>
|
||||
<loggerName>es-logger</loggerName> <!-- optional -->
|
||||
@@ -76,7 +76,7 @@ https://juejin.cn/post/6844903775631572999
|
||||
<properties>
|
||||
<property>
|
||||
<name>system</name>
|
||||
<value>lms</value>
|
||||
<value>sso</value>
|
||||
</property>
|
||||
<property>
|
||||
<name>traceId</name>
|
||||
@@ -118,7 +118,7 @@ https://juejin.cn/post/6844903775631572999
|
||||
|
||||
<!--开发环境:打印控制台-->
|
||||
<springProfile name="dev">
|
||||
<root level="debug">
|
||||
<root level="info">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="esLogAppender" />
|
||||
</root>
|
||||
|
||||
Reference in New Issue
Block a user