บทนำสู่ผู้ผลิต Kafka
เรียนรู้การเผยแพร่ข้อความไปยังหัวข้อ Kafka ด้วยผู้ผลิต Spring Kafka
บทนำสู่ผู้ผลิต Kafka เป็นบทเรียน 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 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to Event-Driven!
In modern applications, systems often need to react to events as they happen, rather than just waiting for requests. This is the core idea behind Event-Driven Architecture (EDA).
EDA helps build flexible and scalable systems by allowing different parts of your application to communicate asynchronously through events.
What is Apache Kafka?
Apache Kafka is a powerful, distributed streaming platform often used to build event-driven microservices. It's designed for high-throughput, low-latency processing of real-time data feeds.
Think of it as a super-fast, durable message queue that can handle huge amounts of data.
Kafka's Core Concepts
Kafka revolves around a few key ideas:
- Producers: Applications that send data (messages/records) to Kafka.
- Consumers: Applications that read data from Kafka.
- Topics: Categories or feeds to which records are published. Consumers subscribe to topics.
- Brokers: Kafka servers that store the published data.
The Producer's Job
A Kafka Producer is responsible for creating and sending data records to Kafka topics. These records are often key-value pairs, though the key is optional.
Producers don't wait for acknowledgements from consumers; they just publish messages to the topic and move on, making the communication asynchronous.
Adding Spring Kafka
To integrate Kafka into your Spring Boot application, you'll need the spring-kafka dependency. This module provides convenient abstractions for interacting with Kafka.
Add this to your pom.xml:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>Producer Configuration
Spring Kafka producers need configuration, typically in application.properties. The most important settings are the Kafka broker addresses and the serializers for your message keys and values.
Serializers convert your Java objects into bytes for transmission over the network.
Config Example
Here's a basic configuration for a Kafka producer:
spring.kafka.producer.bootstrap-servers=localhost:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializerbootstrap-servers points to your Kafka broker. We're using StringSerializer for both key and value.
Introducing KafkaTemplate
KafkaTemplate is the central class in Spring Kafka for sending messages. It simplifies the process of interacting with Kafka brokers.
You can inject it into your Spring components and use its send() methods to publish messages to specified topics.
Sending a Simple Message
Let's see how to send a basic 'Hello Kafka!' message to a topic named 'my-topic' using KafkaTemplate.
Run this example (ensure a Kafka broker is running on localhost:9092 and 'my-topic' exists).
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.core.KafkaTemplate;
@SpringBootApplication
public class KafkaProducerApp {
public static void main(String[] args) {
SpringApplication.run(KafkaProducerApp.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(KafkaTemplate<String, String> kafkaTemplate) {
return args -> {
String topic = "my-topic";
String message = "Hello Kafka!";
System.out.println("Sending message: " + message + " to topic: " + topic);
kafkaTemplate.send(topic, message);
System.out.println("Message sent. Check your Kafka consumer.");
};
}
}Messages with Keys
Messages can also be sent with a key. Kafka uses keys to ensure messages with the same key go to the same partition within a topic, maintaining order for related data.
This is crucial for scenarios where message order matters, like user events or stock updates for a specific product.
Producer Quick Check
Which of the following are essential components or concepts when setting up a Spring Kafka producer?
Lesson Summary
You've taken your first steps into event-driven architecture with Kafka! We learned that Kafka is a distributed streaming platform, and producers are applications that send messages to topics.
We also covered setting up Spring Kafka, configuring producer properties, and using KafkaTemplate to send messages, including those with keys. This forms the foundation for building event-publishing microservices!
คำถามที่พบบ่อย
บทเรียน “บทนำสู่ผู้ผลิต Kafka” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บทนำสู่ผู้ผลิต Kafka” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 3 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บทนำสู่ผู้ผลิต Kafka”
เรียนรู้การเผยแพร่ข้อความไปยังหัวข้อ Kafka ด้วยผู้ผลิต Spring Kafka คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 3 บทเรียน
บทเรียน “บทนำสู่ผู้ผลิต Kafka” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ผู้ผลิต Kafka
- การสร้างผู้บริโภค Kafka
- การผสานไมโครเซอร์วิสที่ขับเคลื่อนด้วยเหตุการณ์