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();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值