在Linux环境下安装Kafka

 更新时间:2021年08月19日 16:45:56   作者:老周聊架构  
本文以安装Kafka的步骤做了图文详细介绍,Kafka是一种高吞吐量 的分布式发布订阅消息系统,对Kagka感兴趣的小伙伴可以参考参考本篇文章

一、环境准备

jdk下载地址链接:下载地址

zookeeper下载地址链接:下载地址

kafka下载地址链接:下载地址

1.1 Java环境为前提

1.1.1 上传jdk-8u261-linux-x64.rpm到服务器并安装

# 安装命令
rpm -ivh jdk-8u261-linux-x64.rpm 

在这里插入图片描述

1.1.2 配置环境变量

# 编辑配置文件,jdk的bin目录到/etc/profile文件,对所有用户的shell有效
vim /etc/profile
# 生效
source /etc/profile

在这里插入图片描述

export JAVA_HOME=/usr/java/jdk1.8.0_261-amd64
export PATH=$PATH:$JAVA_HOME/bin

在这里插入图片描述

# 验证
java -version

在这里插入图片描述

至此,jdk安装成功。

1.2 Zookeeper的安装配置

1.2.1 上传zookeeper-3.4.14.tar.gz到服务器,解压到/opt

# 解压zk到指定目录
tar -zxf zookeeper-3.4.14.tar.gz -C /opt

在这里插入图片描述

1.2.2 修改Zookeeper保存数据的目录,dataDir

# 进入conf配置目录
cd /opt/zookeeper-3.4.14/conf
# 复制zoo_sample.cfg命名为zoo.cfg
cp zoo_sample.cfg zoo.cfg
# 编辑zoo.cfg文件
vim zoo.cfg
dataDir=/var/riemann/zookeeper/data

在这里插入图片描述
在这里插入图片描述

1.2.3 编辑/etc/profile,使配置生效

设置环境变量ZOO_LOG_DIR,指定Zookeeper保存日志的位置;
ZOOKEEPER_PREFIX指向Zookeeper的解压目录;
Zookeeperbin目录添加到PATH中:

在这里插入图片描述

export ZOOKEEPER_PREFIX=/opt/zookeeper-3.4.14
export PATH=$PATH:$ZOOKEEPER_PREFIX/bin
export ZOO_LOG_DIR=/var/riemann/zookeeper/log

配置完以后再生效配置:

source /etc/profile 

1.2.4 启动Zookeeper,确认Zookeeper的状态

zkServer.sh start

在这里插入图片描述

至此,zookeeper安装成功。

1.3 Kafka 的安装与配置

1.3.1 上传kafka_2.12-1.0.2.tgz到服务器并解压

tar -zxf kafka_2.12-1.0.2.tgz -C /opt

在这里插入图片描述

1.3.2 配置环境变量并生效

vim /etc/profile 
export KAFKA=/opt/kafka_2.12-1.0.2
export PATH=$PATH:$KAFKA/bin
source /etc/profile 

1.3.3 配置/opt/kafka_2.12-1.0.2/config中的server.properties文件

vi  /opt/kafka_2.12-1.0.2/config/server.properties

在这里插入图片描述

Kafka连接Zookeeper的地址,此处使用本地启动的Zookeeper实例
连接地址是localhost:2181
后面的 myKafkaKafka在Zookeeper中的根节点路径

在这里插入图片描述

配置kafka存储持久化数据目录

在这里插入图片描述

log.dirs=/var/riemann/kafka/kafka-logs

创建上述持久化数据目录

mkdir -p /var/riemann/kafka/kafka-logs

1.4 启动Kafka

进入Kafka安装的根目录,执行如下命令:

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

在这里插入图片描述

启动成功,可以看到控制台输出的最后一行的started状态:此时kafka安装成功。

在这里插入图片描述

1.5 重新开一个窗口,查看Zookeeper的节点


在这里插入图片描述

1.6 此时Kafka是前台模式启动,要停止,使用Ctrl+C

如果要后台启动,使用命令:

kafka-server-start.sh -daemon config/server.properties 

查看Kafka的后台进程:

ps -ef | grep kafka

在这里插入图片描述

停止后台运行的Kafka:

kafka-server-stop.sh 

二、生产与消费

查看zookeeper状态,zookeeper启动成功,再启动kafka。

在这里插入图片描述

2.1 kafka-topics.sh 用于管理主题

查看命令的帮助信息

[root@master1 bin]# kafka-topics.sh
Create, delete, describe, or change a topic.
Option                                   Description                            
------                                   -----------                            
--alter                                  Alter the number of partitions,        
                                           replica assignment, and/or           
                                           configuration for the topic.         
--config <String: 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 <String: 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 <String:            A list of manual partition-to-broker   
  broker_id_for_part1_replica1 :           assignments for the topic being      
  broker_id_for_part1_replica2 ,           created or altered.                  
  broker_id_for_part2_replica1 :                                                
  broker_id_for_part2_replica2 , ...>                                           
--replication-factor <Integer:           The replication factor for each        
  replication factor>                      partition in the topic being created.
--topic <String: topic>                  The topic to be create, alter or       
                                           describe. Can also accept a regular  
                                           expression except for --create option
--topics-with-overrides                  if set when describing topics, only    
                                           show topics that have overridden     
                                           configs                              
--unavailable-partitions                 if set when describing topics, only    
                                           show partitions whose leader is not  
                                           available                            
--under-replicated-partitions            if set when describing topics, only    
                                           show under replicated partitions     
--zookeeper <String: urls>               REQUIRED: The connection string for    
                                           the zookeeper connection in the form 
                                           host:port. Multiple URLS can be      
                                           given to allow fail-over.            
[root@master1 bin]# 
# 列出现有的主题
[root@master1 ~]# kafka-topics.sh --list --zookeeper localhost:2181/myKafka
# 创建主题,该主题包含一个分区,该分区为Leader分区,它没有Follower分区副本。
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --create --topic topic_test --partitions 1 --replication-factor 1
# 查看分区信息
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
# 查看指定主题的详细信息
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --describe --topic topic_test 
# 删除指定主题
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --delete --topic topic_test 

列出现有主题,创建主题,该主题包含一个分区,该分区为Leader分区,它没有Follower分区副本。

在这里插入图片描述

查看指定主题的详细信息

在这里插入图片描述

创建主题,该主题包含多个分区

多个分区:横向扩展
多个副本:高可用

在这里插入图片描述

2.2 kafka-console-consumer.sh用于消费消息

# 开启消费者
[root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test 
# 开启消费者方式二,从头消费,不按照偏移量消费
[root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test --from-beginning

2.3 kafka-console-producer.sh用于生产消息

# 开启生产者
[root@node1 ~]# kafka-console-producer.sh --topic topic_test --broker-list  localhost:9020

2.4 具体操作

开启消费者和生产者,生产并消费消息。

在这里插入图片描述

消费者,按照偏移量消费

在这里插入图片描述

消费者从头消费,不按照偏移量消费

在这里插入图片描述

到此这篇关于在Linux环境下安装Kafka的文章就介绍到这了,更多相关Linux下安装Kafka内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Centos安装MYSQL8.X的教程

    Centos安装MYSQL8.X的教程

    这篇文章主要介绍了Centos安装MYSQL8.X的教程,本文以CentOS版本为7.6,安装的MySQL版本为8.0.17,感兴趣的朋友跟随小编一起看看吧
    2019-10-10
  • apache中使用mod_log_slow分析响应慢的请求

    apache中使用mod_log_slow分析响应慢的请求

    这篇文章主要介绍了apache中使用mod_log_slow分析响应慢的请求,使用mod_log_slow可以定位到响应慢的PHP代码位置,需要的朋友可以参考下
    2014-06-06
  • Apache Thrift环境配置

    Apache Thrift环境配置

    这篇文章主要介绍了Apache Thrift环境配置的相关资料,需要的朋友可以参考下
    2017-10-10
  • Linux上如何安装配置hive

    Linux上如何安装配置hive

    本文详细介绍了如何在Linux系统下安装和配置Hive,包括安装前的准备工作(如确保已安装Hadoop和MySQL),以及具体的下载、上传、解压、配置环境变量和修改配置文件等步骤,还涉及到了可能遇到的问题如驱动引擎缺失和MySQL连接错误的解决方法
    2024-09-09
  • linux中设置nexus开机自启动的方法

    linux中设置nexus开机自启动的方法

    这篇文章主要介绍了linux中设置nexus开机自启动的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • centos安装jdk1.8时出现没有/lib/ld-linux.so.2:这个文件的原因分析

    centos安装jdk1.8时出现没有/lib/ld-linux.so.2:这个文件的原因分析

    这篇文章主要介绍了centos安装jdk1.8时出现没有/lib/ld-linux.so.2:这个文件的原因分析,通过使用一个简单的命令可以帮助我们解决,需要的朋友跟随脚本之家小编一起看看吧
    2018-08-08
  • 详解CentOS7 FTP服务搭建(虚拟用户访问FTP服务)

    详解CentOS7 FTP服务搭建(虚拟用户访问FTP服务)

    Liunx外部文件的传输,避免不了使用FTP服务,所以现在就整理下,CentOS7环境下,FTP服务的搭建。有兴趣的可以了解一下。
    2017-01-01
  • Linux下alias命令的用法详解

    Linux下alias命令的用法详解

    在本篇文章里小编给大家分享了关于Linux下alias命令的用法的相关知识点内容,有兴趣的朋友们学习下。
    2019-02-02
  • crontab执行结果未通过发送mail通知用户的方法

    crontab执行结果未通过发送mail通知用户的方法

    这篇文章主要给大家介绍了关于crontab执行结果未通过发送mail通知用户的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用linux系统具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • Linux下安装telnet的方法

    Linux下安装telnet的方法

    这篇文章主要介绍了Linux下安装telnet的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07

最新评论