在微服务架构中,会部署众多的应用,其中有基础应用,好比:网关,服务发现等。同时还有大量的业务应用。因此,如何有效的收集它们的日志,而且方便查询,同时提供友好的可视化展现,对于应对微服务架构的复杂性有很大的帮助。在高复杂度的系统中,对于定位线上问题,日志很是重要。ELK(ElasticSearch+Logstash+Kibana),可使用说是目前最流行的日志平台构建方案,之因此深受开发者喜好,主要是由于它解决了大规模系统的日志收集的各类痛点。linux
ELK(ElasticSearch+Logstash+Kibana),主要包含三个组件:spring
ElasticSearchjson
Logstashubuntu
Kibana性能优化
ElasticSearch是一个开源的分布式的搜索引擎,它主要基于Apache Lucene。在整个ELK Stack中,ElasticSearch是最核心的组件,它存储数据,而且提供了许多灵活而实用的Rest API,因此,上层应用能够根据须要去查询数据,使用数据,分析数据。在日志平台中,全部的日志数据都存储到ElasticSearch中,借助其强大的搜索能力,能够很灵活的查询日志。架构
Logstash主要用于收集数据,并将数据保存到ElasticSearch中。并发
Logstash有丰富插件,而且易于扩展,因此,可使用Logstash收集到数据后,能够作不少处理,最终再将数据输出到ElasticSearch中。在日志平台中,它主要复杂采集应用的日志。app
Kibana主要负责读取ElasticSearch中的数据,并进行可视化展现。而且,它还自带Tool,能够方便调用ElasticSearch的Rest API。在日志平台中,咱们经过Kibana查看日志。elasticsearch
使用ELK构建了一个日志平台架构:分布式
这是一个最简化版的日志收集架构,不少基于ELK的日志架构是从它演化而来,核心的问题就是日志数据都保存到ElasticSearch中。好比说,能够先将日志收集到Kafka中,而后再由Logstash采集数据输出到ElasticSearch中,引入了Kafka,就给使用数据增长了不少可能性。
系统:Ubuntu16.06 64
去官网下载ElasticSearch、Logstash、Kibana,注意尽可能保持版本一致,此处使用6.0的大版本,为了便于演示,所有ELK程序和Java应用都安装到一台机器上,目录以下:
noone@ubuntu:/opt$ tree -L 1 . ├── elasticsearch-6.0.0 ├── gs-spring-boot-0.1.0.jar ├── kibana-6.0.1-linux-x86_64 ├── logs └── logstash-6.0.1
为了方便管理,使用systemd管理ElasticSearch、Kibana,配置以下:
/etc/systemd/system/elasticsearch.service
[Service] Environment=ES_HOME=/opt/elasticsearch-6.0.0 Environment=ES_PATH_CONF=/opt/elasticsearch-6.0.0/config Environment=PID_DIR=/var/run/elasticsearch WorkingDirectory=/opt/elasticsearch-6.0.0 User=noone Group=nonone ExecStart=/opt/elasticsearch-6.0.0/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet # StandardOutput is configured to redirect to journalctl since # some error messages may be logged in standard output before # elasticsearch logging system is initialized. Elasticsearch # stores its logs in /var/log/elasticsearch and does not use # journalctl by default. If you also want to enable journalctl # logging, you can simply remove the "quiet" option from ExecStart. StandardOutput=journal StandardError=inherit # Specifies the maximum file descriptor number that can be opened by this process LimitNOFILE=65536 # Specifies the maximum number of processes LimitNPROC=4096 # Specifies the maximum size of virtual memory LimitAS=infinity # Specifies the maximum file size LimitFSIZE=infinity # Disable timeout logic and wait until process is stopped TimeoutStopSec=0 # SIGTERM signal is used to stop the Java process KillSignal=SIGTERM # Send the signal only to the JVM rather than its control group KillMode=process # Java process is never killed SendSIGKILL=no # When a JVM receives a SIGTERM signal it exits with code 143 SuccessExitStatus=143 [Install] WantedBy=multi-user.target
/etc/systemd/system/kibana.service
[Unit] Description=Kibana [Service] Type=simple User=noone Environment=CONFIG_PATH=/opt/kibana-6.0.1-linux-x86_64/config/kibana.yml Environment=NODE_ENV=dev ExecStart=/opt/kibana-6.0.1-linux-x86_64/bin/kibana [Install] WantedBy=multi-user.target
建立一个SpringBoot应用,须要在pom.xml
中引入依赖
<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.1</version> </dependency>
而后配置日志,logback-spring.xml
<configuration scan="true"> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="STASH" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>/opt/logs/logback/app.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/opt/logs/logback/app.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>15</maxHistory> </rollingPolicy> <encoder class="net.logstash.logback.encoder.LogstashEncoder"/> </appender> <root level="INFO"> <appender-ref ref="STASH" /> <appender-ref ref="CONSOLE" /> </root> </configuration>
须要注意日志的名字以及路径,这个后续要和Logstash的配置匹配。这样作,主要是将日志按照格式输出到指定的文件中,方便Logstash监控日志文件,实时获取日志数据。
接下来配置Logstash,/opt/logstash-6.0.1/config/logstash.conf
input { file { path => "/opt/logs/logback/*.log" codec => "json" type => "logback" } } output { if [type]=="logback" { elasticsearch { hosts => [ "localhost:9200" ] index => "logback-%{+YYYY.MM.dd}" } } }
启动ElasticSearch和Kibana
sudo systemctl start elasticsearch.service sudo systemctl start kibana.service
启动SpringBoot应用,而后启动Logstash
sudo bin/logstash -f config/logstash.conf
打开Kibana,稍做配置,就能够查询应用的日志了。
本文主要介绍了基于ELK的日志平台搭建,这只是一个最基础的架构,固然,它也不单单是适用于基于SpringCloud的微服务架构。随着系统业务量的提高,能够在此基础上继续演进,适配更多的业务架构,处理海量的日志,而且根据业务需求从日志数据中提取更多的信息。
在此给你们推荐一个Java架构方面的交流学习群:698581634,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系,主要针对Java开发人员提高本身,突破瓶颈,相信你来学习,会有提高和收获。在这个群里会有你须要的内容 朋友们请抓紧时间加入进来吧。