Spring Boot 搭建 ELK正確看日志的配置流程
ELK實(shí)際上是三個(gè)工具,Elastricsearch + Logstash + Kibana,通過(guò)ELK,用來(lái)收集日志還有進(jìn)行日志分析,最后通過(guò)可視化UI進(jìn)行展示。一開(kāi)始業(yè)務(wù)量比較小的時(shí)候,通過(guò)簡(jiǎn)單的SLF4J+Logger在服務(wù)器打印日志,通過(guò)grep進(jìn)行簡(jiǎn)單查詢(xún),但是隨著業(yè)務(wù)量增加,數(shù)據(jù)量也會(huì)不斷增加,所以使用ELK可以進(jìn)行大數(shù)量的日志收集和分析
簡(jiǎn)單畫(huà)了一下架構(gòu)圖
在環(huán)境配置中,主要介紹Mac和Linux配置,Windows系統(tǒng)大致相同,當(dāng)然,前提是大家都安裝了JDK 1.8及以上版本~
[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)
注意:高版本的ELK同樣需要高版本的JDK支持,本文配置的ELK版本是6.0+,所以需要的JDK版本不小于1.8
ElasticsearchElasticsearch是一個(gè)分布式的RESTful風(fēng)格的搜索和數(shù)據(jù)分析引擎,能夠解決不斷涌現(xiàn)出的各種用例。作為Elastic Stack的核心,它集中存儲(chǔ)您的數(shù)據(jù),幫助您發(fā)現(xiàn)意料之中以及意料之外的情況。
Mac安裝和運(yùn)行:
安裝:brew install elasticsearch 運(yùn)行:elasticsearchLinux:從Elasticsearch官方地址下載(也可以下載完,通過(guò)ftp之類(lèi)的工具傳上去),gz文件的話通過(guò)tar進(jìn)行解壓縮,然后進(jìn)入bin目錄下運(yùn)行軟件。
[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
注意:在Linux機(jī)器上,運(yùn)行Elasticsearch需要一個(gè)新的用戶(hù)組,文章最后有Elastic在Linux安裝的踩坑記錄。
LogstashLogstash是開(kāi)源的服務(wù)器端數(shù)據(jù)處理管道,能夠同時(shí)從多個(gè)來(lái)源采集數(shù)據(jù),轉(zhuǎn)換數(shù)據(jù),然后將數(shù)據(jù)發(fā)送到您最喜歡的“存儲(chǔ)庫(kù)”中。(我們的存儲(chǔ)庫(kù)當(dāng)然是Elasticsearch。)——官方賣(mài)萌
軟件安裝Mac安裝:
brew install logstash
Linux安裝:
[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 CurrentDload 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
修改配置文件
vim /etc/logstash.conf
conf文件,指定要使用的插件,和配置對(duì)應(yīng)的elasticsearch的hosts。
input { stdin { } }output {elasticsearch { hosts => ['localhost:9200'] }stdout { codec => rubydebug }}
運(yùn)行
bin/logstash -f logstash.conf
訪問(wèn) http://localhost:9600/
{'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正常加入的日志。
[2018-08-16T14:08:36,436][INFO ][o.e.c.m.MetaDataIndexTemplateService] [f2s1SD8] adding template [logstash] for index patterns [logstash-*]
看到這種返回值,表示已經(jīng)成功安裝和啟動(dòng)。
踩坑
在運(yùn)行的那一步,有可能遇到內(nèi)存分配錯(cuò)誤:
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error=’Cannot allocate memory’ (errno=12)
這個(gè)錯(cuò)誤很明顯就是內(nèi)存不足,由于個(gè)人購(gòu)買(mǎi)的是騰訊云1G內(nèi)存的服務(wù)器(如果是壕,請(qǐng)隨意購(gòu)買(mǎi)更高的配置=-=),已經(jīng)運(yùn)行了Elasticsearch,導(dǎo)致Logstash分配不到足夠的內(nèi)存,所以最后要修改一下JVM配置。
[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修改為:
-Xms256m
-Xmx256m
然后就能正常啟動(dòng)了~~
Kibana
軟件安裝
Kibana讓您能夠可視化Elasticsearch中的數(shù)據(jù)并操作Elastic Stack,因此您可以在這里解開(kāi)任何疑問(wèn):例如,為何會(huì)在凌晨2:00被傳呼,雨水會(huì)對(duì)季度數(shù)據(jù)造成怎樣的影響。(而且展示的圖標(biāo)十分酷炫)
Mac安裝:
brew install kibana
Linux安裝,官方下載地址: https://www.elastic.co/downloads/kibana
[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 CurrentDload Upload Total Spent Left Speed0 195M 0 271k 0 0 19235 0 2:57:54 0:00:14 2:57:40 26393
在這一步,有可能下載速度奇慢,所以我本地下載好之后,通過(guò)rz命令傳輸?shù)椒?wù)器。
[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修改配置
修改config/kibana.yml配置文件,設(shè)置elasticsearch.url指向Elasticsearch實(shí)例。
如果跟我一樣使用默認(rèn)的配置,可以不需要修改該文件。
啟動(dòng)
[root@VM_234_23_centos kibana]# ./bin/kibana
訪問(wèn) http://localhost:5601/app/kibana#/home?_g= ()
界面顯示了這么多功能,下面通過(guò)整合SLF4J+LogBack。
整合Spring+Logstash
修改logstash.conf后,重新啟動(dòng)Logstash
input { # stdin { }tcp { # host:port就是上面appender中的destination,# 這里其實(shí)把Logstash作為服務(wù),開(kāi)啟9250端口接收l(shuí)ogback發(fā)出的消息 host => '127.0.0.1' port => 9250 mode => 'server' tags => ['tags'] codec => json_lines }}output {elasticsearch { hosts => ['localhost:9200'] }stdout { codec => rubydebug }}
在Java應(yīng)用中引用依賴(lài)
<dependency><groupId>net.logstash.logback</groupId><artifactId>logstash-logback-encoder</artifactId><version>5.2</version></dependency>
在logback.xml中配置日志輸出
<!--日志導(dǎo)出的到 Logstash--><appender name='stash' class='net.logstash.logback.appender.LogstashTcpSocketAppender'><destination>localhost:9250</destination><!-- encoder必須配置,有多種可選 --><encoder charset='UTF-8' > <!-- 'appname':'ye_test' 的作用是指定創(chuàng)建索引的名字時(shí)用,并且在生成的文檔中會(huì)多了這個(gè)字段 --> <customFields>{'appname':'ye_test'}</customFields></encoder></appender> <root level='INFO'><appender-ref ref='stash'/></root>
由于我在第一步驟中,沒(méi)有指定對(duì)應(yīng)的index,所以在服務(wù)啟動(dòng)的時(shí)候,日志采集器Logstash幫我自動(dòng)創(chuàng)建了logstash-timestamp的index。
在Kibana中添加index索引
在左邊discover中查看索引信息
添加可視化圖表Visualize
還有更多功能還在探索中,首先環(huán)境搭起來(lái)才會(huì)用動(dòng)力繼續(xù)去學(xué)習(xí)~
踩坑記錄 啟動(dòng)報(bào)錯(cuò)
uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
原因: 不能使用Root權(quán)限登錄
解決方案: 切換用戶(hù)
[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/# 切換用戶(hù),記得su - ,這樣才能獲得環(huán)境變量[root@VM_234_23_centos ~]# sudo su - esException in thread “main” java.nio.file.AccessDeniedException:
錯(cuò)誤原因:使用非 root用戶(hù)啟動(dòng)ES,而該用戶(hù)的文件權(quán)限不足而被拒絕執(zhí)行。
解決方法: chown -R用戶(hù)名:用戶(hù)名 文件(目錄)名
例如: chown -R abc:abc searchengine,再啟動(dòng)ES就正常了
Elasticsearch啟動(dòng)后報(bào)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 loaded
Killed
修改config目錄下的jvm.options,將堆的大小設(shè)置小一點(diǎn)。
# Xms represents the initial size of total heap space# Xmx represents the maximum size of total heap space-Xms512m-Xmx512m
虛擬內(nèi)存不足
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
需要修改虛擬內(nèi)存的大小(在root權(quán)限下)
[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 checks
ERROR: [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
到此這篇關(guān)于Spring Boot 搭建 ELK正確看日志的配置流程的文章就介紹到這了,更多相關(guān)Spring Boot 搭建 ELK內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. XHTML 1.0:標(biāo)記新的開(kāi)端3. HTML5 Canvas繪制圖形從入門(mén)到精通4. XML解析錯(cuò)誤:未組織好 的解決辦法5. ASP基礎(chǔ)知識(shí)VBScript基本元素講解6. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. 利用CSS3新特性創(chuàng)建透明邊框三角9. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法10. XML入門(mén)的常見(jiàn)問(wèn)題(四)
