Spring Boot集成Kafka并使用多个死信队列的完整示例

以下是Spring Boot集成Kafka并使用多个死信队列的完整示例,包含代码和配置说明。


1. 添加依赖 (pom.xml)

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)

yaml 复制代码
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. 自定义异常类

java 复制代码
public class BusinessException extends RuntimeException {
    public BusinessException(String message) {
        super(message);
    }
}

4. Kafka配置类

java 复制代码
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生产者配置
    @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消费者配置
    @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);
    }

    // 自定义错误处理器(支持多个死信队列)
    @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("消息发送到死信队列: " + dlqTopic);
        }, backOff);

        // 配置需要重试的异常类型
        errorHandler.addRetryableExceptions(BusinessException.class);
        errorHandler.addNotRetryableExceptions(SerializationException.class);

        return errorHandler;
    }

    // 根据异常类型选择死信队列
    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消费者服务

java 复制代码
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("消息格式错误");
            } else if (message.contains("business-error")) {
                throw new BusinessException("业务处理失败");
            }
            System.out.println("成功处理消息: " + message);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

6. 启动类

java 复制代码
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. 测试步骤

  1. 创建Kafka主题

    bash 复制代码
    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. 发送测试消息

    java 复制代码
    @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. 观察死信队列

    • 格式错误的消息会进入 serialization-error-dlq
    • 业务异常的消息会进入 business-error-dlq
    • 其他异常进入 general-error-dlq

关键点说明

  1. 错误路由逻辑 :通过determineDlqTopic方法根据异常类型选择不同的死信队列。
  2. 重试机制 :通过FixedBackOff配置重试策略(最多重试3次,间隔1秒)。
  3. 异常分类
    • SerializationException(序列化问题)直接进入死信队列,不重试。
    • BusinessException(业务异常)会触发重试,最终失败后进入死信队列。
相关推荐
whltaoin2 小时前
SpringCloud 项目阶段九:Kafka 接入实战指南 —— 从基础概念、安装配置到 Spring Boot 实战及高可用设计
spring boot·spring cloud·kafka
Insist7536 小时前
基于OpenEuler部署kafka消息队列
分布式·docker·kafka
BTU_YC7 小时前
FastAPI+Vue前后端分离架构指南
vue.js·架构·fastapi
在未来等你8 小时前
Elasticsearch面试精讲 Day 20:集群监控与性能评估
大数据·分布式·elasticsearch·搜索引擎·面试
大咖分享课9 小时前
双活、异地多活架构怎么设计才不翻车?
架构·两地三中心·多活架构
云宏信息9 小时前
赛迪顾问《2025中国虚拟化市场研究报告》解读丨虚拟化市场迈向“多元算力架构”,国产化与AI驱动成关键变量
网络·人工智能·ai·容器·性能优化·架构·云计算
励志成为糕手10 小时前
Kafka选举机制深度解析:分布式系统中的民主与效率
分布式·kafka·linq·controller·isr机制
Juchecar11 小时前
翻译:为什么 本地优先应用 没有流行开来?
架构
echoyu.12 小时前
微服务-分布式追踪 / 监控工具大全
分布式·微服务·架构
love530love12 小时前
EPGF 架构为什么能保持长效和稳定?
运维·开发语言·人工智能·windows·python·架构·系统架构