【问题标题】:Kafka messages not accepted by server (only from remote IP?)服务器不接受Kafka消息(仅来自远程IP?)
【发布时间】:2018-08-21 20:10:04
【问题描述】:

如果我从 C++ 应用程序或命令行、本地计算机生产和消费,我的 Kafka 服务器工作正常。

但从外部 IP 地址无法正常工作:主题已创建,我看到使用带有 ACK 的 tcpdump 的网络流量(意味着 kafka 正在应答),但我发现队列中没有消息,并且java没有给出错误。在日志中找不到任何内容。或谷歌。这是我的应用程序:

public class KafkaPBJProducer {
    private static String KAFKA_TOPIC = "test";

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", "192.168.1.131:9092");
        properties.put("acks", "all");
        properties.put("retries", 0);
        properties.put("batch.size", 16384);
        properties.put("linger.ms", 1);
        properties.put("buffer.memory", 33554432);
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        properties.put("kafka.topic", KAFKA_TOPIC);
        runMainLoop(properties);
    }

    static void runMainLoop(Properties properties) {
        @SuppressWarnings("resource")
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
        for (int count = 0; count < 10; count++) {
            String msg = getMsg(String.valueOf(count));
            System.out.println("Producing message: " + msg);
            producer.send(new ProducerRecord<String, String>(KAFKA_TOPIC, 0, "dev-" + count, msg));
            producer.flush();
        }
    }

    public static String getMsg(String id) {
        JsonObject obj = new JsonObject();
        try {
            obj.addProperty("id", id);
            obj.addProperty("timestamp", new Timestamp(System.currentTimeMillis()).toString());
            obj.addProperty("data", Base64.getEncoder().encodeToString("Hello, World!".getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new Gson().toJson(obj);
    }
}

当它运行时,每条消息都会卡住一分钟(没有显示错误,只是需要很长时间才能发送):

Producing message: {"id":"0","timestamp":"2018-08-21 13:49:10.697","data":"SGVsbG8sIFdvcmxkIQ\u003d\u003d"}
Producing message: {"id":"1","timestamp":"2018-08-21 13:50:10.794","data":"SGVsbG8sIFdvcmxkIQ\u003d\u003d"}
Producing message: {"id":"2","timestamp":"2018-08-21 13:51:10.797","data":"SGVsbG8sIFdvcmxkIQ\u003d\u003d"}
Producing message: {"id":"3","timestamp":"2018-08-21 13:52:10.813","data":"SGVsbG8sIFdvcmxkIQ\u003d\u003d"}

我可以看到主题已创建:

> bin/kafka-topics.sh --list --zookeeper localhost:2181
__consumer_offsets
test

我可以使用命令行手动向主题添加内容:

$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
>one
>two
>four

但是如果我检查主题,只列出手动发送的消息:

$ bin/kafka-console-consumer.sh --bootstrap-server 192.168.1.131:9092 --topic test --from-beginning
one
two
four

(after several minutes, I press CTRL-C)
^CProcessed a total of 3 messages

这里发生了什么?为什么创建了主题但服务器上不接受任何消息?为什么kafka不报任何错误?

TIA!

【问题讨论】:

    标签: apache-kafka kafka-producer-api


    【解决方案1】:

    找到了。基于Kafka - Unable to send a message to a remote server using Java,问题出在配置文件conf/servers.properties上;它需要取消注释这一行:

    advertised.listeners=PLAINTEXT://192.168.1.131:9092
    

    还是谢谢。

    【讨论】:

    猜你喜欢
    • 2018-07-30
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 2015-12-17
    • 2021-08-05
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多