SpringBoot集成Kafka并使用多個(gè)死信隊(duì)列詳解
以下是Spring Boot集成Kafka并使用多個(gè)死信隊(duì)列的完整示例,包含代碼和配置說明。
1. 添加依賴 (pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
2. 配置文件 (application.yml)
spring:
kafka:
bootstrap-servers: localhost:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
consumer:
group-id: my-group
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
auto-offset-reset: earliest
3. 自定義異常類
public class BusinessException extends RuntimeException {
public BusinessException(String message) {
super(message);
}
}
4. Kafka配置類
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.*;
import org.springframework.kafka.listener.CommonErrorHandler;
import org.springframework.kafka.listener.DefaultErrorHandler;
import org.springframework.util.backoff.FixedBackOff;
@Configuration
@EnableKafka
public class KafkaConfig {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
// Kafka生產(chǎn)者配置
@Bean
public ProducerFactory<String, String> producerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<>(config);
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
// Kafka消費(fèi)者配置
@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
config.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return new DefaultKafkaConsumerFactory<>(config);
}
// 自定義錯(cuò)誤處理器(支持多個(gè)死信隊(duì)列)
@Bean
public CommonErrorHandler errorHandler(KafkaTemplate<String, String> kafkaTemplate) {
// 重試策略:3次重試,間隔1秒
FixedBackOff backOff = new FixedBackOff(1000L, 3);
DefaultErrorHandler errorHandler = new DefaultErrorHandler((record, exception) -> {
String dlqTopic = determineDlqTopic(exception);
kafkaTemplate.send(dlqTopic, record.key(), record.value());
System.out.println("消息發(fā)送到死信隊(duì)列: " + dlqTopic);
}, backOff);
// 配置需要重試的異常類型
errorHandler.addRetryableExceptions(BusinessException.class);
errorHandler.addNotRetryableExceptions(SerializationException.class);
return errorHandler;
}
// 根據(jù)異常類型選擇死信隊(duì)列
private String determineDlqTopic(Throwable exception) {
if (exception.getCause() instanceof SerializationException) {
return "serialization-error-dlq";
} else if (exception.getCause() instanceof BusinessException) {
return "business-error-dlq";
} else {
return "general-error-dlq";
}
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setCommonErrorHandler(errorHandler(kafkaTemplate()));
return factory;
}
}
5. Kafka消費(fèi)者服務(wù)
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class KafkaConsumerService {
@KafkaListener(topics = "main-topic")
public void consume(String message) {
try {
if (message.contains("invalid-format")) {
throw new SerializationException("消息格式錯(cuò)誤");
} else if (message.contains("business-error")) {
throw new BusinessException("業(yè)務(wù)處理失敗");
}
System.out.println("成功處理消息: " + message);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
6. 啟動(dòng)類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KafkaApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaApplication.class, args);
}
}
7. 測(cè)試步驟
1.創(chuàng)建Kafka主題:
kafka-topics --create --bootstrap-server localhost:9092 --topic main-topic
kafka-topics --create --bootstrap-server localhost:9092 --topic serialization-error-dlq
kafka-topics --create --bootstrap-server localhost:9092 --topic business-error-dlq
kafka-topics --create --bootstrap-server localhost:9092 --topic general-error-dlq
2.發(fā)送測(cè)試消息:
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void sendTestMessages() {
kafkaTemplate.send("main-topic", "valid-message");
kafkaTemplate.send("main-topic", "invalid-format");
kafkaTemplate.send("main-topic", "business-error");
}
3.觀察死信隊(duì)列:
- 格式錯(cuò)誤的消息會(huì)進(jìn)入 serialization-error-dlq
- 業(yè)務(wù)異常的消息會(huì)進(jìn)入 business-error-dlq
- 其他異常進(jìn)入 general-error-dlq
關(guān)鍵點(diǎn)說明
錯(cuò)誤路由邏輯:通過determineDlqTopic方法根據(jù)異常類型選擇不同的死信隊(duì)列。
重試機(jī)制:通過FixedBackOff配置重試策略(最多重試3次,間隔1秒)。
異常分類:
- SerializationException(序列化問題)直接進(jìn)入死信隊(duì)列,不重試。
- BusinessException(業(yè)務(wù)異常)會(huì)觸發(fā)重試,最終失敗后進(jìn)入死信隊(duì)列。
到此這篇關(guān)于SpringBoot集成Kafka并使用多個(gè)死信隊(duì)列詳解的文章就介紹到這了,更多相關(guān)SpringBoot Kafka使用死信隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用Redis進(jìn)行限流功能實(shí)現(xiàn)
Spring Boot中使用Redis實(shí)現(xiàn)限流功能是一種常見的做法,特別是在高并發(fā)場(chǎng)景下,限流可以有效防止系統(tǒng)過載,保證服務(wù)的穩(wěn)定性,本文就來詳細(xì)的介紹一下SpringBoot使用Redis進(jìn)行限流功能實(shí)現(xiàn),感興趣的可以了解一下2025-10-10
DUCC配置平臺(tái)實(shí)現(xiàn)一個(gè)動(dòng)態(tài)化線程池示例代碼
這篇文章主要為大家介紹了DUCC配置平臺(tái)實(shí)現(xiàn)一個(gè)動(dòng)態(tài)化線程池示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
使用Jenv管理多版本JDK環(huán)境的詳細(xì)教程
在現(xiàn)代 Java 開發(fā)中,我們經(jīng)常需要在不同的項(xiàng)目中使用不同版本的 JDK,手動(dòng)切換 JAVA_HOME 環(huán)境變量既繁瑣又容易出錯(cuò),Jenv 是一個(gè)優(yōu)秀的 JDK 版本管理工具,可以讓我們輕松地在不同 JDK 版本間切換,所以本文給大家介紹了使用Jenv管理多版本JDK環(huán)境的詳細(xì)教程2025-08-08
如何在Springboot實(shí)現(xiàn)攔截器功能
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了,下面這篇文章主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)攔截器功能的相關(guān)資料,需要的朋友可以參考下2022-06-06
泛談Java中的不可變數(shù)據(jù)結(jié)構(gòu)
開發(fā)人員通常認(rèn)為擁有final引用,或者val在Kotlin或Scala中,足以使對(duì)象不可變。這篇博客文章深入研究了不可變引用和不可變數(shù)據(jù)結(jié)構(gòu),下面小編來和大家一起學(xué)習(xí)它2019-05-05
spring?boot3自動(dòng)配置與手動(dòng)配置的全過程
本文給大家介紹了spring?boot3自動(dòng)配置與手動(dòng)配置的全過程,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2026-01-01

