我遇到了一个我不明白的apache Kafka的问题。我在我的经纪人中订阅了一个名为“topic-received”的主题。这是代码:
protected String readResponse(final String idMessage) {
if (props != null) {
kafkaClient = new KafkaConsumer<>(props);
logger.debug("Subscribed to topic-received");
kafkaClient.subscribe(Arrays.asList("topic-received"));
logger.debug("Waiting for reading : topic-received");
ConsumerRecords<String, String> records =
kafkaClient.poll(kafkaConfig.getRead_timeout());
if (records != null) {
for (ConsumerRecord<String, String> record : records) {
logger.debug("Resultado devuelto : "+record.value());
return record.value();
}
}
}
return null;
}
当发生这种情况时,我会从另一个点向“收到主题”发送消息。代码如下:
private void sendMessageToKafkaBroker(String idTopic, String value) {
Producer<String, String> producer = null;
try {
producer = new KafkaProducer<String, String>(mapProperties());
ProducerRecord<String, String> producerRecord = new
ProducerRecord<String, String>("topic-received", value);
producer.send(producerRecord);
logger.info("Sended value "+value+" to topic-received");
} catch (ExceptionInInitializerError eix) {
eix.printStackTrace();
} catch (KafkaException ke) {
ke.printStackTrace();
} finally {
if (producer != null) {
producer.close();
}
}
}
我第一次尝试使用主题“收到主题”时,会收到类似这样的警告
"WARN 13164 --- [nio-8085-exec-3] org.apache.kafka.clients.NetworkClient :
Error while fetching metadata with correlation id 1 : {topic-
received=LEADER_NOT_AVAILABLE}"
但是,如果我再试一次,对于这个话题“主题收到”,工作正常,并且没有提出警告。无论如何,这对我没用,因为我必须从主题中听取并每次发送给一个新主题(由字符串标识符引用:12Erw45-2345Saf-234DASDFasd)
在google中寻找LEADER_NOT_AVAILABLE,有些人谈到在下一行添加到server.properties:
host.name=127.0.0.1
advertised.port=9092
advertised.host.name=127.0.0.1
但它不适合我(不知道为什么)。
我尝试使用以下代码在所有此过程之前创建主题:
private void createTopic(String idTopic) {
String zookeeperConnect = "localhost:2181";
ZkClient zkClient = new ZkClient(zookeeperConnect,10000,10000,
ZKStringSerializer$.MODULE$);
ZkUtils zkUtils = new ZkUtils(zkClient, new
ZkConnection(zookeeperConnect),false);
if(!AdminUtils.topicExists(zkUtils,idTopic)) {
AdminUtils.createTopic(zkUtils, idTopic, 2, 1, new Properties(),
null);
logger.debug("Created topic "+idTopic+" by super user");
}
else{
logger.debug("topic "+idTopic+" already exists");
}
}
没有错误,但仍然会一直听到超时。
我已经审查了经纪人的财产以检查是否有任何帮助,但我还没有找到足够清楚的东西。我用来阅读的道具是:
props = new Properties();
props.put("bootstrap.servers", kafkaConfig.getBootstrap_servers());
props.put("key.deserializer", kafkaConfig.getKey_deserializer());
props.put("value.deserializer", kafkaConfig.getValue_deserializer());
props.put("key.serializer", kafkaConfig.getKey_serializer());
props.put("value.serializer", kafkaConfig.getValue_serializer());
props.put("group.id",kafkaConfig.getGroupId());
,发送......
Properties props = new Properties();
props.put("bootstrap.servers", kafkaConfig.getHost() + ":" +
kafkaConfig.getPort());
props.put("group.id", kafkaConfig.getGroup_id());
props.put("enable.auto.commit", kafkaConfig.getEnable_auto_commit());
props.put("auto.commit.interval.ms",
kafkaConfig.getAuto_commit_interval_ms());
props.put("session.timeout.ms", kafkaConfig.getSession_timeout_ms());
props.put("key.deserializer", kafkaConfig.getKey_deserializer());
props.put("value.deserializer", kafkaConfig.getValue_deserializer());
props.put("key.serializer", kafkaConfig.getKey_serializer());
props.put("value.serializer", kafkaConfig.getValue_serializer());
有任何线索吗?为什么,我必须使用来自代理和主题的消息的唯一方法是在发生错误后重复请求?
提前致谢
答案 0 :(得分:2)
请注意::在某些Kafka安装中,该框架可以在不存在该主题时自动创建该主题,这说明了为什么一开始只能看到一次该问题。
答案 1 :(得分:1)
当您的主题名称不存在时,会出现此错误。
要列出所有主题,请执行以下操作:
kafka-topics --list --zookeper localhost:2181