我正在使用Confluent Platform开始学习IT技术,需要运行Zookeeper (zookeeper-server-start /etc/kafka/zookeeper.properties
)和Kafka (kafka-server-start /etc/kafka/server.properties
)。我正在编写一个Upstart脚本,应该可以同时运行Kafka和Zookeeper。问题是Kafka必须等待Zookeeper准备就绪(因为它依赖于Zookeeper),但我找不到可靠的方法来知道Zookeeper何时准备就绪。以下是在运行Zookeeper服务器启动后尝试的一些伪代码:
Use a hardcoded block
sleep 5
Does not work reliably on slower computers and/or waits longer than needed.
Check when something (hopefully Zookeeper) is running on port 2181
wait until $(echo stat | nc localhost ${port}) is not none
This did not seem to work as it doesn't wait long enough for Zookeeper to accept a Kafka connection.
Check the logs
wait until specific string in zookeeper log is found
This is sketchy and there isn't even a string that cannot also be found on error too (e.g. "binding to port [...]").