Spring Boot 搭建 ELK,这才是正确看日志的方式!

沙海 2021年4月1日01:10:33杂谈 Java评论110字数 10383阅读34分36秒阅读模式
摘要

Spring Boot 搭建 ELK,这才是正确看日志的方式! sevenyuan 小哈学Java

Spring Boot 搭建 ELK,这才是正确看日志的方式!

sevenyuan 小哈学Java 文章源自JAVA秀-https://www.javaxiu.com/7804.html

点击上方蓝色“小哈学Java”,选择“设为星标”回复“资源”获取独家整理的学习资料!
文章源自JAVA秀-https://www.javaxiu.com/7804.html

作者:JingQ文章源自JAVA秀-https://www.javaxiu.com/7804.html

来源:https://www.sevenyuan.cn文章源自JAVA秀-https://www.javaxiu.com/7804.html

在看大型网站的中间件技术,对于Elasticsearch有点兴趣,所以将配置流程记录了一下。文章源自JAVA秀-https://www.javaxiu.com/7804.html

为什么要用ELK

文章源自JAVA秀-https://www.javaxiu.com/7804.html

ELK实际上是三个工具,Elastricsearch + LogStash + Kibana,通过ELK,用来收集日志还有进行日志分析,最后通过可视化UI进行展示。一开始业务量比较小的时候,通过简单的SLF4J+Logger在服务器打印日志,通过grep进行简单查询,但是随着业务量增加,数据量也会不断增加,所以使用ELK可以进行大数量的日志收集和分析文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

简单画了一下架构图

Spring Boot 搭建 ELK,这才是正确看日志的方式!文章源自JAVA秀-https://www.javaxiu.com/7804.html

在环境配置中,主要介绍Mac和linux配置,windows系统大致相同,当然,前提是大家都安装了JDK1.8及以上版本~文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos ~]# java -versionjava version "1.8.0_161"Java(TM) SE Runtime Environment (build 1.8.0_161-b12)Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

文章源自JAVA秀-https://www.javaxiu.com/7804.html

注意文章源自JAVA秀-https://www.javaxiu.com/7804.html

高版本的ELK同样需要高版本的JDK支持,本文配置的ELK版本是6.0+,所以需要的JDK版本不小于1.8文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

ElasticSearch

文章源自JAVA秀-https://www.javaxiu.com/7804.html

Elasticsearch 是一个分布式的 RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

Mac安装和运行文章源自JAVA秀-https://www.javaxiu.com/7804.html

安装:brew install elasticsearch运行:elasticsearch

linux: 从Elasticsearch官方地址下载(也可以下载完,通过ftp之类的工具传上去),gz文件的话通过tar进行解压缩,然后进入bin目录下运行软件文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos app]# curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz[root@VM_234_23_centos app]# tar -zxvf elasticsearch-6.2.4.tar.gz[root@VM_234_23_centos app]# cd elasticsearch-6.2.4[root@VM_234_23_centos elasticsearch-6.2.4]# ./bin/elasticsearch

文章源自JAVA秀-https://www.javaxiu.com/7804.html

注意文章源自JAVA秀-https://www.javaxiu.com/7804.html

在Linux机器上,运行elasticsearch需要一个新的用户组,文章最后有Elastic在linux安装的踩坑记录文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

Logstash

文章源自JAVA秀-https://www.javaxiu.com/7804.html

Logstash 是开源的服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的 “存储库” 中。(我们的存储库当然是 Elasticsearch。)-官方卖萌文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

1. 软件安装文章源自JAVA秀-https://www.javaxiu.com/7804.html

Mac安装:文章源自JAVA秀-https://www.javaxiu.com/7804.html

brew install logstash

linux安装:文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos app]# curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-6.3.2.tar.gz  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100  137M  100  137M    0     0  5849k      0  0:00:24  0:00:24 --:--:-- 6597k[root@VM_234_23_centos app]# tar -zxvf logstash-6.3.2.tar.gz

2. 修改配置文件文章源自JAVA秀-https://www.javaxiu.com/7804.html

vim /etc/logstash.conf

conf文件,指定要使用的插件,和配置对应的elasticsearch的hosts文章源自JAVA秀-https://www.javaxiu.com/7804.html

input { stdin { } }output {  elasticsearch { hosts => ["localhost:9200"] }  stdout { codec => rubydebug }}

3. 运行文章源自JAVA秀-https://www.javaxiu.com/7804.html

bin/logstash -f logstash.conf

4. 访问http://localhost:9600/文章源自JAVA秀-https://www.javaxiu.com/7804.html

{ "host": "=-=", "version": "6.2.4", "http_address": "127.0.0.1:9600", "id": "5b47e81f-bdf8-48fc-9537-400107a13bd2", "name": "=-=", "build_date": "2018-04-12T22:29:17Z", "build_sha": "a425a422e03087ac34ad6949f7c95ec6d27faf14", "build_snapshot": false}

在elasticsearch日志中,也能看到logstash正常加入的日志文章源自JAVA秀-https://www.javaxiu.com/7804.html

[2018-08-16T14:08:36,436][INFO ][o.e.c.m.MetaDataIndexTemplateService] [f2s1SD8] adding template [logstash] for index patterns [logstash-*]

看到这种返回值,表示已经成功安装和启动文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

踩坑文章源自JAVA秀-https://www.javaxiu.com/7804.html

在运行的那一步,有可能遇到内存分配错误文章源自JAVA秀-https://www.javaxiu.com/7804.html

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error=’Cannot allocate memory’ (errno=12)文章源自JAVA秀-https://www.javaxiu.com/7804.html

这个错误很明显就是内存不足,由于个人购买的是腾讯云1G内存的服务器(如果是壕,请随意购买更高的配置=-=),已经运行了elasticsearch,导致logstash分配不到足够的内存,所以最后要修改一下jvm配置。文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos logstash-6.3.2]# cd config/[root@VM_234_23_centos config]# lltotal 28-rw-r--r-- 1 root root 1846 Jul 20 14:19 jvm.options-rw-r--r-- 1 root root 4466 Jul 20 14:19 log4j2.properties-rw-r--r-- 1 root root 8097 Jul 20 14:19 logstash.yml-rw-r--r-- 1 root root 3244 Jul 20 14:19 pipelines.yml-rw-r--r-- 1 root root 1696 Jul 20 14:19 startup.options[root@VM_234_23_centos config]# vim jvm.options

将-Xms1g -Xmx1g修改为文章源自JAVA秀-https://www.javaxiu.com/7804.html

-Xms256m  -Xmx256m

然后就能正常启动了~~文章源自JAVA秀-https://www.javaxiu.com/7804.html

Kibana

1. 软件安装文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

Kibana 让您能够可视化 Elasticsearch 中的数据并操作 Elastic Stack,因此您可以在这里解开任何疑问:例如,为何会在凌晨 2:00 被传呼,雨水会对季度数据造成怎样的影响。(而且展示的图标十分酷炫)文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

Mac安装文章源自JAVA秀-https://www.javaxiu.com/7804.html

brew install kibana

linux安装,官方下载地址文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos app]# curl -L -O https://artifacts.elastic.co/downloads/kibana/kibana-6.3.2-linux-x86_64.tar.gz  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0  195M    0  271k    0     0  19235      0  2:57:54  0:00:14  2:57:40 26393

在这一步,有可能下载速度奇慢,所以我本地下载好之后,通过rz命令传输到服务器文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos app]# rzrz waiting to receive.Starting zmodem transfer.  Press Ctrl+C to cancel.Transferring kibana-6.3.2-linux-x86_64.tar.gz...  100%  200519 KB     751 KB/sec    00:04:27       0 Errors  [root@VM_234_23_centos app]# tar -zxvf kibana-6.3.2-linux-x86_64.tar.gz

2. 修改配置文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

修改 config/kibana.yml 配置文件,设置 elasticsearch.url 指向 Elasticsearch 实例。文章源自JAVA秀-https://www.javaxiu.com/7804.html

如果跟我一样使用默认的配置,可以不需要修改该文件文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

3. 启动文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos kibana]# ./bin/kibana

4. 访问 http://localhost:5601/app/kibana#/home?_g=()文章源自JAVA秀-https://www.javaxiu.com/7804.html

Spring Boot 搭建 ELK,这才是正确看日志的方式!文章源自JAVA秀-https://www.javaxiu.com/7804.html

界面显示了这么多功能,下面通过整合SLF4J+LogBack文章源自JAVA秀-https://www.javaxiu.com/7804.html

整合Spring+Logstash

1. 修改logstash.conf后,重新启动logstash文章源自JAVA秀-https://www.javaxiu.com/7804.html

input {   # stdin { }  tcp {     # host:port就是上面appender中的 destination, # 这里其实把logstash作为服务,开启9250端口接收logback发出的消息     host => "127.0.0.1" port => 9250 mode => "server" tags => ["tags"] codec => json_lines   }}output {  elasticsearch { hosts => ["localhost:9200"] }  stdout { codec => rubydebug }}

2. 在Java应用中引用依赖文章源自JAVA秀-https://www.javaxiu.com/7804.html

<dependency>  <groupId>net.logstash.logback</groupId>  <artifactId>logstash-logback-encoder</artifactId>  <version>5.2</version></dependency>

文章源自JAVA秀-https://www.javaxiu.com/7804.html

3. 在Logback.xml中配置日志输出文章源自JAVA秀-https://www.javaxiu.com/7804.html

<!--日志导出的到 Logstash--><appender name="stash"              class="net.logstash.logback.appender.LogstashTcpSocketAppender">   <destination>localhost:9250</destination>   <!-- encoder必须配置,有多种可选 -->   <encoder charset="UTF-8"            class="net.logstash.logback.encoder.LogstashEncoder" >       <!-- "appname":"ye_test" 的作用是指定创建索引的名字时用,并且在生成的文档中会多了这个字段  -->       <customFields>{"appname":"ye_test"}</customFields>   </encoder></appender>      <root level="INFO">    <appender-ref ref="stash"/></root>

由于我在第一步骤中,没有指定对应的index,所以在服务启动的时候,日志采集器Logstash帮我自动创建了logstash-timestamp的index。文章源自JAVA秀-https://www.javaxiu.com/7804.html

4. 在kibana中添加index索引文章源自JAVA秀-https://www.javaxiu.com/7804.html

Spring Boot 搭建 ELK,这才是正确看日志的方式!文章源自JAVA秀-https://www.javaxiu.com/7804.html

5. 在左边discover中查看索引信息文章源自JAVA秀-https://www.javaxiu.com/7804.html

Spring Boot 搭建 ELK,这才是正确看日志的方式!文章源自JAVA秀-https://www.javaxiu.com/7804.html

6. 添加可视化图表Visualize文章源自JAVA秀-https://www.javaxiu.com/7804.html

Spring Boot 搭建 ELK,这才是正确看日志的方式!文章源自JAVA秀-https://www.javaxiu.com/7804.html

还有更多功能还在探索中,首先环境搭起来才会用动力继续去学习~文章源自JAVA秀-https://www.javaxiu.com/7804.html

踩坑记录

启动报错

文章源自JAVA秀-https://www.javaxiu.com/7804.html

uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

原因:不能使用Root权限登录文章源自JAVA秀-https://www.javaxiu.com/7804.html

解决方案:切换用户文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos ~]# groupadd es[root@VM_234_23_centos ~]# useradd es -g es -p es[root@VM_234_23_centos ~]# chown es:es /home/app/elasticsearch/# 切换用户,记得su - ,这样才能获得环境变量[root@VM_234_23_centos ~]# sudo su - es

文章源自JAVA秀-https://www.javaxiu.com/7804.html

Exception in thread “main” java.nio.file.AccessDeniedException:文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

错误原因:使用非 root用户启动ES,而该用户的文件权限不足而被拒绝执行。文章源自JAVA秀-https://www.javaxiu.com/7804.html

解决方法:chown -R 用户名:用户名 文件(目录)名文章源自JAVA秀-https://www.javaxiu.com/7804.html

例如:chown -R abc:abc searchengine 再启动ES就正常了文章源自JAVA秀-https://www.javaxiu.com/7804.html

elasticsearch启动后报Killed

[2018-07-13T10:19:44,775][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [aggs-matrix-stats][2018-07-13T10:19:44,779][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [analysis-common][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [ingest-common][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [lang-expression][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [lang-mustache][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [lang-painless][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [mapper-extras][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [parent-join][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [percolator][2018-07-13T10:19:44,780][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [rank-eval][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [reindex][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [repository-url][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [transport-netty4][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [tribe][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-core][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-deprecation][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-graph][2018-07-13T10:19:44,781][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-logstash][2018-07-13T10:19:44,782][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-ml][2018-07-13T10:19:44,782][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-monitoring][2018-07-13T10:19:44,782][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-rollup][2018-07-13T10:19:44,782][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-security][2018-07-13T10:19:44,782][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-sql][2018-07-13T10:19:44,782][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-upgrade][2018-07-13T10:19:44,782][INFO ][o.e.p.PluginsService     ] [f2s1SD8] loaded module [x-pack-watcher][2018-07-13T10:19:44,783][INFO ][o.e.p.PluginsService     ] [f2s1SD8] no plugins loadedKilled

修改config目录下的jvm.options,将堆的大小设置小一点文章源自JAVA秀-https://www.javaxiu.com/7804.html

# Xms represents the initial size of total heap space# Xmx represents the maximum size of total heap space-Xms512m-Xmx512m

虚拟内存不足

文章源自JAVA秀-https://www.javaxiu.com/7804.html

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]文章源自JAVA秀-https://www.javaxiu.com/7804.html

文章源自JAVA秀-https://www.javaxiu.com/7804.html

[2018-07-13T14:02:06,749][DEBUG][o.e.a.ActionModule       ] Using REST wrapper from plugin org.elasticsearch.xpack.security.Security[2018-07-13T14:02:07,249][INFO ][o.e.d.DiscoveryModule    ] [f2s1SD8] using discovery type [zen][2018-07-13T14:02:09,173][INFO ][o.e.n.Node               ] [f2s1SD8] initialized[2018-07-13T14:02:09,174][INFO ][o.e.n.Node               ] [f2s1SD8] starting ...[2018-07-13T14:02:09,539][INFO ][o.e.t.TransportService   ] [f2s1SD8] publish_address {10.105.234.23:9300}, bound_addresses {0.0.0.0:9300}[2018-07-13T14:02:09,575][INFO ][o.e.b.BootstrapChecks    ] [f2s1SD8] bound or publishing to a non-loopback address, enforcing bootstrap checksERROR: [1] bootstrap checks failed[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144][2018-07-13T14:02:09,621][INFO ][o.e.n.Node               ] [f2s1SD8] stopping ...[2018-07-13T14:02:09,726][INFO ][o.e.n.Node               ] [f2s1SD8] stopped[2018-07-13T14:02:09,726][INFO ][o.e.n.Node               ] [f2s1SD8] closing ...[2018-07-13T14:02:09,744][INFO ][o.e.n.Node               ] [f2s1SD8] closed

需要修改虚拟内存的大小(在root权限下)文章源自JAVA秀-https://www.javaxiu.com/7804.html

[root@VM_234_23_centos elasticsearch]# vim /etc/sysctl.conf# 插入下列代码后保存退出vm.max_map_count=655360[root@VM_234_23_centos elasticsearch]# sysctl -p# 最后重启elastricsearch
文章源自JAVA秀-https://www.javaxiu.com/7804.html
1. 如何甄别应聘者简历的包装程度?2. Nacos 2.0 正式发布,性能大幅提升 10 倍!3. Redis单线程已经很快了,为什么6.0要引入多线程?带来什么优势?4. 事务注解 @Transactional 失效的3种场景及解决办法最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复 Java 领取,更多内容陆续奉上。

文章有帮助的话,在看,转发吧。文章源自JAVA秀-https://www.javaxiu.com/7804.html

谢谢支持哟 (*^__^*)文章源自JAVA秀-https://www.javaxiu.com/7804.html 文章源自JAVA秀-https://www.javaxiu.com/7804.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定