kafka_2.11 简单使用

本文介绍了如何在Kafka 2.11中创建topic、producer、consumer,并通过API进行操作。首先,使用kafka.topics.sh脚本创建topic。接着,通过命令行创建producer和consumer,理解其工作原理。最后,通过Maven项目引入Kafka依赖,并展示了简单的producer和consumer的API使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进过上一篇的引导,相信大家应该都已经安装好了。这次我们就来简单使用一下。

1 创建 topic

像官网上写的那样,我们可以使用 kafka.topics.sh 脚本来创建一个 topic 。不过,前提是我们需要启动zookeeper 和 kafka 服务。
这里写图片描述

那我们就用官网上例子好了:

[root@master config]# kafka-topics.sh --create --zookeeper localhost:2181 --topic first                                      

Missing required argument "[partitions]"

Option                                   Description                            
------                                   -----------                            
--alter                                  Alter the number of partitions,        
                                           replica assignment, and/or           
                                           configuration for the topic.         
--config <name=value>                    A topic configuration override for the 
                                           topic being created or altered.The   
                                           following is a list of valid         
                                           configurations:                      
                                                cleanup.policy                        
                                                compression.type                      
                                                delete.retention.ms                   
                                                file.delete.delay.ms                  
                                                flush.messages                        
                                                flush.ms                              
                                                follower.replication.throttled.       
                                           replicas                             
                                                index.interval.bytes                  
                                                leader.replication.throttled.replicas 
                                                max.message.bytes                     
                                                message.format.version                
                                                message.timestamp.difference.max.ms   
                                                message.timestamp.type                
                                                min.cleanable.dirty.ratio             
                                                min.compaction.lag.ms                 
                                                min.insync.replicas                   
                                                preallocate                           
                                                retention.bytes                       
                                                retention.ms                          
                                                segment.bytes                         
                                                segment.index.bytes                   
                                                segment.jitter.ms                     
                                                segment.ms                            
                                                unclean.leader.election.enable        
                                         See the Kafka documentation for full   
                                           details on the topic configs.        
--create                                 Create a new topic.                    
--delete                                 Delete a topic                         
--delete-config <name>                   A topic configuration override to be   
                                           removed for an existing topic (see   
                                           the list of configurations under the 
                                           --config option).                    
--describe                               List details for the given topics.     
--disable-rack-aware                     Disable rack aware replica assignment  
--force                                  Suppress console prompts               
--help                                   Print usage information.               
--if-exists                              if set when altering or deleting       
                                           topics, the action will only execute 
                                           if the topic exists                  
--if-not-exists                          if set when creating topics, the       
                                           action will only execute if the      
                                           topic does not already exist         
--list                                   List all available topics.             
--partitions <Integer: # of partitions>  The number of partitions for the topic 
                                           being created or altered (WARNING:   
                                           If partitions are increased for a    
                                           topic that has a key, the partition  
                                           logic or ordering of the messages    
                                           will be affected                     
--replica-assignment                     A list of manual partition-to-broker   
  <broker_id_for_part1_replica1
### 如何在Ubuntu上部署Kafka 2.11集群 #### 准备工作 为了成功部署Kafka 2.11集群,在Ubuntu环境中需先完成一些准备工作。确保操作系统是最新的,并安装必要的依赖项,如Java环境。 ```bash sudo apt-get update && sudo apt-get upgrade -y sudo apt install openjdk-8-jdk -y java -version ``` 确认已正确设置JAVA_HOME变量[^1]。 #### 下载并解压Kafka软件包 访问Apache Kafka官方网站获取指定版本的二进制分发版,对于本案例而言即为2.11版本: ```bash wget https://downloads.apache.org/kafka/2.11/kafka_2.11-2.11.0.tgz tar zxvf kafka_2.11-2.11.0.tgz cd kafka_2.11-2.11.0/ ``` #### 配置Zookeeper服务 由于Kafka依赖于ZooKeeper来管理其元数据信息,因此需要启动一个或多个ZooKeeper实例作为前置条件。可以利用自带脚本来快速初始化单机模式下的ZooKeeper服务器: ```bash bin/zookeeper-server-start.sh config/zookeeper.properties & ``` #### 修改Broker配置文件 编辑`config/server.properties`以适应多节点集群的需求,主要调整如下几个参数: - `broker.id`: 设置唯一的代理ID编号。 - `listeners`: 定义监听地址,默认即可满足需求。 - `advertised.listeners`: 对外发布的连接字符串,当内部网络与外部网络存在差异时尤为有用。 - `log.dirs`: 日志存储路径,建议指向具有足够空间的数据盘位置。 针对每台机器上的server.properties做相应修改后保存退出。 #### 启动Kafka Broker进程 通过命令行工具依次开启各个节点的服务端口: ```bash nohup bin/kafka-server-start.sh config/server.properties >/tmp/kafka.log 2>&1 & ``` 此时应该能看到日志输出表明服务已经正常运行起来。 #### 创建Topic测试连通性 最后一步是验证整个集群能否协同作业,创建一个新的主题topic用于消息传递实验: ```bash bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor N --partitions M --topic test-topic-name ``` 其中N代表副本数量而M则是分区数目,这两个数值应依据实际硬件资源情况合理规划。 以上就是在Ubuntu平台上构建Kafka 2.11分布式系统的简易指导流程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值