链路追踪

文章40 |   阅读 21443 |   点赞0

来源:https://blog.csdn.net/weixin_42073629/category_9940428.html

Spring Boot 日志平台 ELK + Filebeat 入门

x33g5p2x  于2021-12-21 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(457)

1. 概述

在《Spring Boot 日志集成 Logging 入门》文章中,我们学习了 Spring Boot 如何集成并使用日志框架,进行日志的打印。
友情提示:对于本文来说,即使胖友未阅读过《Spring Boot 日志集成 Logging 入门》文章,也毫无影响,不要慌~

不过正如我们在文末所说,在生产环境下,我们会集群部署我们的应用。那么我们可能需要登陆多台服务器,查看不用应用节点下的日志,这样会非常不方便。

所以,本文我们就一起来,使用 ELK + Filebeat 组件,搭建统一的日志服务。并且,来实现一个对 Spring Boot 应用日志的收集的示例。

在阅读本文之前,胖友先去阅读下艿艿写的《ELK(Elasticsearch + Logstash + Kibana) 极简入门》文章,先把 Elasticsearch、Logstash、Kibana、Filebeat 四个组件给搭建起来。同时在搭建的过程中,会对这四者有个直观的感受和认识。

2. Spring Boot 应用

示例代码对应仓库:lab-38-elk-demo 。

本小节,我们来搭建 Spring Boot 应用示例,使用 SLF4J + Logback 打日志。

2.1 引入依赖

在 pom.xml 文件中,引入相关依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lab-38-elk-demo</artifactId>

    <dependencies>
        <!-- 实现对 Spring MVC 的自动化配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 配置文件

在 application.yml 中,添加日志相关配置,如下:

spring:
  application:
    name: demo-application # 应用名

logging:
  # 日志文件配置
  file:
    name: /Users/yunai/logs/${spring.application.name}.log # 日志文件名。
  • 配置项 spring.application.name ,设置应用名,为 "demo-application"
  • 配置项 logging.file.name ,设置日志文件的地址,为 "/Users/yunai/logs/demo-application.log"

2.3 Application

创建 Application.java 类,配置 @SpringBootApplication 注解即可。代码如下:

// Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
}

2.4 简单测试

执行 Application#main(String[] args) 方法,启动应用。此时,"/Users/yunai/logs/demo-application.log" 日志日志已经有如下内容:

2020-01-01 20:50:58.245  INFO 36540 --- [main] c.i.s.lab38.elkdemo.Application          : Starting Application on MacBook-Pro-8 with PID 36540 (/Users/yunai/Java/SpringBoot-Labs/lab-38/lab-38-elk-demo/target/classes started by yunai in /Users/yunai/Java/SpringBoot-Labs)
2020-01-01 20:50:58.248  INFO 36540 --- [main] c.i.s.lab38.elkdemo.Application          : No active profile set, falling back to default profiles: default
2020-01-01 20:50:59.065  INFO 36540 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-01 20:50:59.072  INFO 36540 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-01 20:50:59.072  INFO 36540 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-01-01 20:50:59.122  INFO 36540 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-01 20:50:59.122  INFO 36540 --- [main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 835 ms
2020-01-01 20:50:59.228  INFO 36540 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-01 20:50:59.360  INFO 36540 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-01 20:50:59.363  INFO 36540 --- [main] c.i.s.lab38.elkdemo.Application          : Started Application in 1.414 seconds (JVM running for 1.802)
2020-01-01 20:51:09.822  INFO 36540 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

3. 接入 ELK 日志服务

友情提示:认为胖友已经参考《ELK(Elasticsearch + Logstash + Kibana) 极简入门》文章,使用 ELK + Filebeat 搭建日志服务。

本小节,我们将配置 Filebeat 采集「2. Spring Boot 应用」的 "/Users/yunai/logs/demo-application.log" 日志文件,最终在 Kibana 展示每一条日志的内容。

3.1 配置 Filebeat 采集日志

① 修改 Filebeat 配置

使用 vi filebeat.yml 命令,编辑 Filebeat 配置文件。主要修改如下行:

filebeat.inputs:
- type: log
  # Change to true to enable this input configuration.
  enabled: true
  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    # - /var/log/*.log
    #- c:\programdata\elasticsearch\logs\*
    - /Users/yunai/logs/spring.log

#-------------------------- 额外新增 ------------------------------
- type: log
  enable: true
  paths:
    - /Users/yunai/logs/demo-application.log
  fields:
    application: demo-application
  • 在原有的 filebeat.inputs 的数组配置项,我们新增了一个读取 "/Users/yunai/logs/demo-application.log" 日志文件的 Input 来源。
  • 比较特别的是,我们设置了自定义字段 fields.application ,主要目的是我们可能有很多不同的 Spring Boot 应用,可以通过该字段区分。

② 重启 Filebeat 服务

为了让 Filebeat 新配置生效,需要重启 Filebeat 服务。操作命令如下:

# 查找 Filebeat 进程号
$ ps -ef | grep filebeat
  501 37369 26612   0 10:22PM ttys005    0:00.26 ./filebeat
  
# 杀死 Filebeat 进程
$ kill 37369

# 后台启动 Filebeat 服务。
$ nohup ./filebeat &

3.2 使用 Kibana 查看日志

因为我们在《ELK(Elasticsearch + Logstash + Kibana) 极简入门》文章的「5.4 简单使用」小节中,已经对 filebeat-7.5.1-* 索引进行配置,所以此时我们无需重新配置。

① 使用浏览器,访问 http://127.0.0.1:5601/ 地址,选择左边「Discover」菜单,进入「Discover」界面。如下图所示:

② 在「UptimeIndexPattern」下拉框,选择 filebeat-7.5.1-* 选项。在「时间范围」下拉框,选择近 30 分钟。如下图所示:

至此,我们已经完成 ELK 日志服务的接入。

相关文章