Spring?boot整合ELK詳細過程
ELK 是三個開源軟件的組合,分別是:Elasticsearch、Logstash 和 Kibana 存數(shù)據(jù) 取數(shù)據(jù) 看數(shù)據(jù)
Elasticsearch 是一個分布式的搜索和分析引擎,用于存儲、搜索和分析大量的數(shù)據(jù)。
Logstash 是一個數(shù)據(jù)收集引擎,用于從各種來源收集數(shù)據(jù),并對數(shù)據(jù)進行處理和轉(zhuǎn)換,然后將其發(fā)送到指定的目的地,如 Elasticsearch。
Kibana 是一個數(shù)據(jù)可視化平臺,與 Elasticsearch 集成,用于創(chuàng)建圖表、儀表盤和搜索界面,以便直觀地分析和理解數(shù)據(jù)。
ELK 堆棧通常用于集中式日志管理、實時數(shù)據(jù)分析、監(jiān)控系統(tǒng)性能等場景,幫助用戶快速有效地處理和理解大量的結(jié)構(gòu)化和非結(jié)構(gòu)化數(shù)據(jù)。
要在 Java 項目中整合 ELK(Elasticsearch、Logstash、Kibana),以下是一般的步驟:
引入相關(guān)依賴
首先,您需要在您的 Java 項目中添加適當(dāng)?shù)囊蕾?,以便與 Elasticsearch 進行交互。可以使用 elasticsearch-rest-high-level-client
庫來與 Elasticsearch 通信。
如果使用 Maven 項目,添加以下依賴:
<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。
如對ELK進行增刪改查的話:
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
是默認的 Elasticsearch 服務(wù)地址和端口。您需要根據(jù)實際情況進行修改。同時,確保已經(jīng)正確啟動和配置了 Elasticsearch 服務(wù),并創(chuàng)建了相應(yīng)的索引。
此外,代碼中的示例數(shù)據(jù)和索引名稱也需要根據(jù)您的實際需求進行調(diào)整。
對于 Logstash,您可以配置 Logstash 來從數(shù)據(jù)源(如文件、網(wǎng)絡(luò)流等)接收數(shù)據(jù),并將其轉(zhuǎn)發(fā)到 Elasticsearch。您需要創(chuàng)建一個 Logstash 配置文件(.conf
),指定輸入、過濾和輸出的相關(guān)設(shè)置。
最后,Kibana 用于可視化和分析存儲在 Elasticsearch 中的數(shù)據(jù)。您只需在服務(wù)器上安裝和配置好 Kibana,并將其連接到相同的 Elasticsearch 實例,就可以通過 Kibana 創(chuàng)建儀表板、搜索和分析數(shù)據(jù)了。
到此這篇關(guān)于Spring boot整合ELK詳細過程的文章就介紹到這了,更多相關(guān)Spring boot整合ELK內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux環(huán)境下的Java(JDBC)連接openGauss數(shù)據(jù)庫實踐記錄
這篇文章主要介紹了Linux環(huán)境下的Java(JDBC)連接openGauss數(shù)據(jù)庫實踐記錄,需要的朋友可以參考下2022-11-11Java 設(shè)計模式之責(zé)任鏈模式及異步責(zé)任鏈詳解
顧名思義,責(zé)任鏈模式(Chain of Responsibility Pattern)為請求創(chuàng)建了一個接收者對象的鏈。這種模式給予請求的類型,對請求的發(fā)送者和接收者進行解耦。這種類型的設(shè)計模式屬于行為型模式2021-11-11Springboot @Transactional使用時需注意的幾個問題記錄
本文詳細介紹了Spring Boot中使用`@Transactional`注解進行事務(wù)管理的多個方面,包括事務(wù)的隔離級別(如REPEATABLE_READ)和傳播行為(如REQUIRES_NEW),并指出了在同一個類中調(diào)用事務(wù)方法時可能遇到的問題以及解決方案,感興趣的朋友跟隨小編一起看看吧2025-01-01Java生成二維碼的兩種實現(xiàn)方式(基于Spring?Boot)
這篇文章主要給大家介紹了關(guān)于Java生成二維碼的兩種實現(xiàn)方式,文中的代碼基于Spring?Boot,本文基于JAVA環(huán)境,以SpringBoot框架為基礎(chǔ)開發(fā),文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07Spring Boot Actuator監(jiān)控的簡單使用方法示例代碼詳解
這篇文章主要介紹了Spring Boot Actuator監(jiān)控的簡單使用,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06