Bläddra i källkod

集成elk收集日志

zhh 6 år sedan
förälder
incheckning
8e959bf873

+ 3 - 2
README.md

@@ -55,10 +55,11 @@ Elasticsearch搜索功能 | ✔
 HTTPS支持 | ✔
 Redis数字型ID生成 | ✔
 SpringTask定时任务支持 | ✔
-RestTemplate服务间调用 |
 docker容器化部署 | ✔
 配置区分生产和测试环境 | ✔
-ELK日志收集功能 |
+ELK日志收集功能 | ✔
+RestTemplate服务间调用 |
+RabbitMq异步通信 |
 SpringSecurity权限管理功能 |
 
 ### 使用工具

+ 21 - 0
document/elk/elk.md

@@ -0,0 +1,21 @@
+#windows下的elk环境搭建
+
+##下载
+下载页面:https://www.elastic.co/downloads/past-releases
+ElasticSearch:https://www.elastic.co/downloads/past-releases/elasticsearch-2-4-6
+Logstash:https://www.elastic.co/downloads/past-releases/logstash-2-4-0
+Kibana:https://www.elastic.co/downloads/past-releases/kibana-4-6-0
+
+##安装
+下载zip包并进行解压
+
+##运行
+
+###logstash配置运行
+添加logstash配置文件:logstash-springboot.conf
+安装logstash-codec-json_lines插件:plugin install logstash-codec-json_lines
+运行logstash命令:logstash -f logstash-springboot.conf
+
+##SpringBoot整合logstash
+引入依赖包:https://github.com/logstash/logstash-logback-encoder
+添加配置文件:logback-spring.xml

+ 14 - 0
document/elk/logback-spring.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE configuration>
+<configuration>
+    <include resource="org/springframework/boot/logging/logback/base.xml"/>
+    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
+        <destination>localhost:4560</destination>
+        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
+    </appender>
+    <root level="INFO">
+        <appender-ref ref="LOGSTASH" />
+        <appender-ref ref="CONSOLE" />
+    </root>
+
+</configuration>

+ 10 - 0
document/elk/logstash-springboot.conf

@@ -0,0 +1,10 @@
+input {
+    tcp {
+        port => 4560
+        codec => json_lines
+    }
+}
+output{
+  elasticsearch { hosts => ["localhost:9200"] }
+  stdout { codec => rubydebug }
+}

+ 6 - 0
mall-admin/pom.xml

@@ -85,6 +85,12 @@
             <artifactId>aliyun-sdk-oss</artifactId>
             <version>2.5.0</version>
         </dependency>
+        <!--集成logstash-->
+        <dependency>
+            <groupId>net.logstash.logback</groupId>
+            <artifactId>logstash-logback-encoder</artifactId>
+            <version>4.8</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

+ 14 - 3
mall-admin/src/main/java/com/macro/mall/component/WebLogAspect.java

@@ -4,6 +4,7 @@ import com.macro.mall.bo.WebLog;
 import com.macro.mall.util.JsonUtil;
 import com.macro.mall.util.RequestUtil;
 import io.swagger.annotations.ApiOperation;
+import net.logstash.logback.marker.Markers;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.Signature;
@@ -22,7 +23,10 @@ import org.springframework.web.context.request.ServletRequestAttributes;
 import javax.servlet.http.HttpServletRequest;
 import java.lang.reflect.Method;
 import java.lang.reflect.Parameter;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * 统一日志处理切面
@@ -53,7 +57,7 @@ public class WebLogAspect {
         //获取当前请求对象
         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
         HttpServletRequest request = attributes.getRequest();
-        //记录请求信息(可以记录到数据库中)
+        //记录请求信息(通过logstash传入elasticsearch)
         WebLog webLog = new WebLog();
         Object result = joinPoint.proceed();
         Signature signature = joinPoint.getSignature();
@@ -73,7 +77,14 @@ public class WebLogAspect {
         webLog.setStartTime(startTime.get());
         webLog.setUri(request.getRequestURI());
         webLog.setUrl(request.getRequestURL().toString());
-        LOGGER.info("{}", JsonUtil.objectToJson(webLog));
+        Map<String,Object> logMap = new HashMap<>();
+        logMap.put("url",webLog.getUrl());
+        logMap.put("method",webLog.getMethod());
+        logMap.put("parameter",webLog.getParameter());
+        logMap.put("spendTime",webLog.getSpendTime());
+        logMap.put("description",webLog.getDescription());
+        //        LOGGER.info("{}", JsonUtil.objectToJson(webLog));
+        LOGGER.info(Markers.appendEntries(logMap),JsonUtil.objectToJson(webLog));
         return result;
     }
 

+ 14 - 0
mall-admin/src/main/resources/logback-spring.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE configuration>
+<configuration>
+    <include resource="org/springframework/boot/logging/logback/base.xml"/>
+    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
+        <destination>localhost:4560</destination>
+        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
+    </appender>
+    <root level="INFO">
+        <appender-ref ref="LOGSTASH" />
+        <appender-ref ref="CONSOLE" />
+    </root>
+
+</configuration>

+ 7 - 2
mall-demo/pom.xml

@@ -32,11 +32,11 @@
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-thymeleaf</artifactId>
+            <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
+            <artifactId>spring-boot-starter-thymeleaf</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -71,6 +71,11 @@
             <artifactId>pagehelper-spring-boot-starter</artifactId>
             <version>1.2.3</version>
         </dependency>
+        <dependency>
+            <groupId>net.logstash.logback</groupId>
+            <artifactId>logstash-logback-encoder</artifactId>
+            <version>4.8</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 14 - 0
mall-demo/src/main/resources/logback-spring.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE configuration>
+<configuration>
+    <include resource="org/springframework/boot/logging/logback/base.xml"/>
+    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
+        <destination>localhost:4560</destination>
+        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
+    </appender>
+    <root level="INFO">
+        <appender-ref ref="LOGSTASH" />
+        <appender-ref ref="CONSOLE" />
+    </root>
+
+</configuration>

+ 16 - 1
mall-demo/src/test/java/com/macro/mall/MallDemoApplicationTests.java

@@ -1,16 +1,31 @@
 package com.macro.mall;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.macro.mall.model.PmsProduct;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
 public class MallDemoApplicationTests {
-
+	private Logger logger = LoggerFactory.getLogger(MallDemoApplicationTests.class);
 	@Test
 	public void contextLoads() {
 	}
 
+	@Test
+	public void testLogStash() throws Exception {
+		ObjectMapper mapper = new ObjectMapper();
+		PmsProduct product = new PmsProduct();
+		product.setId(1L);
+		product.setName("小米手机");
+		product.setBrandName("小米");
+		logger.info(mapper.writeValueAsString(product));
+		logger.error(mapper.writeValueAsString(product));
+	}
+
 }