Docker中的Kafka - 如何从命令行读取/写入主题?

时间:2018-04-23 22:02:07

标签: bash docker apache-kafka confluent-kafka

我在Docker中运行了一个启用了SSL的Kafka。

docker ps

CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS                                        NAMES
b8f6b1c573a1        nginx:1.13.9-alpine                     "nginx -g 'daemon of…"   9 hours ago         Up 9 hours          80/tcp, 0.0.0.0:8081->443/tcp                ng
761ce6ee2960        confluentinc/cp-schema-registry:4.0.0   "/etc/confluent/dock…"   9 hours ago         Up 9 hours          0.0.0.0:8080->8080/tcp, 8081/tcp             sr
16d7b81dfbc8        confluentinc/cp-kafka:4.0.0             "/etc/confluent/dock…"   9 hours ago         Up 9 hours          0.0.0.0:9092-9093->9092-9093/tcp             k1
9be579992536        confluentinc/cp-zookeeper:4.0.0         "/etc/confluent/dock…"   9 hours ago         Up 9 hours          2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp   zk

如何从命令行写入主题?
尝试(主题'测试'存在):

kafka-console-producer  --broker-list kafka:9093 --topic test
# [2018-04-23 17:55:14,325] ERROR Error when sending message to topic test with key: null, value: 2 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
# org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms. (60s timeout)


kafka-console-producer  --broker-list kafka:9092 --topic test
>aa
#[2018-04-23 18:00:59,443] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 1 : {test=TOPIC_AUTHORIZATION_FAILED} (org.apache.kafka.clients.NetworkClient)
#[2018-04-23 18:00:59,444] ERROR Error when sending message to topic test with key: null, value: 2 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
# org.apache.kafka.common.errors.TopicAuthorizationException: Not authorized to access topics: [test]

kafka-console-producer  --broker-list localhost:9092 --topic test                                                                                                                                                             dnk306@9801a7a5b33d
>aa
#[2018-04-23 21:52:47,056] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 1 : {test=TOPIC_AUTHORIZATION_FAILED} (org.apache.kafka.clients.NetworkClient)
#[2018-04-23 21:52:47,056] ERROR Error when sending message to topic test with key: null, value: 2 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
#org.apache.kafka.common.errors.TopicAuthorizationException: Not authorized to access topics: [test]

1 个答案:

答案 0 :(得分:1)

当在架构注册表或Kafka容器中时,如果您使用Docker compose配置,这可能是因为容器将通过Compose链接,但是,容器主机名可能是k1而不是{{1 }}

如果您在Kafka容器内或在容器外使用CLI命令,那么它只是kafka(因为您转发了端口)。

如果您在Kafka之外的某个容器内,并且您想通过主机名解析Kafka容器,则必须在docker run中添加localhost:9093。请参阅Docker文档以链接容器

同样重要的是,您需要将Kafka容器链接到Zookeeper容器,将Schema Registry链接到一个或另一个ZK或Kafka,具体取决于它的配置方式。

https://docs.confluent.io/current/installation/docker/docs/quickstart.html

Also SSL Kafka Docker compose examples here

  

TopicAuthorizationException:无权访问主题:[test]

这表示与Kafka容器的成功连接。您的下一步是ensure your Java environment has the necessary keys to access the broker over SSL