在微服务架构中,会部署众多的应用,其中有基础应用,好比:网关,服务发现等。同时还有大量的业务应用。因此,如何有效的收集它们的日志,而且方便查询,同时提供友好的可视化展现,对于应对微服务架构的复杂性有很大的帮助。在高复杂度的系统中,对于定位线上问题,日志很是重要。ELK(ElasticSearch+Logstash+Kibana),可使用说是目前最流行的日志平台构建方案,之因此深受开发者喜好,主要是由于它解决了大规模系统的日志收集的各类痛点。linux
ELK(ElasticSearch+Logstash+Kibana),主要包含三个组件:spring
ElasticSearch是一个开源的分布式的搜索引擎,它主要基于Apache Lucene。在整个ELK Stack中,ElasticSearch是最核心的组件,它存储数据,而且提供了许多灵活而实用的Rest API,因此,上层应用能够根据须要去查询数据,使用数据,分析数据。在日志平台中,全部的日志数据都存储到ElasticSearch中,借助其强大的搜索能力,能够很灵活的查询日志。json
Logstash主要用于收集数据,并将数据保存到ElasticSearch中。ubuntu
Logstash有丰富插件,而且易于扩展,因此,可使用Logstash收集到数据后,能够作不少处理,最终再将数据输出到ElasticSearch中。在日志平台中,它主要复杂采集应用的日志。bash
Kibana主要负责读取ElasticSearch中的数据,并进行可视化展现。而且,它还自带Tool,能够方便调用ElasticSearch的Rest API。在日志平台中,咱们经过Kibana查看日志。架构
使用ELK构建了一个日志平台架构:app
这是一个最简化版的日志收集架构,不少基于ELK的日志架构是从它演化而来,核心的问题就是日志数据都保存到ElasticSearch中。好比说,能够先将日志收集到Kafka中,而后再由Logstash采集数据输出到ElasticSearch中,引入了Kafka,就给使用数据增长了不少可能性。elasticsearch
系统:Ubuntu16.06 64分布式
去官网下载ElasticSearch、Logstash、Kibana,注意尽可能保持版本一致,此处使用6.0的大版本,为了便于演示,所有ELK程序和Java应用都安装到一台机器上,目录以下:spring-boot
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的微服务架构。随着系统业务量的提高,能够在此基础上继续演进,适配更多的业务架构,处理海量的日志,而且根据业务需求从日志数据中提取更多的信息。关注我,了解更多: