【发布时间】:2022-09-28 03:13:34
【问题描述】:
我正在使用 kafka 将异步和同步消息发布到代理。一个侦听器将侦听主题并响应同步和异步调用。我对两个模板使用相同的请求主题。. 当使用 fire and forget(Async) 时,我看不到任何问题,因为侦听器会从主题中随机收听消息。使用同步调用时,我遇到超时异常。
- 我需要为不同的模板维护多个监听器吗?
- 如果同步和异步操作的主题相同,会有什么问题吗?
KafkaConfig.java
//Template for synchornous call
@Bean public ReplyingKafkaTemplate<String, Model, Model> replyingKafkaTemplate ( ProducerFactory<String, Model> pf, ConcurrentMessageListenerContainer<String, Model> repliesContainer) { ReplyingKafkaTemplate<String, Model, Model> replyTemplate = new ReplyingKafkaTemplate<>(pf, repliesContainer); replyTemplate.setSharedReplyTopic(true); return replyTemplate; } @Bean //register ConcurrentMessageListenerContainer bean public ConcurrentMessageListenerContainer<String, Model> repliesContainer ( ConcurrentKafkaListenerContainerFactory<String, Model> containerFactory) { ConcurrentMessageListenerContainer<String, Model> repliesContainer = containerFactory.createContainer(\"responseTopic\"); repliesContainer.getContainerProperties().setGroupId(UUID.randomUUID().toString()); repliesContainer.setAutoStartup(false); return repliesContainer; }//Template for asynchronous call
@Bean @Qualifier(\"kafkaTemplate\") public KafkaTemplate<String, Model> kafkaTemplate ( ProducerFactory<String, Model> pf, ConcurrentKafkaListenerContainerFactory<String, Model> factory) { KafkaTemplate<String, Model> kafkaTemplate = new KafkaTemplate<>(pf); factory.setReplyTemplate(kafkaTemplate); return kafkaTemplate; }这里是服务类
@Service public class KafkaService { @Autowired private ReplyingKafkaTemplate<String, Model, Model> replyingKafkaTemplate; @Autowired private KafkaTemplate<String, Model> kafkaTemplate; @Autowired private KafkaConfig config; public Object sendAndReceive (Model model) { ProducerRecord<String, Model> producerRecord = new ProducerRecord(\"requestTopic\", model); producerRecord.headers() .add( new RecordHeader(KafkaHeaders.REPLY_TOPIC, \"replyTopic\")); RequestReplyFuture<String, Model, Model> replyFuture = replyingKafkaTemplate.sendAndReceive(producerRecord, Duration.ofSeconds(timeout)); ConsumerRecord<String, Model> consumerRecord = replyFuture.get(timeout, TimeUnit.SECONDS); return consumerRecord.value(); } public ResponseEntity<Object> send (final Model model) { final ProducerRecord<String, Model> producerRecord = new ProducerRecord(\"requestTopic\", model); final ListenableFuture<SendResult<String, Model>> future = kafkaTemplate.send(producerRecord); final SendResult<String, Model> sendResult = future.get(timeout, TimeUnit.SECONDS); return new ResponseEntity<>(sendResult, HttpStatus.ACCEPTED); } }这是侦听器类。
@Slf4j @Service public class MessageListener { @KafkaListener(groupId = \"${group.id}\", topics = \"requestTopic\", errorHandler = \"customKafkaListenerErrorHandler\",containerFactory = \"customKafkaListenerContainerFactory\") @SendTo public Model consumer (Model model) { switch (model.getType()) { case \"async\": System.out.println(\"Async messages are retrieved\"); case \"sync\": System.out.println(\"Sync messages are retrieved\"); return model; } return model; } @Bean public ConcurrentKafkaListenerContainerFactory<?, ?> customKafkaListenerContainerFactory( ConcurrentKafkaListenerContainerFactoryConfigurer configurer, ConsumerFactory<Object, Object> kafkaConsumerFactory) { ConcurrentKafkaListenerContainerFactory<Object, Object> concurrentKafkaListenerContainerFactory = new ConcurrentKafkaListenerContainerFactory<>(); concurrentKafkaListenerContainerFactory. setConsumerFactory(kafkaConsumerFactory); concurrentKafkaListenerContainerFactory.getContainerProperties() .setAckMode(ContainerProperties.AckMode.RECORD); concurrentKafkaListenerContainerFactory. setCommonErrorHandler(errorHandler()); configurer.configure(concurrentKafkaListenerContainerFactory, kafkaConsumerFactory); concurrentKafkaListenerContainerFactory.setReplyTemplate(kafkaTemplate); return concurrentKafkaListenerContainerFactory; } }应用程序属性
spring.kafka.consumer.enable-auto-commit=false spring.kafka.consumer.auto-offset-reset=最早
-
@Gary Russell,你能检查一下吗?
-
您应该显示每种操作类型之一的调试日志(客户端和服务器端)。
标签: spring-boot apache-kafka kafka-consumer-api spring-kafka kafka-producer-api