概念
ELK:Elasticsearch、Logstash、Kibana,组合起来可以搭建线上日志系统
各服务作用
- Elasticsearch:用于存储收集到的日志信息;
- Logstash:用于收集日志,SpringBoot应用整合了Logstash以后会把日志发送给Logstash,- Logstash再把日志转发给Elasticsearch;
- Kibana:通过Web端的可视化界面来查看日志
安装部署
安装docker与Docker Compose
安装Elasticsearch
1
| docker pull elasticsearch:6.4.0
|
安装Logstash
1
| docker pull logstash:6.4.0
|
安装Kibana
1
| docker pull kibana:6.4.0
|
编写 docker-compose.yml
在SpringBoot项目中的scr/main/docker/docker-compose.yml编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| version: '3' services: elasticsearch: image: elasticsearch:6.4.0 container_name: elasticsearch environment: - "cluster.name=elasticsearch" #设置集群名称为elasticsearch - "discovery.type=single-node" #以单一节点模式启动 - "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小 ports: - 9200:9200 kibana: image: kibana:6.4.0 container_name: kibana links: - elasticsearch:es #可以用es这个域名访问elasticsearch服务 depends_on: - elasticsearch #kibana在elasticsearch启动之后再启动 environment: - "elasticsearch.hosts=http://es:9200" #设置访问elasticsearch的地址 ports: - 5601:5601 logstash: image: logstash:6.4.0 container_name: logstash volumes: - ./:/configdir command: logstash -f /configdir/logstash-springboot.conf logstash.conf #挂载logstash的配置文件 depends_on: - elasticsearch #kibana在elasticsearch启动之后再启动 links: - elasticsearch:es #可以用es这个域名访问elasticsearch服务 ports: - 4560:4560
|
编写 logstash-springboot.conf
在SpringBoot项目中的scr/main/docker/logstash-springboot.conf编写:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| input { tcp { mode => "server" host => "192.168.31.13" port => 4560 codec => json_lines } } output { elasticsearch { hosts => "es:9200" index => "springboot-logstash-%{+YYYY.MM.dd}" } }
|
执行
cmd切换到docker文件夹下,执行
安装json_lines插件
1 2 3 4 5 6 7 8 9 10
| # 进入logstash容器 docker exec -it logstash /bin/bash # 进入bin目录 cd /bin/ # 安装插件 logstash-plugin install logstash-codec-json_lines # 退出容器 exit # 重启logstash服务 docker restart logstash
|
访问
浏览器打开
1
| http://localhost:5601/app/kibana#/management?_g=()
|
SpringBoot应用集成Logstash
pom.xml添加引用
1 2 3 4 5 6
| <!--集成logstash--> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.3</version> </dependency>
|
resource中新增logback-spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?xml version="1.0" encoding="UTF-8"?> <!--<!DOCTYPE configuration>--> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <!--应用名称--> <property name="APP_NAME" value="mall-admin"/> <!--日志文件保存路径--> <property name="LOG_FILE_PATH" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/logs}"/> <contextName>${APP_NAME}</contextName> <!--每天记录日志到文件appender--> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE_PATH}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> </appender> <!--输出到logstash的appender--> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <!--可以访问的logstash日志收集端口--> <destination>192.168.31.13:4560</destination> <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/> </appender> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> <appender-ref ref="LOGSTASH"/> </root> </configuration>
|
编写日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class TestController { private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
@GetMapping("/test") public String test() { LOGGER.debug("TestController:{}", "Hello World!!!ELK-debug"); LOGGER.error("TestController:{}", "Hello World!!!ELK-error"); LOGGER.warn("TestController:{}", "Hello World!!!ELK-warn"); LOGGER.info("TestController:{}", "Hello World!!!ELK-info"); // int i = 1 /0; return "Hello World!!!ELK"; }
}
|
运行SpringBoot应用
运行SpringBoot项目,浏览器访问http://192.168.31.13:8901/test
在kibana中查看日志信息
创建index pattern
查看日志
注意ipd地址192.168.31.13
需改为自己的本机ip
总结
搭建了ELK日志收集系统之后,如果要查看SpringBoot应用的日志信息,就不需要查看日志文件了,直接在Kibana中查看即可