Java
MQ在实际业务场景中需要注意的问题
- 消息丢失:
- 发送方丢失:未存储到kafka,由于leader切换导致的丢失
- 消费方丢失:拉取到消息提交后尚未实际消费完成即异常了,导致拉取到的消息没有实际消费
- 重复消费:多个消费者消费同一partition提交时覆盖问题导致的重复
- 顺序消费:partition内有序,
- 多个消费者消费同一partition
使用示例
配置:
spring:
kafka:
bootstrap-servers: "localhost:9092"
consumer:
group-id: "myGroup"
发送消息:
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final KafkaTemplate<String, String> kafkaTemplate;
public MyBean(KafkaTemplate<String, String> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
public void someMethod() {
this.kafkaTemplate.send("someTopic", "Hello");
}
}
接收消息:
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
@KafkaListener(topics = "someTopic")
public void processMessage(String content) {
// ...
}
}
spring中kafka的配置
消费者配置
KafkaListenerContainerFactory
标签:Component,kafkaTemplate,springframework,实际,应用,import,kafka,public From: https://www.cnblogs.com/z-dk/p/18335469