การปรับแต่งการกำหนดค่าผู้ผลิต
สำรวจการกำหนดค่าผู้ผลิตต่าง ๆ เช่น acks ขนาดชุดข้อมูล linger.ms และการลองใหม่ เพื่อเพิ่มประสิทธิภาพและความน่าเชื่อถือ
การปรับแต่งการกำหนดค่าผู้ผลิต เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Customize Producer Settings?
When sending messages to Kafka, default settings might not always be ideal. Customizing your producer's configuration is key to optimizing for specific needs.
You can fine-tune your producer for better performance, increased reliability, or optimized throughput, depending on your application's requirements.
Configuring in Spring Boot
In Spring Boot, Kafka producer properties are typically defined in your application.yml (or .properties) file.
All producer-related settings usually start with the prefix spring.kafka.producer. Spring Boot automatically picks these up to configure your KafkaTemplate.
spring:
kafka:
bootstrap-servers: localhost:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
# Custom configurations go hereAcknowledgments (`acks`)
The acks (acknowledgments) property determines the level of durability for messages sent by the producer. It controls how many replicas must acknowledge the write before the producer considers the message sent successfully.
acks=0: Producer doesn't wait for any acknowledgment. Fastest, but lowest durability (messages might be lost).acks=1: Producer waits for the leader replica to acknowledge the write. Good balance of durability and speed.acks=all: Producer waits for all in-sync replicas to acknowledge. Highest durability, but slowest.
Setting `acks=all`
For critical messages where data loss is unacceptable, you'd typically set acks to all. This ensures that your message is safely replicated before the producer moves on.
Remember, higher durability often means slightly higher latency.
spring:
kafka:
producer:
acks: all # Ensures high durability
# Other producer configsBatching: `batch.size` & `linger.ms`
To improve throughput, Kafka producers don't send every message individually. Instead, they batch multiple messages together.
batch.size: The maximum amount of data (in bytes) that will be collected before sending a batch. Default is 16KB.linger.ms: The maximum time (in milliseconds) the producer will wait for more messages to accumulate in a batch. Default is 0ms (send immediately).
These two properties work together: a batch is sent when either batch.size is reached OR linger.ms expires.
Optimizing Batch Settings
Adjusting batch.size and linger.ms can significantly impact performance. Larger batches and longer linger times can increase throughput but also slightly increase latency for individual messages.
Here's how you might configure them for better batching:
spring:
kafka:
producer:
batch-size: 32768 # 32 KB (larger batch)
linger-ms: 50 # Wait up to 50ms (longer wait)
# Other producer configsRetries (`retries`)
What happens if a message fails to send due to a transient network issue or a temporary broker unavailability?
The retries property specifies how many times the producer should re-attempt sending a message that failed due to a potentially recoverable error. This greatly enhances the reliability of your message delivery.
Setting `retries`
Setting a reasonable number of retries helps ensure your messages eventually reach Kafka, even if there are temporary glitches.
It's often combined with delivery.timeout.ms, which defines the total time a producer will wait for a message to be delivered, including retries.
spring:
kafka:
producer:
retries: 5 # Try up to 5 times on failure
delivery-timeout-ms: 120000 # 2 minutes total timeout
# Other producer configsCustom Producer in Action
While Spring Boot handles the underlying Kafka client configuration based on your application.yml, let's see how these properties are set directly in a Java Kafka producer. This helps understand the core mechanism.
This example explicitly configures and uses a Kafka producer:
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class CustomProducerDemo {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
// Custom configurations we discussed
props.put("acks", "all");
props.put("batch.size", 32768);
props.put("linger.ms", 50);
props.put("retries", 5);
try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
producer.send(new ProducerRecord<>("custom-topic", "key1", "Hello from CoddyKit!"));
producer.send(new ProducerRecord<>("custom-topic", "key2", "Another custom message!"));
producer.flush(); // Ensure all buffered records are sent
System.out.println("Messages sent with custom configurations.");
} catch (Exception e) {
e.printStackTrace();
}
}
}Check Your Knowledge
You've learned about several key producer configurations. Let's test your understanding!
Recap: Customizing Producers
Great job! You've explored how to customize your Kafka producers for various needs:
acks: Controls message durability.batch.size&linger.ms: Optimize for throughput by batching messages.retries: Enhances reliability by re-sending failed messages.
By understanding and adjusting these properties, you can tailor your Spring Boot Kafka applications to meet specific performance and reliability goals. Next, we'll dive into implementing Kafka consumers!
คำถามที่พบบ่อย
บทเรียน “การปรับแต่งการกำหนดค่าผู้ผลิต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การปรับแต่งการกำหนดค่าผู้ผลิต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การปรับแต่งการกำหนดค่าผู้ผลิต”
สำรวจการกำหนดค่าผู้ผลิตต่าง ๆ เช่น acks ขนาดชุดข้อมูล linger.ms และการลองใหม่ เพื่อเพิ่มประสิทธิภาพและความน่าเชื่อถือ คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การปรับแต่งการกำหนดค่าผู้ผลิต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม
ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผนวก Spring Kafka Starter
- การส่งข้อความด้วย KafkaTemplate
- การปรับแต่งการกำหนดค่าผู้ผลิต
- การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต