Spring?boot整合ELK詳細(xì)過程
ELK 是三個(gè)開源軟件的組合,分別是:Elasticsearch、Logstash 和 Kibana 存數(shù)據(jù) 取數(shù)據(jù) 看數(shù)據(jù)
Elasticsearch 是一個(gè)分布式的搜索和分析引擎,用于存儲(chǔ)、搜索和分析大量的數(shù)據(jù)。
Logstash 是一個(gè)數(shù)據(jù)收集引擎,用于從各種來源收集數(shù)據(jù),并對(duì)數(shù)據(jù)進(jìn)行處理和轉(zhuǎn)換,然后將其發(fā)送到指定的目的地,如 Elasticsearch。
Kibana 是一個(gè)數(shù)據(jù)可視化平臺(tái),與 Elasticsearch 集成,用于創(chuàng)建圖表、儀表盤和搜索界面,以便直觀地分析和理解數(shù)據(jù)。
ELK 堆棧通常用于集中式日志管理、實(shí)時(shí)數(shù)據(jù)分析、監(jiān)控系統(tǒng)性能等場景,幫助用戶快速有效地處理和理解大量的結(jié)構(gòu)化和非結(jié)構(gòu)化數(shù)據(jù)。
要在 Java 項(xiàng)目中整合 ELK(Elasticsearch、Logstash、Kibana),以下是一般的步驟:
引入相關(guān)依賴
首先,您需要在您的 Java 項(xiàng)目中添加適當(dāng)?shù)囊蕾?,以便與 Elasticsearch 進(jìn)行交互??梢允?strong>用 elasticsearch-rest-high-level-client
庫來與 Elasticsearch 通信。
如果使用 Maven 項(xiàng)目,添加以下依賴:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.17.3</version> </dependency>
創(chuàng)建 Elasticsearch 客戶端
import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; public class ElasticsearchClient { public RestHighLevelClient getClient() { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); return client; } public static void main(String[] args) { ElasticsearchClient elasticsearchClient = new ElasticsearchClient(); elasticsearchClient.getClient(); } }
將日志數(shù)據(jù)發(fā)送到 Elasticsearch
在您的日志記錄邏輯中,獲取日志數(shù)據(jù),并使用上面創(chuàng)建的客戶端將數(shù)據(jù)發(fā)送到 Elasticsearch。
如對(duì)ELK進(jìn)行增刪改查的話:
import org.apache.http.HttpHost; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; public class ElasticsearchCRUD { private RestHighLevelClient client; public ElasticsearchCRUD() { client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); } public void closeClient() throws IOException { client.close(); } public void insertDocument(String indexName, String id, String jsonData) throws IOException { IndexRequest request = new IndexRequest(indexName) .id(id) .source(jsonData, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println("Document inserted. Response: " + response.getResult()); } public void getDocument(String indexName, String id) throws IOException { GetRequest request = new GetRequest(indexName, id); GetResponse response = client.get(request, RequestOptions.DEFAULT); if (response.isExists()) { System.out.println("Document found: " + response.getSourceAsString()); } else { System.out.println("Document not found"); } } public void updateDocument(String indexName, String id, String updateJsonData) throws IOException { UpdateRequest request = new UpdateRequest(indexName, id) .doc(updateJsonData, XContentType.JSON); UpdateResponse response = client.update(request, RequestOptions.DEFAULT); System.out.println("Document updated. Response: " + response.getResult()); } public void deleteDocument(String indexName, String id) throws IOException { DeleteRequest request = new DeleteRequest(indexName, id); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); System.out.println("Document deleted. Response: " + response.getResult()); } public static void main(String[] args) { ElasticsearchCRUD elasticsearchCRUD = new ElasticsearchCRUD(); String indexName = "your_index_name"; String id = "your_document_id"; // 插入數(shù)據(jù) String jsonData = "{\"name\": \"John Doe\", \"age\": 30}"; try { elasticsearchCRUD.insertDocument(indexName, id, jsonData); } catch (IOException e) { e.printStackTrace(); } // 獲取數(shù)據(jù) try { elasticsearchCRUD.getDocument(indexName, id); } catch (IOException e) { e.pr
上述代碼中的 localhost
和端口 9200
是默認(rèn)的 Elasticsearch 服務(wù)地址和端口。您需要根據(jù)實(shí)際情況進(jìn)行修改。同時(shí),確保已經(jīng)正確啟動(dòng)和配置了 Elasticsearch 服務(wù),并創(chuàng)建了相應(yīng)的索引。
此外,代碼中的示例數(shù)據(jù)和索引名稱也需要根據(jù)您的實(shí)際需求進(jìn)行調(diào)整。
對(duì)于 Logstash,您可以配置 Logstash 來從數(shù)據(jù)源(如文件、網(wǎng)絡(luò)流等)接收數(shù)據(jù),并將其轉(zhuǎn)發(fā)到 Elasticsearch。您需要?jiǎng)?chuàng)建一個(gè) Logstash 配置文件(.conf
),指定輸入、過濾和輸出的相關(guān)設(shè)置。
最后,Kibana 用于可視化和分析存儲(chǔ)在 Elasticsearch 中的數(shù)據(jù)。您只需在服務(wù)器上安裝和配置好 Kibana,并將其連接到相同的 Elasticsearch 實(shí)例,就可以通過 Kibana 創(chuàng)建儀表板、搜索和分析數(shù)據(jù)了。
到此這篇關(guān)于Spring boot整合ELK詳細(xì)過程的文章就介紹到這了,更多相關(guān)Spring boot整合ELK內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合ELK做日志超完整詳細(xì)教程
- Spring?boot整合ELK詳細(xì)過程
- Spring?Boot整合ELK實(shí)現(xiàn)日志采集與監(jiān)控
- Spring Boot 搭建 ELK正確看日志的配置流程
- SpringBoot應(yīng)用整合ELK實(shí)現(xiàn)日志收集的示例代碼
- SpringBoot2.3集成ELK7.1.0的示例代碼
- 詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn)
- springboot向elk寫日志實(shí)現(xiàn)過程
- Spring Boot 使用 logback、logstash、ELK 記錄日志文件的方法
相關(guān)文章
Java連接數(shù)據(jù)庫實(shí)現(xiàn)方式
文章講述了Java連接MySQL數(shù)據(jù)庫的詳細(xì)步驟,包括下載和導(dǎo)入JDBC驅(qū)動(dòng)、創(chuàng)建數(shù)據(jù)庫和表、以及編寫連接和讀取數(shù)據(jù)的代碼2024-11-11Linux環(huán)境下的Java(JDBC)連接openGauss數(shù)據(jù)庫實(shí)踐記錄
這篇文章主要介紹了Linux環(huán)境下的Java(JDBC)連接openGauss數(shù)據(jù)庫實(shí)踐記錄,需要的朋友可以參考下2022-11-11Java 設(shè)計(jì)模式之責(zé)任鏈模式及異步責(zé)任鏈詳解
顧名思義,責(zé)任鏈模式(Chain of Responsibility Pattern)為請(qǐng)求創(chuàng)建了一個(gè)接收者對(duì)象的鏈。這種模式給予請(qǐng)求的類型,對(duì)請(qǐng)求的發(fā)送者和接收者進(jìn)行解耦。這種類型的設(shè)計(jì)模式屬于行為型模式2021-11-11Springboot @Transactional使用時(shí)需注意的幾個(gè)問題記錄
本文詳細(xì)介紹了Spring Boot中使用`@Transactional`注解進(jìn)行事務(wù)管理的多個(gè)方面,包括事務(wù)的隔離級(jí)別(如REPEATABLE_READ)和傳播行為(如REQUIRES_NEW),并指出了在同一個(gè)類中調(diào)用事務(wù)方法時(shí)可能遇到的問題以及解決方案,感興趣的朋友跟隨小編一起看看吧2025-01-01springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn)
這篇文章主要介紹了springboot短信驗(yàn)證碼登錄功能的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Java生成二維碼的兩種實(shí)現(xiàn)方式(基于Spring?Boot)
這篇文章主要給大家介紹了關(guān)于Java生成二維碼的兩種實(shí)現(xiàn)方式,文中的代碼基于Spring?Boot,本文基于JAVA環(huán)境,以SpringBoot框架為基礎(chǔ)開發(fā),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07Spring Boot Actuator監(jiān)控的簡單使用方法示例代碼詳解
這篇文章主要介紹了Spring Boot Actuator監(jiān)控的簡單使用,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06