如果我从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!
答案 0 :(得分:2)
找到了。基于Kafka - Unable to send a message to a remote server using Java,问题出在配置文件 conf / servers.properties 上;它需要取消注释此行:
advertised.listeners=PLAINTEXT://192.168.1.131:9092
还是谢谢。