首页 技术 正文
技术 2022年11月14日
0 收藏 786 点赞 5,278 浏览 1128 个字

Kafka发送消息方法如下:

Properties properties = new Properties();

properties.put(“zookeeper.connect”, “server1:2181”);

properties.put(“serializer.class”,StringEncoder.class.getName());

properties.put(“metadata.broker.list”,”server1:9092″);

ProducerConfig config = new ProducerConfig(properties);

Producer<String, String> producer = new Producer<String, String>(config);

producer.send(new KeyedMessage<Integer, String>(“

mytopic

“, “Hello Kafka”));

运行程序出现如下错误:

Exception in thread "Thread-0" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90)
at kafka.producer.Producer.send(Producer.scala:76)
at kafka.javaapi.producer.Producer.send(Producer.scala:33)
atits.kafka.Producer.run(Producer.java:46)

问题原因:

需要改动kafka安装目录下config文件夹下的server.properties中的以下两个属性

zookeeper.connect=localhost:2181改成zookeeper.connect=ip or domainname:2181

以及默认注释掉的

#host.name=localhost改成host.name=ip or domainname

重新启动kafka:

bin/kafka-server-start.sh config/server.properties &

发送消息成功

在kafka安装目录下执行下列语句查看kafka reciever是否收到消息

bin/kafka-console-consumer.sh –zookeeper [ip or domainname]:2181 –topic [topic name] –from-beginning

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289