SpringBoot整合ES+Kibana

前言:最近在写一个HTTP代理服务器,记录日志使用的是ES,所以涉及到SpringBoot和ES的整合,整合完毕后又涉及到数据可视化分析,所以使用了Kibana进行管理,有些坑,需要记录一下
SpringBoot整合ES【SpringBoot整合ES+Kibana】gradle项目,在build.gradle文件中引入依赖`dependencies {//........// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearchimplementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-elasticsearch'
}ES和MySQL有点像,ES中索引的概念对应MySQL中表的概念,ES中文档的概念对应MySQL中的一行数据 。因此我们需要定义一个索引import java.util.Map;
@Data@Document(indexName = "request_log", createIndex = false)public class RequestIndex {private Long id;private String domain;private String ip;private HttpMethod method;private String uri;private Map<String, Object> headers;private byte[] body;这里有个坑,给我在后面整合Kibana的时候造成了困难 , @Document注解中的createIndex的默认值为true,可省略 。所以在后续插入数据时,request_log索引不存在时,会自动创建一个request_log索引,而这个默认创建的索引并没有@timestamp字段,所以导致Kibana的柱状图无法生成,影响数据可视化 因此,我建议这里直接手动设置createIndex的值为false 我们另外手动创建request_log索引 , 这样可以手动开启@timestamp字段 在ES7中,创建@timestamp的方法与之前的版本不同,使用的是pipeline 首先创建一个pipelinePUT _ingest/pipeline/timestamp_pipeline{"description": "Adds a field to a document with the time of ingestion","processors": [{"set": {"field": "@timestamp","value": "{{_ingest.timestamp}}"}}]}这样我们就成功创建了一个名为timestamp_pipeline的pipeline 紧接着我们创建request_log索引,并把这个pipeline配置到这个索引上PUT request_log{"settings": {"default_pipeline": "timestamp_pipeline"}}`至此,我们就创建索引成功了,看一下日志
SpringBoot整合ES+Kibana

文章插图
果然成功出现了@timestamp字段
Kibana搭建Kibana的版本必须和ES的版本一致 , 否则无法启动Kibana的配置文件中,主要需要配置以下几项:server.hostserver.portelasticsearch.hostselasticsearch.usernameelasticsearch.passwordi18n.locale: "zh-CN"其中,elasticsearch.username不能使用elastic用户,由于ES配置了xpack,所以创建了其他用户访问 GET /_xpack/security/user从名字看起来,可以使用kibana和kibana_system,但是看提示,kibana用户已经被弃用了,建议使用kibana_system
SpringBoot整合ES+Kibana

文章插图
搭建好Kibana后,进入Stack Management,索引模式,创建索引模式
SpringBoot整合ES+Kibana

文章插图
以@timestamp为时间戳字段
SpringBoot整合ES+Kibana

文章插图
成功啦~

    推荐阅读