Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

problem connecting to Apache Kafka from Java

I am trying to connect to one Kafka server. in Java, I have configured:

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewTopic;
.
.
Properties properties = new Properties();
String bootstrapServers = "localhost:9092";
properties.put("bootstrap.servers", bootstrapServers);
properties.put("connections.max.idle.ms", 10000);
properties.put("request.timeout.ms", 5000);
try {
        AdminClient adminClient = AdminClient.create(properties);
        NewTopic newTopic = new NewTopic("testtopic", 1, (short)1); 
.
.

Although there is a configuration to use localhost, I get errors:

INFO: AdminClientConfig values: 
    bootstrap.servers = [localhost:9092]
Jan 22, 2021 9:48:01 PM org.apache.kafka.common.utils.AppInfoParser$AppInfo <init>
INFO: Kafka version: 2.7.0
Jan 22, 2021 9:48:01 PM org.apache.kafka.common.utils.AppInfoParser$AppInfo <init>
INFO: Kafka commitId: 448719dc99a19793
Jan 22, 2021 9:48:01 PM org.apache.kafka.common.utils.AppInfoParser$AppInfo <init>
INFO: Kafka startTimeMs: 1611348481265
Jan 22, 2021 9:48:03 PM org.apache.kafka.clients.NetworkClient initiateConnect
WARNING: [AdminClient clientId=adminclient-1] Error connecting to node e4f0851caf23:9092 (id: 1001 rack: null)
java.net.UnknownHostException: No such host is known (e4f0851caf23)

Is there any additional configuration that I am missing? This is only a sample code, I am trying to create one topic.

thank you!

question from:https://stackoverflow.com/questions/65852633/problem-connecting-to-apache-kafka-from-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thank you yuppie-flu! I have to add:

KAFKA_CFG_ADVERTISED_LISTENERS=INTERNAL://kafka:9093,CLIENT://kafka:9092


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...