学了最基础的 Kafka 消息的发送和消费,现在我们进一步学习,当遇见错误信息时,该如何处理,下面是 Spring for kafka 的一句话,告诉我们 Spring Boot 做错误处理有多简单
If you are using Spring Boot, you simply need to add the error handler as a @Bean and boot will add it to the auto-configured factory.
配置文件
server:
port: 8888
spring:
kafka:
consumer:
bootstrap-servers: 127.0.0.1:9092
group-id: group_id
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
producer:
bootstrap-servers: 127.0.0.1:9092
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
消息发送
import com.bob.kafka.model.Foo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org