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

文章插圖
成功啦~

    推薦閱讀