【发布时间】:2018-06-05 20:08:23
【问题描述】:
我正在使用这个 docker-compose 设置在本地设置 Kafka:https://github.com/wurstmeister/kafka-docker/
docker-compose up 工作正常,通过 shell 创建主题工作正常。
现在我尝试通过spring-kafka:2.1.0.RELEASE 连接到 Kafka
当启动 Spring 应用程序时,它会打印正确版本的 Kafka:
o.a.kafka.common.utils.AppInfoParser : Kafka version : 1.0.0
o.a.kafka.common.utils.AppInfoParser : Kafka commitId : aaa7af6d4a11b29d
我尝试发送这样的消息
kafkaTemplate.send("test-topic", UUID.randomUUID().toString(), "test");
在客户端发送失败
UnknownServerException: The server experienced an unexpected error when processing the request
在服务器控制台中,我收到消息 Magic v1 不支持记录标头
Error when handling request {replica_id=-1,max_wait_time=100,min_bytes=1,max_bytes=2147483647,topics=[{topic=test-topic,partitions=[{partition=0,fetch_offset=39,max_bytes=1048576}]}]} (kafka.server.KafkaApis)
java.lang.IllegalArgumentException: Magic v1 does not support record headers
谷歌搜索提示版本冲突,但版本似乎适合(org.apache.kafka:kafka-clients:1.0.0 在类路径中)。
有什么线索吗?谢谢!
编辑: 我缩小了问题的根源。发送纯字符串有效,但通过 JsonSerializer 发送 Json 会导致给定问题。这是我的生产者配置的内容:
@Value("\${kafka.bootstrap-servers}")
lateinit var bootstrapServers: String
@Bean
fun producerConfigs(): Map<String, Any> =
HashMap<String, Any>().apply {
// list of host:port pairs used for establishing the initial connections to the Kakfa cluster
put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers)
put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer::class.java)
put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer::class.java)
}
@Bean
fun producerFactory(): ProducerFactory<String, MyClass> =
DefaultKafkaProducerFactory(producerConfigs())
@Bean
fun kafkaTemplate(): KafkaTemplate<String, MyClass> =
KafkaTemplate(producerFactory())
【问题讨论】:
-
这没有意义; (在服务器端获取该消息)。如果客户端版本较旧,它不会发送任何标头,所以一切都应该很好(我已经使用带有 0.10 代理的 1.0.0 客户端进行了测试,只要您不尝试发送标头,它就可以工作)。对于 1.0.0 客户端,当模板不发送任何标头时(由客户端)发送“空”
RecordHeaders。 -
映像名称似乎没有指定版本,因此您可能使用的是较旧的缓存 docker 映像和较新的客户端。向 0.10 代理发送标头的 1.0 客户端会收到此错误。尝试检查 docker 镜像版本和 docker pull 最新的 1.0 代理镜像。
-
将 Kafka 版本更新到最新版本后,我没有收到“Magic v1 不支持记录标头”异常,并且代码运行良好。
标签: spring spring-boot apache-kafka docker-compose spring-kafka