kafka代碼初步涉獵,用代碼發送消息到客戶端

服務端發送消息

[2016-07-13 07:35:03,473] ERROR Error when sending message to topic test with key: null, value: 2 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
[2016-07-13 07:36:03,502] ERROR Error when sending message to topic test with key: null, value: 4 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
^C[root@localhost kafka_2.11-0.10.0.0]#
[root@localhost kafka_2.11-0.10.0.0]#
[root@localhost kafka_2.11-0.10.0.0]#
[root@localhost kafka_2.11-0.10.0.0]# bin/kafka-console-producer.sh --broker-list 192.168.255.128:9092 --topic test
sa
dsada
dsadsadasdasdasdas





收消息:

其中客戶端的haloworld 由代碼實現發送


直接上代碼客戶端接[root@localhost kafka]# ls
kafka_2.11-0.10.0.0  kafka_2.11-0.10.0.0.tgz
[root@localhost kafka]# cd kafka_2.11-0.10.0.0
[root@localhost kafka_2.11-0.10.0.0]# bin/kafka-console-consumer.sh --zookeeper 192.168.255.128:2181 --topic  test --from-beginning

dsada
sa
dsada
dsadsadasdasdasdas
haloworld!!!


#######################################


    public static void main(String[] args) {
        Properties props=new Properties();
        props.put("zk.connect", "192.168.255.128:2181");  
        props.put("metadata.broker.list", "192.168.255.128:9092");
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        props.put("request.required.acks", "1");
        ProducerConfig config = new ProducerConfig(props);  
        Producer<String, String> producer = new Producer<String, String>(config);    
        producer.send(new KeyedMessage<String,String>("test","192.168.255.128", "haloworld!!!"));
        
        producer.close();
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章