springboot 生产者kafka 死信队列
时间: 2023-11-26 19:48:31 浏览: 141
以下是使用Spring Boot实现Kafka生产者和死信队列的步骤:
1.在pom.xml文件中添加Kafka和Spring Kafka依赖:
```xml
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.0</version>
</dependency>
```
2.在application.properties文件中配置Kafka:
```properties
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=my-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.properties.max.poll.interval.ms=60000
spring.kafka.consumer.properties.max.poll.records=100
spring.kafka.consumer.properties.max.partition.fetch.bytes=1048576
spring.kafka.consumer.properties.fetch.max.bytes=5242880
spring.kafka.consumer.properties.fetch.min.bytes=1
spring.kafka.consumer.properties.fetch.max.wait.ms=500
spring.kafka.consumer.properties.session.timeout.ms=30000
spring.kafka.consumer.properties.enable.auto.commit=true
spring.kafka.consumer.properties.auto.commit.interval.ms=5000
spring.kafka.consumer.properties.isolation.level=read_committed
spring.kafka.consumer.properties.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.properties.value.serializer=org.apache.kafka.common.serialization.StringSerializer
```
3.创建一个Kafka生产者:
```java
@Service
public class KafkaProducerService {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void sendMessage(String topic, String message) {
kafkaTemplate.send(topic, message);
}
}
```
4.创建一个Kafka消费者:
```java
@Service
public class KafkaConsumerService {
@KafkaListener(topics = "my-topic")
public void receiveMessage(String message) {
// 处理消息
}
@KafkaListener(id = "my-group", topics = "my-topic-dlq")
public void receiveDlqMessage(String message) {
// 处理死信队列消息
}
}
```
5.在application.properties文件中配置死信队列:
```properties
spring.kafka.listener.type=single
spring.kafka.listener.poll-timeout=5000
spring.kafka.listener.concurrency=1
spring.kafka.listener.ack-mode=manual_immediate
spring.kafka.listener.retry-template.max-attempts=3
spring.kafka.listener.retry-template.back-off=1000
spring.kafka.listener.retry-template.multiplier=2.0
spring.kafka.listener.retry-template.max-interval=60000
spring.kafka.listener.retry-template.exponential-backoff=true
spring.kafka.listener.retry-template.initial-interval=1000
spring.kafka.listener.retry-template.retry-policy=SIMPLE_RETRY
spring.kafka.listener.retry-template.stateful-retry=true
spring.kafka.listener.retry-template.include-headers=true
spring.kafka.listener.retry-template.max-time=60000
spring.kafka.listener.retry-template.recovery-callback-class=org.springframework.kafka.listener.SeekToCurrentErrorHandler
spring.kafka.listener.retry-template.recovery-callback-method=recover
spring.kafka.listener.retry-template.back-off-strategy=org.springframework.kafka.backoff.ExponentialBackOff
spring.kafka.listener.retry-template.back-off-strategy.max-interval=60000
spring.kafka.listener.retry-template.back-off-strategy.initial-interval=1000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.max-attempts=3
spring.kafka.listener.retry-template.back-off-strategy.exponential=true
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-interval=1000
spring.kafka.listener.retry-template.back-off-strategy.max-interval=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta=1000
spring.kafka.listener.retry-template.back-off-strategy.initial-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.multiplier=2.0
spring.kafka.listener.retry-template.back-off-strategy.randomization-factor=0.5
spring.kafka.listener.retry-template.back-off-strategy.max-interval-factor=2.0
spring.kafka.listener.retry-template.back-off-strategy.min-duration=1000
spring.kafka.listener.retry-template.back-off-strategy.max-duration=60000
spring.kafka.listener.retry-template.back-off-strategy.delta
阅读全文