การผนวก Spring Kafka Starter
เพิ่มทรัพยากรพึ่งพา Spring Kafka ที่จำเป็นในโครงการ Spring Boot และกำหนดค่าการเชื่อมต่อพื้นฐานไปยังคลัสเตอร์ Kafka
การผนวก Spring Kafka Starter เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Spring Kafka: The Easy Way
Welcome! In this lesson, we'll get your Spring Boot application ready to talk to Apache Kafka. We'll add the essential library and set up basic connection details.
Spring Kafka is a powerful module that simplifies integrating Kafka with Spring applications. It handles much of the boilerplate, letting you focus on your business logic.
Add the Dependency
First things first, we need to add the spring-kafka dependency to your project. If you're using Maven, add this to your pom.xml file:
- Open your
pom.xml. - Locate the
<dependencies>section. - Add the snippet below.
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>Basic Spring Boot Project
Before we configure Kafka, ensure you have a standard Spring Boot project. This typically includes:
- A main application class annotated with
@SpringBootApplication. - An
application.properties(orapplication.yml) file insrc/main/resources.
This setup provides the foundation for Spring's auto-configuration magic.
Configure Kafka Connection
Spring Boot automatically picks up configuration from application.properties or application.yml. This is where we'll tell our application how to find the Kafka cluster.
All Kafka-related properties usually start with spring.kafka., making them easy to identify and manage.
Key Config: Bootstrap Servers
The most crucial configuration for connecting to Kafka is spring.kafka.bootstrap-servers. This property specifies a comma-separated list of Kafka broker addresses.
Your application uses these addresses to establish an initial connection to the Kafka cluster. Think of them as entry points.
- Example:
localhost:9092for a local setup. - Example:
kafka1.example.com:9092,kafka2.example.com:9092for a cluster.
Producer Settings (Preview)
While we won't implement a producer yet (that's the next lesson!), it's good to know that producer-specific settings also go into your properties file.
These settings control how your application sends messages. Examples include:
spring.kafka.producer.key-serializerspring.kafka.producer.value-serializer
We'll dive into customizing these later.
Consumer Settings (Preview)
Similarly, when your application needs to receive messages, you'll configure consumer-specific properties. These control how messages are consumed.
Common consumer settings include:
spring.kafka.consumer.group-idspring.kafka.consumer.auto-offset-reset
We'll explore these in detail when building consumers.
Minimal Kafka Properties
For our initial setup, we only need the bootstrap-servers. Add this line to your src/main/resources/application.properties file:
spring.kafka.bootstrap-servers=localhost:9092Starting a Kafka-Ready App
With the dependency added and bootstrap-servers configured, your Spring Boot app is now ready to interact with Kafka! Try running this simple application.
Spring Kafka will auto-configure the necessary components based on your properties, even if you're not sending or receiving messages yet.
package com.coddykit.kafka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KafkaIntegrationApp {
public static void main(String[] args) {
SpringApplication.run(KafkaIntegrationApp.class, args);
System.out.println("Spring Boot Kafka app started. Ready to connect!");
}
}Quick Check
You've just learned how to set up a basic Spring Boot application for Kafka. Let's test your understanding!
Recap: Ready for Kafka!
Great job! You've successfully integrated the Spring Kafka Starter into your Spring Boot project. Here's what you accomplished:
- Added the
spring-kafkadependency. - Understood the role of
application.propertiesfor Kafka configuration. - Configured the vital
spring.kafka.bootstrap-serversproperty.
Your application is now prepared to send and receive messages. In the next lesson, we'll dive into building a Spring Boot Kafka Producer!
คำถามที่พบบ่อย
บทเรียน “การผนวก Spring Kafka Starter” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผนวก Spring Kafka Starter” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผนวก Spring Kafka Starter”
เพิ่มทรัพยากรพึ่งพา Spring Kafka ที่จำเป็นในโครงการ Spring Boot และกำหนดค่าการเชื่อมต่อพื้นฐานไปยังคลัสเตอร์ Kafka คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การผนวก Spring Kafka Starter” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม
ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผนวก Spring Kafka Starter
- การส่งข้อความด้วย KafkaTemplate
- การปรับแต่งการกำหนดค่าผู้ผลิต
- การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต