我正在尝试使用以下代码在动态创建的主题上发送消息:
@Service
public class KafkaMessageSender {
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaMessageSender.class);
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void send(String topic, String payload) {
String topicName = topic + Integer.valueOf(RandomStringUtils.randomNumeric(1, 4))%100;
kafkaTemplate.send(topicName, payload);
LOGGER.info("sent payload to topic='{}'", topicName);
}
}
当我使用topic ='somethingnew'调用此发送方法时,我得到以下错误,(有趣的是它能够发送一些消息,但对于很多人来说它会给出错误,并且在一段时间后,它开始发送消息关于那些话题)但是当我重新启动服务器,并使用topic ='somethingnew'再次调用send方法时,它工作正常!卡夫卡有一些潜在的错误吗?我使用的是春季启动版:1.5.4.RELEASE。
2018-02-13 09:52:35.927 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] o.a.kafka.common.utils.AppInfoParser : Kafka version : 0.10.1.1
2018-02-13 09:52:35.927 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] o.a.kafka.common.utils.AppInfoParser : Kafka commitId : f10ef2720b03b247
2018-02-13 09:52:36.067 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 0 : {betsyncDataTopicNew123.5=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:36.272 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.5'
2018-02-13 09:52:36.372 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 3 : {betsyncDataTopicNew123.93=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:36.475 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.93'
2018-02-13 09:52:36.583 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 6 : {betsyncDataTopicNew123.4=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:36.686 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.4'
2018-02-13 09:52:36.794 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 9 : {betsyncDataTopicNew123.7=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:36.897 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.7'
2018-02-13 09:52:37.008 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 12 : {betsyncDataTopicNew123.0=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:37.110 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.0'
2018-02-13 09:52:37.220 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 15 : {betsyncDataTopicNew123.1=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:37.320 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.1'
2018-02-13 09:52:37.431 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 18 : {betsyncDataTopicNew123.10=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:37.534 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.10'
2018-02-13 09:52:37.534 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.4'
2018-02-13 09:52:37.642 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 22 : {betsyncDataTopicNew123.71=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:37.747 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.71'
2018-02-13 09:52:37.859 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 25 : {betsyncDataTopicNew123.2=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:37.962 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.2'
2018-02-13 09:52:37.963 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.7'
2018-02-13 09:52:38.074 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 29 : {betsyncDataTopicNew123.33=LEADER_NOT_AVAILABLE}
2018-02-13 09:52:38.176 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.33'
2018-02-13 09:52:38.176 INFO [betsync-adapter-service,,,] 22241 --- [ Thread-28] c.b.b.a.k.message.KafkaMessageSender : sent payload to topic='betsyncDataTopicNew123.1'
2018-02-13 09:52:38.285 WARN [betsync-adapter-service,,,] 22241 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : Error while fetching metadata with correlation id 33 : {betsyncDataTopicNew123.17=LEADER_NOT_AVAILABLE}
这只是警告,我们没有任何异常,所以不知道消息是否丢失。
答案 0 :(得分:2)
使用AdminClient
按需创建主题。
Spring for Apache Kafka提供了一个方便的KafkaAdmin
,它可以在应用程序上下文中为NewTopic
bean创建主题,但它也可以用于创建AdminClient
,以便您可以手动创建主题 - docs here。 Spring Boot 2.0自动配置为您注册一个。
获得AdminClient
个实例后,请使用createTopics()
方法之一。有关更多信息,请参阅AdminClient
javadocs。请务必等待CreateTopicsResult
Future<?>
完成。
答案 1 :(得分:1)
错误Error while fetching metadata with correlation id 0 ... LEADER_NOT_AVAILABLE
是因为在发布第一条消息时topic doesn't exist,如果您的配置允许自动创建主题,则会自动处理。
上面的消息指的是Kafka无法获取有关主题,其分区和使用者组的元数据(例如,无法找到每个消费者组的每个分区的偏移量)。
正如您所观察到的那样,发送给该主题的第二个及后续消息不会引发此错误。
创建具有随机名称的主题是非常不寻常的,因为消费者需要找到&#39;主题是为了从中消费。一般的想法是在使用之前预先创建Kafka主题,这将允许您配置重要因素,如分区等。