centos中怎么安装kafka
这篇文章将为大家详细讲解有关centos中怎么安装kafka,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1.kafka专业术语介绍
Broker kafk实例,kafka集群包含一个或多个实例,原则上来说一台主机上只配一个实例。
Topic kafka消息主题,kafka的消息必须属于某个主题,一个Broker存在多个Topic。
Partition
它是物理层的概念,被称之为分区,每个Topic包含一个或者多个分区。Producer
消息发布者,即负责发布消息到kafka的BrokerConsumer
消息消费者,即负责向kafka Broker消费消息的客户端Consumer Group
每个消费者必须属于一个消费者组
1.下载kafka
$ cd /usr/java
[root@43-c59438365-0048-0727982 java]# cd kafka
[root@43-c59438365-0048-0727982 kafka]# wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz
由此可见,我下载的kafka版本是2.3.0
2.解压kafka
[root@43-c59438365-0048-0727982 kafka]# ll
total 55936
-rw-r--r-- 1 root root 57215197 Jun 25 08:38 kafka_2.12-2.3.0.tgz
[root@43-c59438365-0048-0727982 kafka]# tar zxvf kafka_2.12-2.3.0.tgz
...
[root@43-c59438365-0048-0727982 kafka]# ll
total 55940
drwxr-xr-x 6 root root 4096 Jun 20 04:44 kafka_2.12-2.3.0
-rw-r--r-- 1 root root 57215197 Jun 25 08:38 kafka_2.12-2.3.0.tgz
[root@43-c59438365-0048-0727982 kafka]#
3.配置kafka环境变量
$ vi /etc/profile
添加如下两行
export KAFKA_HOME=/usr/java/kafka/kafka_2.12-2.3.0
export PATH=$PATH:$KAFKA_HOME/bin
退出后,执行刷新命令
source profile
4.启动kafka
由于kafka依赖zookeeper,所以启动kafka之前需要先启动zookeeper。所以我们需要找到zookeeper的配置文件,如下
[root@43-c59438365-0048-0727982 config]# ll
total 68
-rw-r--r-- 1 root root 906 Jun 20 04:43 connect-console-sink.properties
-rw-r--r-- 1 root root 909 Jun 20 04:43 connect-console-source.properties
-rw-r--r-- 1 root root 5321 Jun 20 04:43 connect-distributed.properties
-rw-r--r-- 1 root root 883 Jun 20 04:43 connect-file-sink.properties
-rw-r--r-- 1 root root 881 Jun 20 04:43 connect-file-source.properties
-rw-r--r-- 1 root root 1552 Jun 20 04:43 connect-log4j.properties
-rw-r--r-- 1 root root 2262 Jun 20 04:43 connect-standalone.properties
-rw-r--r-- 1 root root 1221 Jun 20 04:43 consumer.properties
-rw-r--r-- 1 root root 4727 Jun 20 04:43 log4j.properties
-rw-r--r-- 1 root root 1925 Jun 20 04:43 producer.properties
-rw-r--r-- 1 root root 6851 Jun 20 04:43 server.properties
-rw-r--r-- 1 root root 1032 Jun 20 04:43 tools-log4j.properties
-rw-r--r-- 1 root root 1169 Jun 20 04:43 trogdor.conf
-rw-r--r-- 1 root root 1023 Jun 20 04:43 zookeeper.properties
[root@43-c59438365-0048-0727982 config]# pwd
/usr/java/kafka/kafka_2.12-2.3.0/config
[root@43-c59438365-0048-0727982 config]#
如上就是zookeeper配置文件的绝对路径,接下来启动zookeeper,进入kafka安装目录bin下,执行如下启动命令
[root@43-c59438365-0048-0727982 bin]# ./zookeeper-server-start.sh /usr/java/kafka/kafka_2.12-2.3.0/config/zookeeper.properties &
[1] 25053
[root@43-c59438365-0048-0727982 bin]# [2019-08-24 15:31:29,375] INFO Reading configuration from: /usr/java/kafka/kafka_2.12-2.3.0/config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...
[2019-08-24 15:31:29,407] INFO tickTime set to 3000 (org.apache.zookeeper.server.ZooKeeperServer)
[2019-08-24 15:31:29,407] INFO minSessionTimeout set to -1 (org.apache.zookeeper.server.ZooKeeperServer)
[2019-08-24 15:31:29,407] INFO maxSessionTimeout set to -1 (org.apache.zookeeper.server.ZooKeeperServer)
[2019-08-24 15:31:29,413] INFO Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory (org.apache.zookeeper.server.ServerCnxnFactory)
[2019-08-24 15:31:29,416] INFO binding to port 0.0.0.0/0.0.0.0:2181 (org.apache.zookeeper.server.NIOServerCnxnFactory)
^C
[root@43-c59438365-0048-0727982 bin]#
如上说明zookeeper启动成功。
接着启动kafka服务
kafka服务启动需要kafka配置文件,配置文件同zookeeper配置文件是一个位置,如/usr/java/kafka/kafka_2.12-2.3.0/config
[root@43-c59438365-0048-0727982 bin]# ./kafka-server-start.sh /usr/java/kafka/kafka_2.12-2.3.0/config/server.properties &
[2] 25406
[root@43-c59438365-0048-0727982 bin]# [2019-08-24 15:37:12,829] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
[2019-08-24 15:37:13,489] INFO Registered signal handlers for TERM, INT, HUP (org.apache.kafka.common.utils.LoggingSignalHandler)
[2019-08-24 15:37:13,490] INFO starting (kafka.server.KafkaServer)
[2019-08-24 15:37:13,491] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)
...
[2019-08-24 15:37:14,823] INFO [SocketServer brokerId=0] Started data-plane processors for 1 acceptors (kafka.network.SocketServer)
[2019-08-24 15:37:14,830] INFO Kafka version: 2.3.0 (org.apache.kafka.common.utils.AppInfoParser)
[2019-08-24 15:37:14,830] INFO Kafka commitId: fc1aaa116b661c8a (org.apache.kafka.common.utils.AppInfoParser)
[2019-08-24 15:37:14,830] INFO Kafka startTimeMs: 1566632234826 (org.apache.kafka.common.utils.AppInfoParser)
[2019-08-24 15:37:14,831] INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
由此可见,kafka启动成功了。
5.测试kafka
创建Topic
[root@43-c59438365-0048-0727982 bin]# kafka-topics.sh --create --zookeeper localhost:2181 --topic myTopic --partitions 1 --replication-factor 1
[2019-08-24 16:15:21,854] INFO Accepted socket connection from /0:0:0:0:0:0:0:1:44731 (org.apache.zookeeper.server.NIOServerCnxnFactory)
[2019-08-24 16:15:21,857] INFO Client attempting to establish new session at /0:0:0:0:0:0:0:1:44731 (org.apache.zookeeper.server.ZooKeeperServer)
[2019-08-24 16:15:21,860] INFO Established session 0x10085c3259c0002 with negotiated timeout 30000 for client /0:0:0:0:0:0:0:1:44731 (org.apache.zookeeper.server.ZooKeeperServer)
[2019-08-24 16:15:22,126] INFO Got user-level KeeperException when processing sessionid:0x10085c3259c0002 type:setData cxid:0x4 zxid:0x44 txntype:-1 reqpath:n/a Error Path:/config/topics/myTopic Error:KeeperErrorCode = NoNode for /config/topics/myTopic (org.apache.zookeeper.server.PrepRequestProcessor)
Created topic myTopic.
由上可知,我们创建了一个叫myTopic的主题。创建生产者 [root@43-c59438365-0048-0727982 bin]# kafka-console-producer.sh --topic myTopic --broker-list localhost:9092
$>
由上可知,生产者创建好了。创建消费者
这里很明显,需要新开一个窗口,
[root@43-c59438365-0048-0727982 bin]# kafka-console-consumer.sh --topic myTopic --bootstrap-server localhost:9092
如下就可以在生产者窗口发送消息,消费者窗口及时收到消息。
##6.kafka开外网
设置kafka的server.properties文件,
listeners=PLAINTEXT://服务器内网ip:9092
advertised.listeners=PLAINTEXT://服务器外网ip:9092
通过ifconfig查看服务器的内网ip。通过内外网映射关系查看服务器外网ip。
运行一段时间后,发现kafka进行被杀掉了,采用如下命令启动kafka服务可以避免这个问题
./kafka-server-start.sh -daemon /usr/java/kafka/kafka_2.12-2.5.0/config/server.properties &
启动命令后加上-daemon
自此结束。
关于centos中怎么安装kafka就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。