如何从一台PC上的Kafka生产者向另一台PC上的kafka经纪人发送消息?

时间:2019-08-26 08:07:33

标签: apache-kafka

我正在尝试使用 wifi接口 将消息从一台PC上的Kafka Producer发送到另一台PC上的Kafka Broker,但是消息未出现在指定的主题中在卡夫卡经纪人。

我使用ASUS无线路由器连接了两台PC,并禁用了PC和路由器上的所有防火墙。两台PC彼此ping通。当我转向有线连接时,它可以正常工作,并且消息会提取到kafka broker pc上的指定主题。

卡夫卡制片人:

public class CarDataProducer {
    public static void main(String[] args) {

        CarDataProducer fProducer= new CarDataProducer();
        Producer<String, CarData> producer= fProducer.initializeKafkaProducer();

        String topicName = "IN-DATA";

        CSVReaderCarData csvReader = new CSVReaderCarData();
        List<CarData> CarDataList = csvReader.readCarDataFromCSV("data/mllib/TrainTest_101.csv");

        //read from CSV file and send
        for (CarData val : CarDataList) {
            producer.send(new ProducerRecord<String, CarData>(topicName, val));
        }
    }

    public KafkaProducer<String, CarData> initializeKafkaProducer() {

        // Set the producer configuration properties.
        Properties props = ProducerProperties.getInstance();

        // Instantiate a producerSampleJDBC
        KafkaProducer<String, CarData> producer = new KafkaProducer<String, CarData>(props);

        return producer;
    }

public class ProducerProperties {
    private ProducerProperties() {
    }

    public static final Properties props = new Properties();
    static {
        props.put("bootstrap.servers", "192.168.1.124:9092");
        props.put("acks", "0");
        props.put("retries", 0);
        props.put("batch.size", 500);
        props.put("linger.ms", 500);
        props.put("buffer.memory", 500);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "com.iov.safety.vehicleproducer.CarDataSerializer");
    }

    public static Properties getInstance() {
        return props;
    }
}

使用服务器端的控制台通过Kafka Consumer检查消息接收:

kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic IN-DATA

消息的接收应如下所示:

 kafka-console-consumer.sh --bootstrap-server 192.168.1.124:9092 --topic IN-DATA
{"instSpeed":19.0,"time":15.0,"label":0.0}
{"instSpeed":64.0,"time":15.0,"label":1.0}
{"instSpeed":10.0,"time":16.0,"label":0.0}

服务器端的ifconfig


ifconfig on server side

kafka生产者方面的ipconfig


ipconfig on kafka producer side

  • server.properties:

listeners = PLAINTEXT://:9092

  • netstat -ano | grep'9092'

  

tcp6 0 0 ::: 9092 ::: *
  监听关闭(0.00 / 0/0)tcp6 0 0 127.0.0.1:53880
  127.0.1.1:9092已建立keepalive(6659.53 / 0/0)tcp6 0 0 127.0.1.1:9092 127.0.0.1:53880已建立   keepalive(6659.53 / 0/0)tcp6 0 0 127.0.1.1:9092
  127.0.0.1:53878建立了keepalive(6659.15 / 0/0)tcp6 0 0 127.0.0.1:53878 127.0.1.1:9092建立了   keepalive(6659.15 / 0/0)


通过添加回调以发送kafka生产者,我收到超时错误:

  

org.apache.kafka.common.errors.TimeoutException:IN-DATA-0的8条记录已过期:自上次追加以来已过去30045 ms

1 个答案:

答案 0 :(得分:0)

我解决了。每个Kafka经纪人都必须宣传其主机名/ ip,以便可以在另一台PC上从Kafka Producer进行访问。

 kafka-server-start.sh config/server.properties --override  advertised.listeners=PLAINTEXT://192.168.1.124:9092
sha

相反,我们可以如下更新config / server.properties:

advertised.listeners=PLAINTEXT://your.host.name:9092 

advertised.listeners=PLAINTEXT://192.168.1.124:9092