欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?boot整合ELK詳細(xì)過程

 更新時(shí)間:2024年10月10日 15:45:55   作者:阿賈克斯的黎明  
ELK是由Elasticsearch、Logstash和Kibana三個(gè)開源軟件組成的技術(shù)堆棧,主要用于數(shù)據(jù)的存儲(chǔ)、處理和可視化,本文給大家介紹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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論