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 producer = new Producer(config);    
        producer.send(new KeyedMessage("test","192.168.255.128", "haloworld!!!"));
        
        producer.close();
    }

你可能感兴趣的:(JAVA)