Kafka 启动报错java.io.IOException: Can't resolve address.

时间:2024-04-10 16:35:28
阿里云上 部署Kafka 启动报错java.io.IOException: Can't resolve address.
本地调试的,报错
需要在本地添加阿里云主机的 host 映射
linux kafka server 192.168.0.141 hostname bogon
在windows
java 代码
package myzookeeper;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class myCustomer {
public static void  main(String[] args) {
Properties props = new Properties();  
        props.put("bootstrap.servers", "192.168.0.141:9092");  
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");  
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");  
        Producer producer = new KafkaProducer<>(props);  
        producer.send(new ProducerRecord<>("mytopic", "axxxx", "axxxx"));
        System.out.println("1");
        producer.close();  
}
}
报错 Error connecting to node 0 at bogon:9092:
不能解析bogon
kafka 连接原理
首先连接 192.168.0.141:9092
再连接返回的host.name = bogon,
最后继续连接advertised.host.name=bogon
解决办法
添加window解析
hosts 文件增加 
192.168.0.141 bogon
用cmd ping bogon 试试如果可以ping通即可。