从0开始搭建kafka客户端
上一节,我们实现了搭建kafka集群。本节我们将从0开始,使用Java,搭建kafka客户端生产消费模型。
1.创建maven项目2.kafka producer3.kafka consumer4.结果生产者:消费者:可能遇到的坑:最后:
1.创建maven项目
首先我们使用idea创建项目。
这里我们使用maven来管理jar包,所以创建的是一个maven项目。
然后输入GroupId和ArtifactId即可,这两个id在maven中相当于“坐标”,其中ArtifactId是你的项目名。
这时候,一个maven项目就创建完成了。但是maven还需要配置,我们在idea中找到Preferences(mac系统快捷键:command + ,),搜索maven,接着配置maven home directory(maven安装路径),User settings file(maven 配置文件所在位置 settings.xml), Local repository(本地仓库位置,在setting.xml中配置),配置完成后,点击APPLY即可。到此,一个maven项目就配置完成了。
最后,我们需要在pom.xml中配置kafka依赖的坐标
1 <dependencies>
2 <dependency>
3 <groupId>org.apache.kafka</groupId>
4 <artifactId>kafka-clients</artifactId>
5 <version>0.11.0.3</version>
6 </dependency>
7</dependencies>
2.kafka producer
接下来,我们要对kafka中的生产者进行开发。在开发之前,要保证我们kafka服务处于可用的状态。
生产者程序如下:
1public class Producer {
2 public static void main(String[] args) {
3 Properties props = new Properties();
4 props.put("bootstrap.servers", "localhost:9092,localhost:9093");
5 props.put("key.serializer",
6 "org.apache.kafka.common.serialization.StringSerializer");
7 props.put("value.serializer",
8 "org.apache.kafka.common.serialization.StringSerializer");
9 props.put("acks", "-1");
10 props.put("retries", 3);
11 props.put("batch.size", 232840);
12 props.put("linger.ms", 10);
13 props.put("buffer.memory", 33554432);
14 props.put("max.block.ms", 3000);
15 KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
16 for (int i = 0; i < 100; i++) {
17 producer.send(new ProducerRecord<String, String>(
18 "my-topic", Integer.toString(i), Integer.toString(i)), new Callback() {
19 public void onCompletion(RecordMetadata recordMetadata, Exception e) {
20 if (e == null) {
21 System.out.println("消息发送成功");
22 } else {
23 System.out.println(String.format("消息发送失败: %s", e.getMessage()));
24 }
25 }
26 });
27 }
28 producer.close();
29 }
30}
3.kafka consumer
接下来是kafka中的消费者代码。
1public class Consumer {
2 public static void main(String[] args) {
3 String topicName = "my-topic";
4 String groupId = "test-group";
5
6 Properties props = new Properties();
7 props.put("bootstrap.servers", "localhost:9092,localhost:9093");
8 props.put("group.id", groupId);
9 props.put("enable.auto.commit", "true");
10 props.put("auto.commit.interval.ms", "1000");
11 props.put("auto.offset.reset", "earliest");
12 props.put("key.deserializer",
13 "org.apache.kafka.common.serialization.StringDeserializer");
14 props.put("value.deserializer",
15 "org.apache.kafka.common.serialization.StringDeserializer");
16 KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
17 // 订阅主题
18 consumer.subscribe(Collections.singletonList(topicName));
19 try {
20 while (true) {
21 ConsumerRecords<String, String> records = consumer.poll(1000);
22 for (ConsumerRecord<String, String> record : records) {
23 System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
24 }
25 }
26 } finally {
27 consumer.close();
28 }
29 }
30}
4.结果
最后是对代码进行测试。依次启动生产者、消费者实例,观察控制台输出接口。
生产者:
1消息发送成功
2...
3消息发送成功
消费者:
1offset = 0, key = 0, value = 0
2...
3offset = 99, key = 99, value = 99
可能遇到的坑:
如果你的程序出错,请首先检查代码props是否正确,其次应该确认你的kafka client和kafka server 版本相同,最后bootstrap.servers这个参数的配置值,要与kafka中server.properties中一致,否则将会出现获取不到元数据信息的异常。(消息发送失败: Failed to update metadata after 3000 ms.)
最后:
上述代码,只是实现了一个最小的生产消费模型,写法上并不规范(比如配置应该写在配置文件中、对于异常应该有处理方法,不能只是输出日志),不能直接使用在生产环境中。只能用于kafka入门学习。
最后,期待您的订阅和点赞,专栏每周都会更新,希望可以和您一起进步,同时也期待您的批评与指正!
从0开始搭建kafka客户端的更多相关文章
- oauth2.0服务端与客户端搭建
oauth2.0服务端与客户端搭建 - 推酷 今天搭建了oauth2.0服务端与客户端.把搭建的过程记录一下.具体实现的功能是:client.ruanwenwu.cn的用户能够通过 server.ru ...
- K8S 搭建 Kafka:2.13-2.6.0 和 Zookeeper:3.6.2 集群
搭建 Kafka:2.13-2.6.0 和 Zookeeper:3.6.2 集群 一.服务版本信息: Kafka:v2.13-2.6.0 Zookeeper:v3.6.2 Kubernetes:v1. ...
- 【原创】Windows平台搭建Kafka源代码开发环境(Eclipse版本)
最近在研究Kafka源代码,需要自己搭建一个开发环境.官网上给出的提示略显简单,照着做了一遍也碰到了一些问题.特此记录下来. 开发环境: Oracle Java 1.7_u71 + Eclipse 4 ...
- centos7搭建kafka集群-第二篇
好了,本篇开始部署kafka集群 Zookeeper集群搭建 注:Kafka集群是把状态保存在Zookeeper中的,首先要搭建Zookeeper集群(也可以用kafka自带的ZK,但不推荐) 1.软 ...
- docker容器中搭建kafka集群环境
Kafka集群管理.状态保存是通过zookeeper实现,所以先要搭建zookeeper集群 zookeeper集群搭建 一.软件环境: zookeeper集群需要超过半数的的node存活才能对外服务 ...
- 利用新版本自带的Zookeeper搭建kafka集群
安装简要说明新版本的kafka自带有zookeeper,其实自带的zookeeper完全够用,本篇文章以记录使用自带zookeeper搭建kafka集群.1.关于kafka下载kafka下载页面:ht ...
- 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...
- 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...
随机推荐
- 新开通blog
从今天开始我有blog了,,以后要经常总结一些自己接触的东西,提升自己
- 扩展 ajaxupload.js ,支持客户端判断上传文件的大小
onSubmit: function(file, extension){}, 修改为 onSubmit: function(file, extension, size){}, if (! (setti ...
- 谈谈有关 Python 的GIL 和 互斥锁
转载:https://blog.csdn.net/Amberdreams/article/details/81274217 有 Python 开发经验的人也许听说过这样一句话:Python 不能充分利 ...
- @EnableGlobalMethodSecurity(prePostEnabled = true)
http://www.bubuko.com/infodetail-2243816.html
- The Mean of the Sample Mean|Standard Deviation of the Sample Mean|SE
7.2 The Mean and Standard Deviation of the Sample Mean Recall that the mean of a variable is denote ...
- every|each|the用于姓氏的复数形式|comrades-in-arms|clothes are|word|steel|affect|effect
________ man in the crowd raised his hand. A. All B. Each C. Every D. Both 题目解析 考查代词的用法.此句意思是:人群 ...
- REMODE解析
版权声明:本文为博主原创文章,未经博主允许不得转载. 纯视觉的三维重建(不考虑用结构光的那一类)常用的有两大类方法:一类是SfM,缺点是计算量比较大,做不到实时运行:另一类是KinectFusion为 ...
- 每天一点Linux-01文档系统
Windows: 以多根的方式组织文档 C: D: E:Linux: 以单根的方式组织文档 / /目录结构: FSH (Filesystem Hierarchy Standard) [root@yan ...
- Witness组件详解
- 如何创建Hexo站点的Tags和Categories默认页面
安装Hexo的categories生成插件 1 $ npm install hexo-generator-category --save 安装Hexo的Tags生成插件 1 $ npm install ...