最開始我將kafka和zookeeper部署在遠程服務器上面,然后在本地用sarama連接kafka並且向里面寫數據,但是報錯:
send msg failed,err: dial tcp: lookup localhost.localdomain: getaddrinfow: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative se
這是因為我修改kafka和zookeeper的配置文件的時候只修改了日志文件存放位置這個配置選項,沒有增加listeners和advertised.listeners這兩個選項
所以在kafka的配置文件里面server.properties這個文件中增加這兩個配置選項
listeners=PLAINTEXT://:9099 advertised.listeners=PLAINTEXT://175.24.115.7:9099
但是我在sarama的代碼中寫的是:
client, err := sarama.NewSyncProducer([]string{"175.24.115.7:9092"}, config)
所以在連接的時候又出現了下面這個錯誤:
producer closed,err: kafka: client has run out of available brokers to talk to (Is your cluster reachable?)
然后我修改了端口,就可以正常使用了:
listeners=PLAINTEXT://:9092 advertised.listeners=PLAINTEXT://175.24.115.7:9092
我想我出現這些問題的原因是最開始沒有設置listeners和advertised.listeners這兩個配置,因為我們的客戶端連接kafka的時候首先是與zookeeper連接,然后通過zookeeper獲取kafka的ip和端口,如果不設置listeners和advertised.listeners這兩個配置,那么zookeeper就用默認的localhost代替kafka的ip,然而我們在遠程連接kafka的時候肯定不能夠使用localhost去連接,所以需要通過advertised.listeners設置的ip去連接。