javascript
SpringBoot整合Elastricsearch + LogStash + Kibana太简单了!
作者:JingQ
https://www.sevenyuan.cn/
在看大型網(wǎng)站的中間件技術(shù),對(duì)于Elasticsearch有點(diǎn)興趣,所以將配置流程記錄了一下
為什么要用ELK
ELK實(shí)際上是三個(gè)工具,Elastricsearch + LogStash + Kibana,通過ELK,用來收集日志還有進(jìn)行日志分析,最后通過可視化UI進(jìn)行展示。一開始業(yè)務(wù)量比較小的時(shí)候,通過簡單的SLF4J+Logger在服務(wù)器打印日志,通過grep進(jìn)行簡單查詢,但是隨著業(yè)務(wù)量增加,數(shù)據(jù)量也會(huì)不斷增加,所以使用ELK可以進(jìn)行大數(shù)量的日志收集和分析
簡單畫了一下架構(gòu)圖
「在環(huán)境配置中,主要介紹Mac和linux配置,windows系統(tǒng)大致相同,當(dāng)然,前提是大家都安裝了JDK1.8及以上版本~」
注意
高版本的ELK同樣需要高版本的JDK支持,本文配置的ELK版本是6.0+,所以需要的JDK版本不小于1.8
ElasticSearch
Elasticsearch 是一個(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官方地址下載(也可以下載完,通過ftp之類的工具傳上去),gz文件的話通過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è)新的用戶組,文章最后有Elastic在linux安裝的踩坑記錄
Logstash
Logstash 是開源的服務(wù)器端數(shù)據(jù)處理管道,能夠同時(shí)從多個(gè)來源采集數(shù)據(jù),轉(zhuǎn)換數(shù)據(jù),然后將數(shù)據(jù)發(fā)送到您最喜歡的 “存儲(chǔ)庫” 中。(我們的存儲(chǔ)庫當(dāng)然是 Elasticsearch。)-官方賣萌
「1. 軟件安裝」
Mac安裝:
brew?install?logstashlinux安裝:
[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??Speed 100??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. 修改配置文件」
vim?/etc/logstash.conf「conf文件,指定要使用的插件,和配置對(duì)應(yīng)的elasticsearch的hosts」
input?{?stdin?{?}?} output?{elasticsearch?{?hosts?=>?["localhost:9200"]?}stdout?{?codec?=>?rubydebug?} }「3. 運(yùn)行」
bin/logstash?-f?logstash.conf「4. 訪問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è)人購買的是騰訊云1G內(nèi)存的服務(wù)器(如果是壕,請(qǐng)隨意購買更高的配置=-=),已經(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]#?ll total?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
「1. 軟件安裝」
Kibana 讓您能夠可視化 Elasticsearch 中的數(shù)據(jù)并操作 Elastic Stack,因此您可以在這里解開任何疑問:例如,為何會(huì)在凌晨 2:00 被傳呼,雨水會(huì)對(duì)季度數(shù)據(jù)造成怎樣的影響。(而且展示的圖標(biāo)十分酷炫)
Mac安裝
brew?install?kibanalinux安裝,官方下載地址:
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「在這一步,有可能下載速度奇慢,所以我本地下載好之后,通過rz命令傳輸?shù)椒?wù)器」
[root@VM_234_23_centos?app]#?rz rz?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. 修改配置」
修改 config/kibana.yml 配置文件,設(shè)置 elasticsearch.url 指向 Elasticsearch 實(shí)例。
如果跟我一樣使用默認(rèn)的配置,可以不需要修改該文件
「3. 啟動(dòng)」
[root@VM_234_23_centos?kibana]#?./bin/kibana「4. 訪問?http://localhost:5601/app/kibana#/home?_g=()」
「界面顯示了這么多功能,下面通過整合SLF4J+LogBack」
整合Spring+Logstash
「1. 修改logstash.conf后,重新啟動(dòng)logstash」
input?{?#?stdin?{?}tcp?{?#?host:port就是上面appender中的?destination,#?這里其實(shí)把logstash作為服務(wù),開啟9250端口接收logback發(fā)出的消息?host?=>?"127.0.0.1"?port?=>?9250?mode?=>?"server"?tags?=>?["tags"]?codec?=>?json_lines?} } output?{elasticsearch?{?hosts?=>?["localhost:9200"]?}stdout?{?codec?=>?rubydebug?} }「2. 在Java應(yīng)用中引用依賴」
<dependency><groupId>net.logstash.logback</groupId><artifactId>logstash-logback-encoder</artifactId><version>5.2</version> </dependency>「3. 在Logback.xml中配置日志輸出」
<!--日志導(dǎo)出的到?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"?的作用是指定創(chuàng)建索引的名字時(shí)用,并且在生成的文檔中會(huì)多了這個(gè)字段??--><customFields>{"appname":"ye_test"}</customFields></encoder> </appender>??<root?level="INFO"><appender-ref?ref="stash"/> </root>「由于我在第一步驟中,沒有指定對(duì)應(yīng)的index,所以在服務(wù)啟動(dòng)的時(shí)候,日志采集器Logstash幫我自動(dòng)創(chuàng)建了logstash-timestamp的index」
「4. 在kibana中添加index索引」
「5. 在左邊discover中查看索引信息」
「6. 添加可視化圖表Visualize」
「還有更多功能還在探索中,首先環(huán)境搭起來才會(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)限登錄
解決方案:切換用戶
[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?-?,這樣才能獲得環(huán)境變量 [root@VM_234_23_centos?~]#?sudo?su?-?esException in thread “main” java.nio.file.AccessDeniedException:
錯(cuò)誤原因:使用非 root用戶啟動(dòng)ES,而該用戶的文件權(quán)限不足而被拒絕執(zhí)行。
解決方法:chown -R 用戶名:用戶名 文件(目錄)名
例如: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]
[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需要修改虛擬內(nèi)存的大小(在root權(quán)限下)
[root@VM_234_23_centos?elasticsearch]#?vim?/etc/sysctl.conf #?插入下列代碼后保存退出 vm.max_map_count=655360 [root@VM_234_23_centos?elasticsearch]#?sysctl?-p #?最后重啟elastricsearch參考文章
ELK 快速指南
Mac上搭建ELK
通過ELK快速搭建一個(gè)你可能需要的集中化日志平臺(tái)
Logback JSON encoder
elasticSearch安裝常見錯(cuò)誤或問題
Linux中root用戶找不到JAVA_HOME
遠(yuǎn)程連接elasticsearch遇到的問題
springboot+redis+Interceptor+自定義annotation實(shí)現(xiàn)接口自動(dòng)冪等
100W個(gè)微信紅包封面,速度領(lǐng)取!!!
API接口的安全設(shè)計(jì)驗(yàn)證:ticket,簽名,時(shí)間戳
牛逼,一份基于SSM框架實(shí)現(xiàn)的支付寶支付功能,附完整源代碼...
Redis、Kafka或RabbitMQ,哪個(gè)更和微服務(wù)更般配?
JDK 16 即將發(fā)布,看完這些新特性,我感覺已經(jīng)學(xué)不動(dòng)了..
Redis 分布式鎖使用不當(dāng),超賣了100瓶飛天茅臺(tái)!!!
SQL優(yōu)化最干貨總結(jié)(2021最新版)
點(diǎn)擊閱讀全文前往微服務(wù)電商教程
總結(jié)
以上是生活随笔為你收集整理的SpringBoot整合Elastricsearch + LogStash + Kibana太简单了!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在 HTML5 中捕获音频和视频
- 下一篇: 【LOJ6247】九个太阳(单位根反演)