隨著企業系統規模的不斷擴大,系統的日誌越來越龐大,如果沒有一個可靠的日誌收集和分析系統,就很難有效地監控和維護系統。本文將介紹如何基於Spring Boot和Flume建立一個高效的日誌收集和分析系統。
- 前置條件
在開始之前,需要安裝並設定以下軟體:
- JDK 8 以上版本
- Maven 3.3 或以上版本
- Apache Flume 1.9.0 或以上版本
- Elasticsearch 7.6.2 或以上版本
- Kibana 7.6.2 或以上版本
- Spring Boot應用程式設定
首先,我們需要建立一個Spring Boot應用,並且加入所需的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
在application.properties文件中,新增以下配置:
# 应用端口号 server.port=8080 # log4j2配置 logging.config=classpath:log4j2.xml # flume配置 flume.agentName=myflume flume.sourceType=avro flume.clientType=load-balancing flume.hosts=localhost:41414 # elasticsearch配置 spring.elasticsearch.rest.uris=http://localhost:9200
以上配置中,我們指定了應用程式的連接埠號碼、log4j2設定檔、Flume的相關設定和Elasticsearch的存取URI。
- 日誌收集器
為了將應用程式日誌傳送到Flume,我們需要建立一個自訂的log4j2 Appender。
@Plugin(name = "Flume", category = "Core", elementType = "appender", printObject = true) public class FlumeAppender extends AbstractAppender { private static final ObjectMapper MAPPER = new ObjectMapper(); private final FlumeClient client; private final String sourceType; protected FlumeAppender(String name, Filter filter, Layout<? extends Serializable> layout, FlumeClient client, String sourceType) { super(name, filter, layout, true); this.client = client; this.sourceType = sourceType; } @PluginFactory public static FlumeAppender createAppender(@PluginAttr("name") String name, @PluginElement("Filters") Filter filter, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginAttr("sourceType") String sourceType, @PluginAttr("hosts") String hosts) { if (name == null) { LOGGER.error("FlumeAppender missing name"); return null; } if (client == null) { LOGGER.error("FlumeAppender missing client"); return null; } return new FlumeAppender(name, filter, layout, createClient(hosts), sourceType); } private static FlumeClient createClient(String hosts) { LoadBalancingRpcClient rpcClient = new LoadBalancingRpcClient(); String[] hostArray = hosts.split(","); for (String host : hostArray) { String[] hostParts = host.split(":"); rpcClient.addHost(new InetSocketAddress(hostParts[0], Integer.parseInt(hostParts[1]))); } Properties props = new Properties(); props.setProperty(RpcClientConfigurationConstants.CONFIG_CLIENT_TYPE, "default_loadbalance"); props.setProperty(RpcClientConfigurationConstants.CONFIG_HOSTS, hosts); props.setProperty(RpcClientConfigurationConstants.CONFIG_MAX_BACKOFF, "10000"); AvroEventSerializer serializer = new AvroEventSerializer(); serializer.configure(props, false); return new FlumeClient(rpcClient, serializer); } @Override public void append(LogEvent event) { try { byte[] body = ((StringLayout) this.getLayout()).toByteArray(event); Map<String, String> headers = new HashMap<>(); headers.put("timestamp", Long.toString(event.getTimeMillis())); headers.put("source", "log4j"); headers.put("sourceType", sourceType); Event flumeEvent = EventBuilder.withBody(body, headers); client.sendEvent(flumeEvent); } catch (Exception e) { LOGGER.error("Failed to send event to Flume", e); } } }
以上程式碼中,我們實作了一個log4j2 Appender,它會將日誌事件打包成一個Flume Event,並傳送到Flume伺服器。
建立一個log4j2設定文件,設定FlumeAppender。
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Flume name="flume" sourceType="spring-boot" hosts="${flume.hosts}"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Flume> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="flume"/> </Root> </Loggers> </Configuration>
在這個log4j2設定檔中,我們定義了一個FlumeAppender,並在Root Logger中引用它。
- Flume設定
我們需要設定Flume,在Flume Agent中接收從應用程式傳送的日誌訊息,並將它們傳送到Elasticsearch。
建立一個Flume設定文件,如下所示。
# Define the agent name and the agent sources and sinks myflume.sources = mysource myflume.sinks = mysink myflume.channels = channel1 # Define the source myflume.sources.mysource.type = avro myflume.sources.mysource.bind = 0.0.0.0 myflume.sources.mysource.port = 41414 # Define the channel myflume.channels.channel1.type = memory myflume.channels.channel1.capacity = 10000 myflume.channels.channel1.transactionCapacity = 1000 # Define the sink myflume.sinks.mysink.type = org.elasticsearch.hadoop.flume.ElasticsearchSink myflume.sinks.mysink.hostNames = localhost:9200 myflume.sinks.mysink.indexName = ${type}-%{+YYYY.MM.dd} myflume.sinks.mysink.batchSize = 1000 myflume.sinks.mysink.typeName = ${type} # Link the source and sink with the channel myflume.sources.mysource.channels = channel1 myflume.sinks.mysink.channel = channel1
在Flume設定檔中,我們定義了一個agent,一個source和一個sink。 source是avro類型,綁定到41414埠上,channel1是一個memory類型,capacity為10000,transactionCapacity為1000。 sink是一個ElasticsearchSink類型,在本地主機的9200連接埠上建立一個名為type的索引,在1000個事件達到時批次提交到Elasticsearch。
- Elasticsearch和Kibana配置
最後,我們需要設定Elasticsearch和Kibana。在Elasticsearch中,我們需要建立一個與Flume設定檔中定義的索引名稱相符的索引。
在Kibana中,我們需要建立一個索引模式。在Kibana的主選單中,選擇"Management",然後選擇"Kibana"。在Kibana索引模式中,選擇"Create Index Pattern"。輸入Flume設定檔中定義的索引名稱,並依照提示進行設定。
我們還需要為Kibana建立一個Dashboard,以便查看應用程式的日誌訊息。在Kibana的主選單中,選擇"Dashboard",然後選擇"Create Dashboard"。在"Visualizations"標籤中,選擇"Add a visualization"。選擇"Data Table",然後配置所需的欄位和視覺化選項。
- 結論
在本文中,我們介紹如何使用Spring Boot和Flume建立一個高效的日誌收集和分析系統。我們實作了一個自訂的log4j2 Appender,將應用程式的日誌事件傳送到Flume伺服器,並使用Elasticsearch和Kibana進行日誌分析和視覺化。希望這篇文章能對你建立自己的日誌收集和分析系統有所幫助。
以上是基於Spring Boot和Flume建置日誌收集和分析系統的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

Atom編輯器mac版下載
最受歡迎的的開源編輯器