简体   繁体   English

在 kafka-avro-console-consumer 中检索 Avro 架构时出现错误 40101

[英]Error 40101 when retrieving Avro schema in kafka-avro-console-consumer

The following error appears when attempting to use Confluent Platform CLI tools to read messages from Kafka.尝试使用 Confluent Platform CLI 工具从 Kafka 读取消息时出现以下错误。

[2023-01-17T18:00:14.960189+0100] [2023-01-17 18:00:14,957] ERROR Unknown error when running consumer:  (kafka.tools.ConsoleConsumer$:105)
[2023-01-17T18:00:14.960210+0100] org.apache.kafka.common.errors.SerializationException: Error retrieving Avro schema for id 119
[2023-01-17T18:00:14.960230+0100] Caused by: io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException: Unauthorized; error code: 40101
[2023-01-17T18:00:14.960249+0100]   at io.confluent.kafka.schemaregistry.client.rest.RestService.sendHttpRequest(RestService.java:170)
[2023-01-17T18:00:14.960272+0100]   at io.confluent.kafka.schemaregistry.client.rest.RestService.httpRequest(RestService.java:188)
[2023-01-17T18:00:14.960293+0100]   at io.confluent.kafka.schemaregistry.client.rest.RestService.getId(RestService.java:330)
[2023-01-17T18:00:14.960312+0100]   at io.confluent.kafka.schemaregistry.client.rest.RestService.getId(RestService.java:323)
[2023-01-17T18:00:14.960332+0100]   at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.getSchemaByIdFromRegistry(CachedSchemaRegistryClient.java:63)
[2023-01-17T18:00:14.960353+0100]   at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.getBySubjectAndID(CachedSchemaRegistryClient.java:118)
[2023-01-17T18:00:14.960372+0100]   at io.confluent.kafka.serializers.AbstractKafkaAvroDeserializer.deserialize(AbstractKafkaAvroDeserializer.java:121)
[2023-01-17T18:00:14.960391+0100]   at io.confluent.kafka.serializers.AbstractKafkaAvroDeserializer.deserialize(AbstractKafkaAvroDeserializer.java:92)
[2023-01-17T18:00:14.960412+0100]   at io.confluent.kafka.formatter.AvroMessageFormatter.writeTo(AvroMessageFormatter.java:120)
[2023-01-17T18:00:14.960431+0100]   at io.confluent.kafka.formatter.AvroMessageFormatter.writeTo(AvroMessageFormatter.java:112)
[2023-01-17T18:00:14.960449+0100]   at kafka.tools.ConsoleConsumer$.process(ConsoleConsumer.scala:137)
[2023-01-17T18:00:14.960468+0100]   at kafka.tools.ConsoleConsumer$.run(ConsoleConsumer.scala:75)
[2023-01-17T18:00:14.960487+0100]   at kafka.tools.ConsoleConsumer$.main(ConsoleConsumer.scala:50)
[2023-01-17T18:00:14.960506+0100]   at kafka.tools.ConsoleConsumer.main(ConsoleConsumer.scala)

I am using Kafka 3.2 (both client and server), with a Karapace schema registry by Aiven.我正在使用 Kafka 3.2(客户端和服务器),以及 Aiven 的 Karapace 模式注册表。 I can query the schema registry manually using curl by including the credentials in the URL:我可以通过在 URL 中包含凭据,使用curl手动查询架构注册表:

(base) me@my-laptop:~$ curl https://$SCHEMA_REGISTRY_USER:$SCHEMA_REGISTRY_PASSWORD@$SCHEMA_REGISTRY_HOST:$SCHEMA_REGISTRY_PORT/subjects
["my-topic-" <redacted>

Or as basic auth in a header:或者作为 header 中的基本身份验证:

(base) me@my-laptop:~$ curl -u "$SCHEMA_REGISTRY_USER:$SCHEMA_REGISTRY_PASSWORD" https://$SCHEMA_REGISTRY_HOST:$SCHEMA_REGISTRY_PORT/subjects
["my-topic-" <redacted>

The error seems to happen when the credentials are not passed to the schema registry:当凭证未传递到架构注册表时,似乎会发生错误:

(base) me@my-laptop:~$ curl https://$SCHEMA_REGISTRY_HOST:$SCHEMA_REGISTRY_PORT/subjects
{"error_code": 40101, "message": "Unauthorized"}

According to official docs for kafka-avro-console-consumer , I can use the authentication source URL or USER_INFO , and it should pass those credentials to the schema registry.根据kafka-avro-console-consumer官方文档,我可以使用身份验证源URLUSER_INFO ,它应该将这些凭据传递给架构注册表。 This does not work, and causes the above error.这不起作用,并导致上述错误。

kafka-avro-console-consumer \
          --bootstrap-server $KAFKA_HOST:$KAFKA_PORT \
          --consumer.config /home/guido/.tls/kafka/client-tls.properties \
          --property schema.registry.url=https://$SCHEMA_REGISTRY_USER:$SCHEMA_REGISTRY_PASSWORD@$SCHEMA_REGISTRY_HOST:$SCHEMA_REGISTRY_PORT \
          --property basic.auth.credentials.source=URL \
          --topic my-topic

I've tried every combination I can think of, with URL, USER_INFO, separate credentials, prefixed with schema.registry and without, but all lead to the same error.我已经尝试了我能想到的每一种组合,URL,USER_INFO,单独的凭据,前缀为schema.registry和没有,但都会导致相同的错误。 When I use the regular kafka-console-consumer.sh the same settings work, but I see the Kafka messages as a byte stream, rather than the deserialized Avro message that I'm looking for.当我使用常规的kafka-console-consumer.sh时,相同的设置起作用,但我看到 Kafka 消息是字节 stream,而不是我正在寻找的反序列化 Avro 消息。

EDIT: it appears that java.net.HttpURLConnection is the problem.编辑:问题似乎出在 java.net.HttpURLConnection 上。 It strips credendtials from the URL, and the version of schema-registry-client packaged with Confluent Platform does not support any other version of Basic Authentication yet.它从 URL 中剥离凭证,并且与 Confluent Platform 打包的 schema-registry-client 版本不支持任何其他版本的基本身份验证。

import java.net.URL
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ExampleTest extends AnyFlatSpec with Matchers {
  
  behavior.of("Example")

  it should "work" in {
    val url = "https://username:p4ssw0rd@kafka.example.com:12345"

    val connection = new URL(url).openConnection()

    noException shouldBe thrownBy {
      connection.getInputStream
    }
  }
}

The test fails测试失败

Found it.找到了。 There were three causes for my problem.我的问题有三个原因。

  1. I had an old version of Confluent Platform installed, namely confluent-platform-2.11 .我安装了旧版本的 Confluent Platform,即confluent-platform-2.11 This version did not yet support any schema registry authentication, beyond username and password in the URL.除了 URL 中的用户名和密码外,此版本尚不支持任何模式注册表身份验证。
  2. I thought I had the latest version already (3.3.x) but that's actually the latest version of Kafka, not the latest version of Confluent Platform.我以为我已经有了最新版本 (3.3.x) 但那实际上是 Kafka 的最新版本,而不是 Confluent Platform 的最新版本。
  3. Java's default web request implementation, sun.net.www.protocol.http.HttpURLConnection , does not support credentials in the URL. They are stripped before making the request, despite the URL correctly containing the credentials. Java 的默认 web 请求实现sun.net.www.protocol.http.HttpURLConnection不支持 URL 中的凭据。尽管 URL 正确包含凭据,但在发出请求之前它们会被剥离。

The correct solution was to upgrade to a later version of Confluent Platform.正确的解决方案是升级到更高版本的 Confluent Platform。

See https://docs.confluent.io/platform/current/installation/installing_cp/deb-ubuntu.html#configure-cp参见https://docs.confluent.io/platform/current/installation/installing_cp/deb-ubuntu.html#configure-cp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 kafka-avro-console-consumer 使用旧模式使用消息 - Consume a message with an older schema using kafka-avro-console-consumer Kafka Avro Consumer (kafka-avro-console-consumer) 日志级别 - Kafka Avro Consumer (kafka-avro-console-consumer) Logging Level 带有kafka-avro-console-consumer的LoggingMessageFormatter - LoggingMessageFormatter with kafka-avro-console-consumer 使用具有已注册架构的 kafka-avro-console-consumer - 错误 500 - Use kafka-avro-console-consumer with already registered schema - error 500 使用kafka-avro-console-consumer获取Avro记录为二进制数组 - Get Avro record as binary array with kafka-avro-console-consumer Kafka + Kubernetes + Helm + `/usr/bin/kafka-avro-console-consumer`? - Kafka + Kubernetes + Helm + `/usr/bin/kafka-avro-console-consumer`? kafka-avro-console-consumer:指定架构注册表的信任库位置 - kafka-avro-console-consumer: Specify truststore location for schema-registry 带有kafka-avro-console-consumer的未知魔术字节 - Unknown magic byte with kafka-avro-console-consumer 在终端中从 Kafka 读取 Avro 消息 - kafka-avro-console-consumer 替代方案 - Read Avro messages from Kafka in terminal - kafka-avro-console-consumer alternative 无法通过 kafka-avro-console-consumer 读取 avro 消息(最终目标是通过 Spark 流读取) - unable to read avro message via kafka-avro-console-consumer (end goal read it via spark streaming)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM