目标描述
基于springboot开发的一套接口服务部署在TOMCAT中, 需要对接口的日常运行情况的数据进行收集和展示,便于及时掌握服务的实时运行状态,对异常情况及时告警。 需要统计的数据如下:
- 访问请求数统计
- 访问流量统计
- 访问处理响应时间统计
- 统计接口的错误码分布情况
- 统计访问最频繁的接口
- 统计访问处理时间最久的接口
- 统计并发访问频率最高的接口
方案简介
Tomcat Access Log中存放了很多有用的信息,将这些信息利用起来,就可以我们的统计目标。 完整流程为: Filebeat 收集Tomcat Access Log,将数据发送到 Logstash,然后 Logstash 对数据进行处理,并将处理后的数据发送到 ES,最后再用 Kibana 进行数据展示。
方案实施
1 配置tomcat access Log
Tomcat Access Log 的展示信息在 conf/server.xml 中进行配置,配置如下:
使用的tomcat版本为8.5,各配置含义如下:
2 安装 ELK,配置Logstash
使用 docker,参照 docker-elk。
git clone https://github.com/deviantony/docker-elk.git
cd docker-elk
修改 ./logstash/pipeline/logstash.conf
input {
beats {
port => 5044
client_inactivity_timeout => 3600
type => "logs"
}
}
filter {
grok {
match => { "message" => "%{IPORHOST:clientip}%{SPACE}%{USER:ident}%{SPACE}%{USER:auth}%{SPACE}[%{HTTPDATE:timestamp}]%{SPACE}"(?:%{WORD:verb}%{SPACE}%{NOTSPACE:request}?%{NOTSPACE:urlparams}(?:%{SPACE}HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})"%{SPACE}%{NUMBER:response}%{SPACE}(?:%{NUMBER:bytes}|-)(?:%{SPACE}(?:%{NUMBER:duration}|-)%{SPACE}(?:%{NUMBER:responstime}|-)%{SPACE})" }
}
if "_grokparsefailure" not in [tags] {
date {
match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
}
mutate {
remove_field => ["message"]
convert => ["bytes", "integer"]
convert => ["duration", "integer"]
}
}
}
output {
elasticsearch {
hosts => "elasticsearch:9200"
user => "elastic"
password => "changeme"
index => "apache-access-log"
}
stdout { codec => rubydebug }
}
input.beats.port 是用来供 filebeat 传输数据的监听接口, output.elasticsearch.hosts/user/password 需要正确设置,elk-docker 中默认为 eleatic/changeme。
Grok表达式可以在(Grok debugger)[http://grokdebug.herokuapp.com/]中调试。
启动ELK
[root@ecs-b84c docker-elk]# pwd
/root/test/docker-elk
[root@ecs-b84c docker-elk]# docker-compose up -d
3 安装并配置 Filebeat
下载并安装
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.2.1-x86_64.rpm
rpm -vi filebeat-7.2.1-x86_64.rpm
编辑配置文件
- type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /root/tomcat/logs/localhost_access*
#- c:programdataelasticsearchlogs*
...省略默认的部分
#================================ Outputs =====================================
# Configure what output to use when sending the data collected by the beat.
#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
# Array of hosts to connect to.
#hosts: ["localhost:9200"]
# Optional protocol and basic auth credentials.
#protocol: "https"
#username: "elastic"
#password: "changeme"
#----------------------------- Logstash output --------------------------------
output.logstash:
# The Logstash hosts
hosts: ["localhost:5044"]
# Optional SSL. By default is off.
# List of root certificates for HTTPS server verifications
#ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]
# Certificate for SSL client authentication
#ssl.certificate: "/etc/pki/client/cert.pem"
# Client Certificate Key
#ssl.key: "/etc/pki/client/cert.key"
启动Filebeat
filebeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]' -E 'output.elasticsearch.protocol="http"' -E 'output.elasticsearch.username="elastic"' -E 'output.elasticsearch.password="changeme"'
service filebeat start
4配置 Kibana
登录htpp://localhost:5601,默认为 eleatic/changeme。 当tomcat的accesslog日志发生变动时,Filebeat 会将日志传输给 Logstash,Logstash 会根据我们定义的 Pattern 将日志进行装换,然后存入 ElasticSearch,我们在 Kibana 中刷新拿到 ElasticSearch 中的 index 就可以配置我们想要的报表了。