[英]How to check whether the Kafka service is running or not # GOLang
I am using the below code to push the messages to Kafka Topic.我正在使用以下代码将消息推送到 Kafka 主题。 Sometimes, even when the Kafka is not running, I am getting the success response.
有时,即使 Kafka 没有运行,我也会收到成功响应。 I want to error out when the Kafka is not running.
我想在 Kafka 未运行时出错。 Please help.
请帮忙。
import (
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
func main() {
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost"})
if err != nil {
panic(err)
}
// Delivery report handler for produced messages
go func() {
for e := range p.Events() {
switch ev := e.(type) {
case *kafka.Message:
if ev.TopicPartition.Error != nil {
fmt.Printf("Delivery failed: %v\n", ev.TopicPartition)
} else {
fmt.Printf("Delivered message to %v\n", ev.TopicPartition)
}
}
}
}()
// Produce messages to topic (asynchronously)
topic := "myTopic"
for _, word := range []string{"Welcome", "to", "the", "Confluent", "Kafka", "Golang", "client"} {
p.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
Value: []byte(word),
}, nil)
}
// Wait for message deliveries
p.Flush(15 * 1000)
}
All Kafka brokers must be assigned a broker.id.必须为所有 Kafka 代理分配一个 broker.id。 On startup a broker will create an ephemeral node in Zookeeper with a path of /broker/ids/$id.
启动时,代理将在 Zookeeper 中创建一个临时节点,路径为 /broker/ids/$id。 As the node is ephemeral it will be removed as soon as the broker disconnects, eg by shutting down.
由于节点是短暂的,一旦代理断开连接,它将被删除,例如通过关闭。
You can view the list of the ephemeral broker nodes like so:您可以像这样查看临时代理节点的列表:
echo dump |回声转储 | nc localhost 2181 |
数控本地主机 2181 | grep brokers.
grep 经纪人。
The ZooKeeper client interface exposes a number of commands; ZooKeeper 客户端接口公开了许多命令; dump lists all the sessions and ephemeral nodes for the cluster.
dump 列出集群的所有会话和临时节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.