你好,我在项目中使用ReplyingKafkaTemplate。配置如下
import Select from 'react-select';
...
const [dataBanks, setDataBanks] = useState([]);
const retrieveBanks = useCallback(() => {
ClientMaintenanceService.retrieveBanks()
.then((response) => {
setDataBanks(response.data);
}).catch((err) => {
console.log("ClientMaintenancePage - retrieveBanks catch >>> " + err)
})
});
const optionsBrstn = dataBanks.map(({BIC, BRSTN, NAME}) => ({
label: BIC + " " + BRSTN + " " + NAME ,
value: BRSTN
}))
return (
<Select
isSearchable="true"
options={ optionsBrstn }
placeholder="Select BRSTN"
/>
)
代码如下 在这种情况下,requestTopic是帐户后请求,而responseTopic是帐户后响应
@Bean
public KafkaMessageListenerContainer<Object, Object> replyListenerContainer() {
ContainerProperties containerProperties = new ContainerProperties("account-get-response",
"account-put-response",
"account-post-response",
"account-delete-response");
return new KafkaMessageListenerContainer<>(consumerFactory(), containerProperties);
}
@Bean
public ReplyingKafkaTemplate<Object, Object, Object> replyKafkaTemplate(ProducerFactory<Object, Object> pf,
KafkaMessageListenerContainer<Object, Object> lc) {
ReplyingKafkaTemplate<Object, Object, Object> replyKafkaTemplate = new ReplyingKafkaTemplate<>(pf, lc);
replyKafkaTemplate.setReplyTimeout(300000);
replyKafkaTemplate.setSharedReplyTopic(true);
return replyKafkaTemplate;
}
@Bean
public ConsumerFactory<Object, Object> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs());
}
@Bean
public ProducerFactory<Object, Object> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapServers);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "grp");
props.put(JsonDeserializer.TRUSTED_PACKAGES, "*");
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
return props;
}
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
props.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, kafkaRequestTimeoutMs);
props.put(ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG, kafkaDeliveryTimeoutMs);
props.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, kafkaRetryBackofMs);
props.put(ProducerConfig.ACKS_CONFIG, kafkaackconfig);
return props;
}
在这种情况下,使用者正在消费请求,以将响应发送回响应主题。但在生产者端(replyingkafkatemplate)是否停留在
ProducerRecord<Object, Object> record = new ProducerRecord<Object, Object>(requestTopic, jsonNode);
record.headers().add(new RecordHeader(KafkaHeaders.REPLY_TOPIC, responseTopic.getBytes()));
record.headers().add(new RecordHeader("TransactionID", clientTxnId.getBytes()));
LOGGER.info("TransactionID: " + clientTxnId + " Produced record on topic " + requestTopic);
RequestReplyFuture<Object, Object, Object> sendAndReceive = replyKafkaTemplate.sendAndReceive(record);
try {
LOGGER.info("TransactionID: " + clientTxnId + " Waiting for consumer response ");
reply = sendAndReceive.get().value();
LOGGER.info("TransactionID: " + clientTxnId + " Got the response from consumer for topic :" + responseTopic);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
表示等待回复 但是当我签入kafka工具后,响应就写在了具有相同相关ID的欲望主题上。
我无法弄清楚为什么即使有响应主题也无法从响应主题中读取它? 预先感谢。
答案 0 :(得分:1)
这可能是比赛条件;将ConsumerConfig.AUTO_OFFSET_RESET_CONFIG
设置为earliest
。
默认为latest
;如果响应在消费者订阅响应主题之前到达,则不会获得响应。