我正在使用 github.com/Shopify/sarama 包与 Kafka 进行交互。在我目前的方法中,我可以连接到代理并毫无问题地获取所有主题名称(下面的消费者代码)。
但是,当我尝试使用管理客户端(下面的管理代码)删除一些主题时,我收到“dial tcp:lookup ip-xx-xx.ec2.internal:no such host”错误。
我不知道为什么会收到此错误。我将不胜感激任何提示或可能的解决方案。
消费者
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
//get broker
cluster, err := sarama.NewConsumer("localhost:9092", config)
if err != nil {
panic(err)
}
defer func() {
if err := cluster.Close(); err != nil {
panic(err)
}
}()
//get all topic from cluster
topics, _ := cluster.Topics()
行政
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
config.Version = sarama.V2_4_0_0
//admin broker
admin, err := sarama.NewClusterAdmin("localhost:9092", config)
if err != nil {
panic(err)
}
defer func() {
if err := admin.Close(); err != nil {
panic(err)
}
}()
topic := []string{"test-topic"}
output := admin.DeleteTopic(topic)
if output == nil {
fmt.Printf(" delete - %s\n", topic[0])
} else {
fmt.Println(output)
}
注意我通过转发 ssh 端口将堡垒实例连接到远程。
更新
设置后sarama.Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)
我得到以下信息:
[sarama] 2020/03/25 02:08:03 Initializing new client
[sarama] 2020/03/25 02:08:03 client/metadata fetching metadata for all topics from broker localhost:9092
[sarama] 2020/03/25 02:08:04 Connected to broker at localhost:9092 (unregistered)
[sarama] 2020/03/25 02:08:04 client/brokers registered new broker #1001 at ip-x-x-x-1.ec2.internal:9092
[sarama] 2020/03/25 02:08:04 client/brokers registered new broker #1003 at ip-x-x-x-2.ec2.internal:9092
[sarama] 2020/03/25 02:08:04 client/brokers registered new broker #1002 at ip-x-x-x-3.ec2.internal:9092
[sarama] 2020/03/25 02:08:04 Successfully initialized new client
[sarama] 2020/03/25 02:08:04 Failed to connect to broker ip-x-x-x-3.ec2.internal:9092: dial tcp: lookup ip-x-x-x-3.ec2.internal: no such host`
更新 2
我的卡夫卡 server.properties:
advertised.listeners=INTERNAL://ip-x-x-x-1.ec2.internal:9091,EXTERNAL_INSECURE://ip-x-x-x-1.ec2.internal:9092
listeners=INTERNAL://:9091,EXTERNAL_INSECURE://:9092
listener.security.protocol.map=INTERNAL:SSL,EXTERNAL_INSECURE:PLAINTEXT