elasticsearch 使用Filebeat将Elastic ELK stack 8.5与Spring Boots应用程序集成

yx2lnoni  于 2022-12-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(221)

在本地设置一个ElasticSearch、kibana和logstash的管道,并使用filebeat将日志从spring Boot 应用程序推送到管道中。你会发现官方文档定义良好,但我创建这些问题是为了回答一些不清楚的问题。我回答了一个spring boot应用程序场景,感谢人们也添加了他们的场景。

jogvjijk

jogvjijk1#

我花了几天时间用我的spring Boot 应用程序来配置ELK堆栈。这里我不会详细说明逐步集成的过程,你可以参考官方文档。这更侧重于我在文档步骤中没有找到的内容。
环境:这将集中在设置8.5.3版本在mac操作系统。
对于Elasticsearch和Kibana,我没有任何麻烦,按照官方文件逐字逐句。
ElasticSearch:https://www.elastic.co/downloads/elasticsearch
木花:https://www.elastic.co/downloads/kibana
在我的项目中,我只需要提取一个特定的日志行并处理它。你可以使用下面的官方文档链接下载并提取logstash和filebeat。然后你可以在运行它之前使用上面提到的配置。
日志存储:https://www.elastic.co/downloads/logstash
文件节拍:https://www.elastic.co/downloads/beats/filebeat

文件节拍:

首先,你需要对你的filebeat.yml文件进行权限修改,导航到你的filebeat解压文件夹,如果需要的话,你可以使用下面的配置。

filebeat.inputs:

- type: filestream
  id: filebeat-id-name
  enabled: true
  paths:
    - /Users/leons/IdeaProjects/SpringELKDemo/myapplogs.log  #Path to you log file
#I wanted to only read the log line with MainController string in it
  include_lines: ['MainController'] 

output.logstash:
  hosts: ["localhost:5044"]

然后你需要用下面的命令(mac)修改这个文件的写权限。以后你可以用sudo nano编辑这个文件。

sudo chown root filebeat.yml

日志存储区:

初始化一个logstash.conf的示例文件,可以在logstash.conf的config文件夹中找到。你可以参考它,也可以看看我的。

input {
  beats {
    port => 5044
  }
}
filter {
    dissect {
        mapping => {
            "message" => "%{}: %{data_message}"
         }
    }
    json {
    source => "data_message"
    }
}
output {
  elasticsearch {
    hosts => ["https://localhost:9200"]
    index => "index_name"
    user => "elastic"
    password => "XXXXXXXXXXXXX-XXX"
    ssl_certificate_verification => false
  }
  stdout{
    codec => rubydebug
  }
}

我使用了dissect过滤器在我的日志行中进行字符串操作,该filebeat被传输。下面是我的日志,我只需要确切的消息,即JSON字符串

2022-12-15 21:14:56.152  INFO 9278 --- [http-nio-8080-exec-10] c.p.t.springdemo.controller.MainController    : {"name":"leons","id":"123123","msg":"hello world"}

有关解剖的更多信息,请参阅官方docs
json过滤器用于转换JSON密钥:将值转换为字段,并将值转换为弹性文档中的值。
现在你应该准备好使用官方文档命令运行logstash和filebeat了。
日志存放:

bin/logstash -f logstash.conf

文件节拍:

sudo ./filebeat -e -c filebeat.yml

相关问题