Spring Boot 4 Microservices & REST APIs · บทเรียน

การบันทึกเหตุการณ์แบบรวมศูนย์ด้วย ELK Stack

ตั้งค่าและใช้ Elasticsearch, Logstash และ Kibana สำหรับการบันทึกเหตุการณ์และวิเคราะห์บันทึกแบบรวมศูนย์

บทเรียน 1 จาก 311 ขั้นตอน

การบันทึกเหตุการณ์แบบรวมศูนย์ด้วย ELK Stack เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 3 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Centralized Logging?

In a microservices world, applications don't run as one big piece. Instead, they're many small, independent services.

This means logs are scattered across many servers and containers. Finding issues becomes a huge challenge without a central place to view them all.

  • Debugging: Hard to trace requests across services.
  • Monitoring: Difficult to spot trends or errors.
  • Maintenance: Manually checking logs is time-consuming.

Meet the ELK Stack

The ELK Stack is a popular solution for centralized logging. It's a powerful collection of three open-source tools:

  • Elasticsearch: Stores and indexes logs.
  • Logstash: Processes and transforms logs.
  • Kibana: Visualizes and analyzes logs.

Together, they create a robust pipeline for all your application logs.

Elasticsearch: Data Storage

Elasticsearch is a highly scalable search engine. It's built for speed and can handle vast amounts of data, like all your application logs.

  • Indexing: Organizes logs for fast searching.
  • Scalability: Easily handles growing log volumes.
  • Powerful Search: Allows complex queries to find specific events or patterns.

Think of it as the super-efficient database for your logs.

Logstash: Data Pipeline

Logstash is the data processing engine of the ELK stack. It collects logs from various sources, transforms them, and then sends them to Elasticsearch.

It works like a funnel with three main stages:

  • Inputs: Where logs are collected from (e.g., files, network, message queues).
  • Filters: Where logs are parsed, enriched, or modified (e.g., extract data, remove sensitive info).
  • Outputs: Where processed logs are sent (e.g., Elasticsearch).

Kibana: Visualization & Analysis

Kibana is the user interface that sits on top of Elasticsearch. It lets you explore, visualize, and build dashboards from your log data.

With Kibana, you can:

  • Search and filter logs in real-time.
  • Create interactive charts and graphs.
  • Build custom dashboards to monitor application health.
  • Identify trends and troubleshoot issues quickly.

Spring Boot Logging Basics

Spring Boot applications use SLF4J as a logging facade and Logback as the default implementation.

By default, Spring Boot logs to the console. These logs include timestamps, log levels (INFO, DEBUG, WARN, ERROR), thread names, and the actual log message.

This console output is a common source for Logstash or other agents to pick up.

Simple Spring Boot Logger

Here's a minimal Spring Boot application that demonstrates basic logging at different levels. Run it and observe the console output.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LoggingApp {

    private static final Logger logger = LoggerFactory.getLogger(LoggingApp.class);

    public static void main(String[] args) {
        SpringApplication.run(LoggingApp.class, args);
        logger.info("Spring Boot LoggingApp started!");
        logger.debug("This is a debug message.");
        logger.warn("A warning in the app.");
        logger.error("An error occurred!");
    }
}

Getting Logs to ELK

How do logs from your Spring Boot app reach the ELK stack?

A common approach is to use Filebeat. Filebeat is a lightweight shipper that runs on your application servers. It monitors log files or console output and forwards them to Logstash or Elasticsearch.

This makes sure logs are collected reliably without burdening your application.

The ELK Stack Workflow

Let's summarize the typical flow for centralized logging with ELK:

  1. Your Spring Boot Application generates logs (e.g., to console or a file).
  2. Filebeat collects these logs from the source.
  3. Logstash receives logs from Filebeat, processes them (parses, filters).
  4. Logstash sends processed logs to Elasticsearch for storage and indexing.
  5. Kibana queries Elasticsearch to visualize and analyze the log data.

ELK Component Roles

Each component in the ELK stack has a distinct and crucial role. Understanding these roles is key to effective logging.

Lesson Summary

We've explored the importance of centralized logging for microservices and introduced the ELK (Elasticsearch, Logstash, Kibana) stack.

  • Elasticsearch: Stores and indexes logs for fast searching.
  • Logstash: Processes and transforms logs from various sources.
  • Kibana: Provides powerful visualization and analysis tools.
  • Filebeat: A common agent for collecting logs from applications.

This powerful combination ensures you can effectively monitor and troubleshoot your distributed applications.

เริ่มต้นได้ฟรี

เรียนรู้ Java ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
24
บทเรียน
93

คำถามที่พบบ่อย

บทเรียน “การบันทึกเหตุการณ์แบบรวมศูนย์ด้วย ELK Stack” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การบันทึกเหตุการณ์แบบรวมศูนย์ด้วย ELK Stack” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 3 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การบันทึกเหตุการณ์แบบรวมศูนย์ด้วย ELK Stack”

ตั้งค่าและใช้ Elasticsearch, Logstash และ Kibana สำหรับการบันทึกเหตุการณ์และวิเคราะห์บันทึกแบบรวมศูนย์ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 3 บทเรียน

บทเรียน “การบันทึกเหตุการณ์แบบรวมศูนย์ด้วย ELK Stack” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การบันทึกเหตุการณ์แบบรวมศูนย์ด้วย ELK Stack
  2. การตรวจสอบสถานะและเมตริก
  3. การแจ้งเตือนและแดชบอร์ด
← กลับไปที่ Spring Boot 4 Microservices & REST APIs