我正在尝试创建一个简单的实用程序来向Kafka发布消息,但是还需要传递标题以及消息。 该实用程序没有标题工作正常但在尝试发送标题时我收到错误。
下面是我正在使用的示例代码 -
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "host:port");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
List<Header> headers = Arrays.asList(new RecordHeader("sample_header", "sample_value".getBytes()));
ProducerRecord<String, String> record = new ProducerRecord<>("TEST", 0, "key", "sample message", headers);
Future<RecordMetadata> future = producer.send(record);
System.out.println(future.get());
producer.close();
}
我得到的例外是 -
Exception in thread "main" java.lang.IllegalArgumentException: Magic v1 does not support record headers
at org.apache.kafka.common.record.MemoryRecordsBuilder.appendWithOffset(MemoryRecordsBuilder.java:385)
at org.apache.kafka.common.record.MemoryRecordsBuilder.appendWithOffset(MemoryRecordsBuilder.java:424)
at org.apache.kafka.common.record.MemoryRecordsBuilder.append(MemoryRecordsBuilder.java:481)
at org.apache.kafka.common.record.MemoryRecordsBuilder.append(MemoryRecordsBuilder.java:504)
at org.apache.kafka.clients.producer.internals.ProducerBatch.tryAppend(ProducerBatch.java:106)
at org.apache.kafka.clients.producer.internals.RecordAccumulator.append(RecordAccumulator.java:219)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:791)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:745)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:634)
at com.test.kafka.KafkaProducerApp.main(KafkaProducerApp.java:45)
答案 0 :(得分:0)
通过不同的方式(Spring Cloud Streams Kafka生产者)遇到了相同的错误。我的问题是由于将class MainApplication : Application() {
lateinit var applicationComponent: ApplicationComponent
override fun onCreate() {
super.onCreate()
this.registerDependencies()
}
private fun registerDependencies() {
val coreModule = CoreModule(applicationContext)
val applicationModule = ApplicationModule(LocalisationModule(coreModule).localisationComponent,
ConfigurationModule(coreModule).configurationComponent)
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(applicationModule)
.build()
}
}
设置为低于允许它的经纪人本身see Kafka's broker configs的版本而设置的:
指定将使用哪个版本的中间经纪人协议。在将所有代理升级到新版本后,通常会遇到这种情况。
对于我的部署,代理是inter.broker.protocol.version
,而v1.1
是inter.broker.protocol.version
。当v0.10.1
代理收到带有标头的消息时,一直很好,直到它使用不支持标头的v1.1
复制消息(产生v0.10.1
的错误)。>