使用Kafka客户端api 1.0.1生成远程Kafka代理时发生超时异常

时间:2018-04-06 18:48:05

标签: java apache-kafka google-compute-engine kafka-producer-api

producer-api(Kafka-client-1.0.1)不能在我在谷歌云中托管的远程Kafka代理(v1.1.0)中生成消息。

我已经配置了server.properties文件,添加以下内容:

listeners=PLAINTEXT://0.0.0.0:9092

advertised.listeners=PLAINTEXT://<Local-IP>:9092

但结果相同。

我的制片人Api看起来像这样: -

public class KafkaProducerExample {
private final static String TOPIC = "my-topic";
private final static String BOOTSTRAP_SERVERS ="Public-IP:9092";
private static Producer<Long, String> createProducer() {
    Properties props = new Properties();
    props.put("bootstrap.servers",BOOTSTRAP_SERVERS);

    props.put(ProducerConfig.CLIENT_ID_CONFIG, "KafkaExampleProducer");
    props.put("key.serializer",LongSerializer.class.getName());
    props.put("value.serializer",StringSerializer.class.getName());
    return new KafkaProducer<>(props);
}

static void runProducer(final int sendMessageCount) throws Exception {
    final Producer<Long, String> producer = createProducer();
    long time = System.currentTimeMillis();
    try {
        for (long index = time; index < time + sendMessageCount; index++) {
            final ProducerRecord<Long, String> record =
                    new ProducerRecord<>(TOPIC, index,
                                "Hello Suvro " + index);
            RecordMetadata metadata = producer.send(record).get();
            long elapsedTime = System.currentTimeMillis() - time;
            System.out.printf("sent record(key=%s value=%s) " +
                            "meta(partition=%d, offset=%d) time=%d\n",
                    record.key(), record.value(), metadata.partition(),
                    metadata.offset(), elapsedTime);
        }
    } finally {
        producer.flush();
        producer.close();
    }
}

public static void main(String args[]) {
    try {
        runProducer(5);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

错误如下:

java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for my-topic-0: 30039 ms has passed since batch creation plus linger time
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.valueOrError(FutureRecordMetadata.java:94)
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:64)
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:29)
at in.gov.enam.etrade.notification.KafkaProducerExample.runProducer(KafkaProducerExample.java:39)
at in.gov.enam.etrade.notification.KafkaProducerExample.main(KafkaProducerExample.java:54)
Caused by: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for my-topic-0: 30039 ms has passed since batch creation plus linger time

然而,当zookeeper,Kafka-server和Producer api与localhost在同一台机器上运行时,整个过程都有效。

我不知道我错过了什么或我哪里出错了。

1 个答案:

答案 0 :(得分:0)

我有类似的问题指向部署在Docker中的Kafka集群。这是因为您的本地计算机和远程Kafka群集之间存在访问问题。如果您能够打包生产者应用程序,在远程谷歌云上部署/运行它,生产者应用程序应该可以正常工作。