<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot 配置 ELK 環(huán)境

          共 9938字,需瀏覽 20分鐘

           ·

          2021-01-07 03:13

          作者: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í)候,通過簡(jiǎn)單的SLF4J+Logger在服務(wù)器打印日志,通過grep進(jìn)行簡(jiǎn)單查詢,但是隨著業(yè)務(wù)量增加,數(shù)據(jù)量也會(huì)不斷增加,所以使用ELK可以進(jìn)行大數(shù)量的日志收集和分析

          簡(jiǎn)單畫了一下架構(gòu)圖

          c1dcd05b316387d3ad203aaa1b186eaf.webp

          「在環(huán)境配置中,主要介紹Mac和linux配置,windows系統(tǒng)大致相同,當(dāng)然,前提是大家都安裝了JDK1.8及以上版本~」

          [root@VM_234_23_centos?~]#?java?-version
          java?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


          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)行:elasticsearch

          linux: 從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?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??Current
          ?????????????????????????????????Dload??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?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??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命令傳輸?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=()」

          bdc2df84127de1aae4d530c11ca74365.webp

          「界面顯示了這么多功能,下面通過整合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.logbackgroupId>
          ??<artifactId>logstash-logback-encoderartifactId>
          ??<version>5.2version>
          dependency>

          「3. 在Logback.xml中配置日志輸出」


          <appender?name="stash"
          ??????????????class="net.logstash.logback.appender.LogstashTcpSocketAppender">

          ???<destination>localhost:9250destination>
          ???
          ???<encoder?charset="UTF-8"
          ????????????class="net.logstash.logback.encoder.LogstashEncoder"?>

          ???????
          ???????<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索引」

          11d8600590b26cd8f87eecb43979e2f1.webp

          「5. 在左邊discover中查看索引信息」

          185bc4f01d4118ecb8f8c26a66a3a26b.webp

          「6. 添加可視化圖表Visualize」

          257b71527b69131405034c050f5a886f.webp

          「還有更多功能還在探索中,首先環(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?-?es

          Exception 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

          參考文章

          1. ELK 快速指南

          2. Mac上搭建ELK

          3. 通過ELK快速搭建一個(gè)你可能需要的集中化日志平臺(tái)

          4. Logback JSON encoder

          5. elasticSearch安裝常見錯(cuò)誤或問題

          6. Linux中root用戶找不到JAVA_HOME

          7. 遠(yuǎn)程連接elasticsearch遇到的問題

          END


          推薦閱讀

          GitHub 下載神器強(qiáng)勢(shì)回歸!

          巧用枚舉來干掉if-else,代碼更優(yōu)雅!

          如何正確訪問Redis中的海量數(shù)據(jù)?服務(wù)才不會(huì)掛掉!

          超硬核!1.6W 字 Redis 面試知識(shí)點(diǎn)總結(jié),建議收藏!

          瀏覽 40
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一级A片免费观看 | 四虎黄色网址 | 四虎做爱| 亚洲无码视 | 日韩黄色电影院 |