作者:JingQ
来源:
在看大型网站的中间件技术,对于Elasticsearch有点兴趣,所以将配置流程记录了一下。
为什么要用ELK
“ELK实际上是三个工具,Elastricsearch + LogStash + Kibana,通过ELK,用来收集日志还有进行日志分析,最后通过可视化UI进行展示。一开始业务量比较小的时候,通过简单的SLF4J+Logger在服务器打印日志,通过grep进行简单查询,但是随着业务量增加,数据量也会不断增加,所以使用ELK可以进行大数量的日志收集和分析
”
简单画了一下架构图
在环境配置中,主要介绍Mac和linux配置,windows系统大致相同,当然,前提是大家都安装了JDK1.8及以上版本~
[root@VM_234_23_centos~]#java-versionjavaversion"1.8.0_161"Java(TM)SERuntimeEnvironment(build1.8.0_161-b12)JavaHotSpot(TM)64-BitServerVM(build25.161-b12,mixedmode)
“注意
高版本的ELK同样需要高版本的JDK支持,本文配置的ELK版本是6.0+,所以需要的JDK版本不小于1.8
”
ElasticSearch
“Elasticsearch 是一个分布式的 RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。
”
Mac安装和运行
安装:brew install elasticsearch运行:elasticsearch
linux: 从Elasticsearch官方地址下载(也可以下载完,通过ftp之类的工具传上去),gz文件的话通过tar进行解压缩,然后进入bin目录下运行软件
[root@VM_234_23_centosapp]#curl-L-Ohttps://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz[root@VM_234_23_centosapp]#tar-zxvfelasticsearch-6.2.4.tar.gz[root@VM_234_23_centosapp]#cdelasticsearch-6.2.4[root@VM_234_23_centoselasticsearch-6.2.4]#./bin/elasticsearch
“注意
在Linux机器上,运行elasticsearch需要一个新的用户组,文章最后有Elastic在linux安装的踩坑记录
”
Logstash
“Logstash 是开源的服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的 “存储库” 中。(我们的存储库当然是 Elasticsearch。)-官方卖萌
”
1. 软件安装
Mac安装:
brewinstalllogstash
linux安装:
[root@VM_234_23_centosapp]#curl-L-Ohttps://artifacts.elastic.co/downloads/logstash/logstash-6.3.2.tar.gz%Total%Received%XferdAverageSpeedTimeTimeTimeCurrentDloadUploadTotalSpentLeftSpeed100137M100137M005849k00:00:240:00:24--:--:--6597k[root@VM_234_23_centosapp]#tar-zxvflogstash-6.3.2.tar.gz
2. 修改配置文件
vim/etc/logstash.conf
conf文件,指定要使用的插件,和配置对应的elasticsearch的hosts
input{stdin{}}output{elasticsearch{hosts=>["localhost:9200"]}stdout{codec=>rubydebug}}
3. 运行
bin/logstash-flogstash.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":"-04-12T22:29:17Z","build_sha":"a425a422e03087ac34ad6949f7c95ec6d27faf14","build_snapshot":false}
在elasticsearch日志中,也能看到logstash正常加入的日志
[-08-16T14:08:36,436][INFO][o.e.c.m.MetaDataIndexTemplateService][f2s1SD8]addingtemplate[logstash]forindexpatterns[logstash-*]
看到这种返回值,表示已经成功安装和启动
“踩坑
在运行的那一步,有可能遇到内存分配错误
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error=’Cannot allocate memory’ (errno=12)
这个错误很明显就是内存不足,由于个人购买的是腾讯云1G内存的服务器(如果是壕,请随意购买更高的配置=-=),已经运行了elasticsearch,导致logstash分配不到足够的内存,所以最后要修改一下jvm配置。
”
[root@VM_234_23_centoslogstash-6.3.2]#cdconfig/[root@VM_234_23_centosconfig]#lltotal28-rw-r--r--1rootroot1846Jul:19jvm.options-rw-r--r--1rootroot4466Jul:19log4j2.properties-rw-r--r--1rootroot8097Jul:19logstash.yml-rw-r--r--1rootroot3244Jul:19pipelines.yml-rw-r--r--1rootroot1696Jul:19startup.options[root@VM_234_23_centosconfig]#vimjvm.options
将-Xms1g -Xmx1g修改为
-Xms256m-Xmx256m
然后就能正常启动了~~
Kibana
1. 软件安装
“Kibana 让您能够可视化 Elasticsearch 中的数据并操作 Elastic Stack,因此您可以在这里解开任何疑问:例如,为何会在凌晨 2:00 被传呼,雨水会对季度数据造成怎样的影响。(而且展示的图标十分酷炫)
”
Mac安装
brewinstallkibana
linux安装,官方下载地址
[root@VM_234_23_centosapp]#curl-L-Ohttps://artifacts.elastic.co/downloads/kibana/kibana-6.3.2-linux-x86_64.tar.gz%Total%Received%XferdAverageSpeedTimeTimeTimeCurrentDloadUploadTotalSpentLeftSpeed0195M0271k001923502:57:540:00:142:57:4026393
在这一步,有可能下载速度奇慢,所以我本地下载好之后,通过rz命令传输到服务器
[root@VM_234_23_centosapp]#rzrzwaitingtoreceive.Startingzmodemtransfer.PressCtrl+Ctocancel.Transferringkibana-6.3.2-linux-x86_64.tar.gz...100%19KB751KB/sec00:04:270Errors[root@VM_234_23_centosapp]#tar-zxvfkibana-6.3.2-linux-x86_64.tar.gz
2. 修改配置
“修改 config/kibana.yml 配置文件,设置 elasticsearch.url 指向 Elasticsearch 实例。
如果跟我一样使用默认的配置,可以不需要修改该文件
”
3. 启动
[root@VM_234_23_centoskibana]#./bin/kibana
4. 访问 http://localhost:5601/app/kibana#/home?_g=()
界面显示了这么多功能,下面通过整合SLF4J+LogBack
整合Spring+Logstash
1. 修改logstash.conf后,重新启动logstash
input{#stdin{}tcp{#host:port就是上面appender中的destination,#这里其实把logstash作为服务,开启9250端口接收logback发出的消息host=>"127.0.0.1"port=>9250mode=>"server"tags=>["tags"]codec=>json_lines}}output{elasticsearch{hosts=>["localhost:9200"]}stdout{codec=>rubydebug}}
2. 在Java应用中引用依赖
<dependency><groupId>net.logstash.logback</groupId><artifactId>logstash-logback-encoder</artifactId><version>5.2</version></dependency>
3. 在Logback.xml中配置日志输出
<!--日志导出的到Logstash--><appendername="stash"class="net.logstash.logback.appender.LogstashTcpSocketAppender"><destination>localhost:9250</destination><!--encoder必须配置,有多种可选--><encodercharset="UTF-8"class="net.logstash.logback.encoder.LogstashEncoder"><!--"appname":"ye_test"的作用是指定创建索引的名字时用,并且在生成的文档中会多了这个字段--><customFields>{"appname":"ye_test"}</customFields></encoder></appender><rootlevel="INFO"><appender-refref="stash"/></root>
由于我在第一步骤中,没有指定对应的index,所以在服务启动的时候,日志采集器Logstash帮我自动创建了logstash-timestamp的index。
4. 在kibana中添加index索引
5. 在左边discover中查看索引信息
6. 添加可视化图表Visualize
还有更多功能还在探索中,首先环境搭起来才会用动力继续去学习~
踩坑记录
启动报错
“uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
”
原因:不能使用Root权限登录
解决方案:切换用户
[root@VM_234_23_centos~]#groupaddes[root@VM_234_23_centos~]#useraddes-ges-pes[root@VM_234_23_centos~]#chownes:es/home/app/elasticsearch/#切换用户,记得su-,这样才能获得环境变量[root@VM_234_23_centos~]#sudosu-es
“Exception in thread “main” java.nio.file.AccessDeniedException:
”
错误原因:使用非 root用户启动ES,而该用户的文件权限不足而被拒绝执行。
解决方法:chown -R 用户名:用户名 文件(目录)名
例如:chown -R abc:abc searchengine 再启动ES就正常了
elasticsearch启动后报Killed
[-07-13T10:19:44,775][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[aggs-matrix-stats][-07-13T10:19:44,779][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[analysis-common][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[ingest-common][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[lang-expression][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[lang-mustache][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[lang-painless][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[mapper-extras][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[parent-join][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[percolator][-07-13T10:19:44,780][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[rank-eval][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[reindex][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[repository-url][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[transport-netty4][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[tribe][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-core][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-deprecation][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-graph][-07-13T10:19:44,781][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-logstash][-07-13T10:19:44,782][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-ml][-07-13T10:19:44,782][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-monitoring][-07-13T10:19:44,782][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-rollup][-07-13T10:19:44,782][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-security][-07-13T10:19:44,782][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-sql][-07-13T10:19:44,782][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-upgrade][-07-13T10:19:44,782][INFO][o.e.p.PluginsService][f2s1SD8]loadedmodule[x-pack-watcher][-07-13T10:19:44,783][INFO][o.e.p.PluginsService][f2s1SD8]nopluginsloadedKilled
修改config目录下的jvm.options,将堆的大小设置小一点
#Xmsrepresentstheinitialsizeoftotalheapspace#Xmxrepresentsthemaximumsizeoftotalheapspace-Xms512m-Xmx512m
虚拟内存不足
“max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
”
[-07-13T14:02:06,749][DEBUG][o.e.a.ActionModule]UsingRESTwrapperfrompluginorg.elasticsearch.xpack.security.Security[-07-13T14:02:07,249][INFO][o.e.d.DiscoveryModule][f2s1SD8]usingdiscoverytype[zen][-07-13T14:02:09,173][INFO][o.e.n.Node][f2s1SD8]initialized[-07-13T14:02:09,174][INFO][o.e.n.Node][f2s1SD8]starting...[-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}[-07-13T14:02:09,575][INFO][o.e.b.BootstrapChecks][f2s1SD8]boundorpublishingtoanon-loopbackaddress,enforcingbootstrapchecksERROR:[1]bootstrapchecksfailed[1]:maxvirtualmemoryareasvm.max_map_count[65530]istoolow,increasetoatleast[262144][-07-13T14:02:09,621][INFO][o.e.n.Node][f2s1SD8]stopping...[-07-13T14:02:09,726][INFO][o.e.n.Node][f2s1SD8]stopped[-07-13T14:02:09,726][INFO][o.e.n.Node][f2s1SD8]closing...[-07-13T14:02:09,744][INFO][o.e.n.Node][f2s1SD8]closed
需要修改虚拟内存的大小(在root权限下)
[root@VM_234_23_centoselasticsearch]#vim/etc/sysctl.conf#插入下列代码后保存退出vm.max_map_count=655360[root@VM_234_23_centoselasticsearch]#sysctl-p#最后重启elastricsearch
BAT等大厂Java面试经验总结 想获取 Java大厂面试题学习资料扫下方二维码回复「BAT」就好了回复【加群】获取github掘金交流群回复【电子书】获取电子书教程回复【C】获取全套C语言学习知识手册回复【Java】获取java相关的视频教程和资料回复【爬虫】获取SpringCloud相关多的学习资料回复【Python】即可获得Python基础到进阶的学习教程回复【idea破解】即可获得intellij idea相关的破解教程回复【BAT】即可获得intellij idea相关的破解教程关注我gitHub掘金,每天发掘一篇好项目,学习技术不迷路!回复【idea激活】即可获得idea的激活方式回复【Java】获取java相关的视频教程和资料回复【SpringCloud】获取SpringCloud相关多的学习资料回复【python】获取全套0基础Python知识手册回复【】获取java相关面试题教程回复【加群】即可加入终端研发部相关的技术交流群为什么HTTPS是安全的因为BitMap,白白搭进去8台服务器...《某厂内部SQL大全 》.PDF字节跳动一面:i++ 是线程安全的吗?大家好,欢迎加我微信,很高兴认识你!在华为鸿蒙 OS 上尝鲜,我的第一个“hello world”,起飞!相信自己,没有做不到的,只有想不到的在这里获得的不仅仅是技术!喜欢就给个“在看”