无法阅读 kafka 消息,但可以列出可用主题

时间:2021-02-07 22:07:23

标签: java kubernetes apache-kafka kafka-consumer-api

我正在尝试从 IntelliJ IDEA 本地连接到 minikube 上 k8s 中的 kafka 并阅读一些消息。我可以向消费者列出可用的主题,但无法阅读任何消息。

消费者代码:

public class TestConsumer {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "consumer-test");
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props, new StringDeserializer(), new StringDeserializer());

    consumer.listTopics().forEach((key, value) -> System.out.println("topic = " + key));

    consumer.subscribe(Collections.singletonList("test"));

    try {
      while (true) {
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1L));

        for (ConsumerRecord<String, String> record : records)
          System.out.println(record.offset() + ": " + record.value());

        consumer.commitAsync();// doesn't work with or without this line
      }
    } finally {
      consumer.close();
    }
  }
}

为了在 k8s 中安装,我使用了这个 helm chart https://github.com/bitnami/charts/tree/master/bitnami/kafka 和以下配置

replicaCount: 3
deleteTopicEnable: true
metrics:
  kafka:
    enabled: true

k8s 服务:

NAME                       TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE

kafka                      ClusterIP      10.97.216.82     <none>        9092/TCP                     30m
kafka-headless             ClusterIP      None             <none>        9092/TCP,9093/TCP            30m
kafka-metrics              ClusterIP      10.107.104.199   <none>        9308/TCP                     30m
kafka-zookeeper            ClusterIP      10.101.103.6     <none>        2181/TCP,2888/TCP,3888/TCP   30m
kafka-zookeeper-headless   ClusterIP      None             <none>        2181/TCP,2888/TCP,3888/TCP   30m

k8s 豆荚:

NAME                                     READY   STATUS    RESTARTS   AGE

kafka-0                                  1/1     Running   4          32m
kafka-1                                  1/1     Running   4          32m
kafka-2                                  1/1     Running   2          32m
kafka-client                             1/1     Running   0          31m
kafka-exporter-6ccc69f8cc-tgnxh          1/1     Running   0          33m
kafka-zookeeper-0                        1/1     Running   0          33m

(只有 kafka-client pod 是手动创建的,而不是由 helm chart 创建的)

我正在使用 kafka compile group: 'org.apache.kafka', name: 'kafka-clients', version: '2.7.0' 的这种依赖

作为我运行的设置的补充:

kubectl port-forward kafka-0 9092:9092

并在我的主机文件中添加这一行

127.0.0.1 kafka-headless.default.svc.cluster.local kafka-headless

创建主题:

./kafka-topics.sh --create \
--zookeeper kafka-zookeeper:2181 \
--replication-factor 1 \
--partitions 1 \
--topic test

制作主题

./kafka-console-producer.sh --broker-list kafka:9092 --topic test

虽然我可以通过控制台消费者读取所有消息

./kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic test --from-beginning

感谢任何帮助和建议。

1 个答案:

答案 0 :(得分:0)

谢谢你们!我终于成功读取了来自 kafka 的消息。

@OneCricketeer 指出问题所在,我找到了一个类似的链接,它很好地解释了问题https://www.confluent.io/blog/kafka-client-cannot-connect-to-broker-on-aws-on-docker-etc/

我所做的是在端口 kafka-0-external 上转发 9094 服务并将舵图的值更改为

replicaCount: 3
deleteTopicEnable: true
metrics:
  kafka:
    enabled: true
externalAccess:
  enabled: true
  service:
    type: NodePort
    port: 9094
  autoDiscovery:
    enabled: true
serviceAccount:
  create: true
rbac:
  create: true
extraEnvVars:
  - name: KAFKA_LISTENERS
    value: "CLIENT://:9092,INTERNAL://:9093,EXTERNAL://localhost:9094"
  - name: KAFKA_ADVERTISED_LISTENERS
    value: "CLIENT://:9092,INTERNAL://:9093,EXTERNAL://localhost:9094"
  - name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP
    value: "INTERNAL:PLAINTEXT,CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT"