部署单机版的kafka消息队列,参考了网上很多的教程,发现网上的教程不是很详细,特别是kafak的配置文件相关的,于是自己搭建完成之后,重新整理并标注了配置文件。
配置单机kafka
关闭selinux,开启防火墙9092端口
1、关闭selinux
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
2、配置防火墙,开启9092端口号
vi /etc/sysconfig/iptables #编辑防火墙配置文件
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9092 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出
service iptables restart #最后重启防火墙使配置生效
安装jdk
kafka运行需要JDK支持
下载地址:https://www.oracle.com/java/technologies/javase-downloads.html
历史版本:https://www.oracle.com/java/technologies/oracle-java-archive-downloads.html
这里稍微多说一句,下载地址偶尔是会变动的,上面的地址是截止到这片文章发布时的最新链接;但是一般Oracle都会做兼容(如果上面的链接失效了,点开后一般会重定向到对应的新地址),下载界面也可能会随着网站整体UI而更新,但都是大同小异,进到Oracle官网按照导航找到需要的JDK版本并不是很难
1、解压安装
shell> mkdir /usr/local/java
shell> cd /usr/local/java
shell> tar -zxvf /tools/jdk-8u241-linux-x64.tar.gz
shell> ll
total 4
drwxr-xr-x 7 10143 10143 4096 Dec 11 18:39 jdk1.8.0_241
2、配置环境变量
shell> vim /etc/profile
文件末尾增加以下配置
# JDK
export JAVA_HOME=/usr/local/java/jdk1.8.0_241
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
使配置生效
source /etc/profile
3、测试
shell> java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
安装ZooKeeper
安装kafka前还需要安装Zookeeper,zookeeper是运行在java环境下的。
1、下载
官网:http://zookeeper.apache.org/releases.html
镜像:https://www.apache.org/dyn/closer.cgi/zookeeper
2、解压
tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz -C /usr/local/
3、修改配置
shell> cd /usr/local/apache-zookeeper-3.5.8-bin/conf/
shell> cp zoo_sample.cfg zoo.cfg
shell> vim zoo.cfg
dataDir=/usr/local/apache-zookeeper-3.5.8-bin/data
dataLogDir=/usr/local/apache-zookeeper-3.5.8-bin/logs
4、启动&停止
# 启动
shell> bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
# 查看状态
shell> bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
# 停止
shell> bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
# 重启
shell> bin/zkServer.sh restart
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
5、测试
# jps,查看QuorumPeerMain是否存在
shell> jps
19748 Jps
19159 QuorumPeerMain
# zkServer.sh status
shell> bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
# zkClient
shell> bin/zkCli.sh -server localhost:2181
Connecting to localhost:2181
2020-03-26 23:41:43,303 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60, built on 04/03/2017 16:19 GMT
2020-03-26 23:41:43,306 [myid:] - INFO [main:Environment@109] - Client environment:host.name=VM_2_24_centos
2020-03-26 23:41:43,306 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_241
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:java.home=/usr/local/java/jdk1.8.0_241/jre
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=/usr/local/zookeeper-3.5.3-beta/bin/../build/classes:/usr/local/zookeeper-3.5.3-beta/bin/../build/lib/*.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/slf4j-log4j12-1.7.5.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/slf4j-api-1.7.5.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jline-2.11.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-util-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-servlet-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-server-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-security-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-io-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-http-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jackson-mapper-asl-1.9.11.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jackson-core-asl-1.9.11.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper-3.5.3-beta/bin/../zookeeper-3.5.3-beta.jar:/usr/local/zookeeper-3.5.3-beta/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.5.3-beta/bin/../conf:.:/usr/local/java/jdk1.8.0_241/lib/dt.jar:/usr/local/java/jdk1.8.0_241/lib/tools.jar
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=<NA>
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Linux
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:os.version=3.10.0-1062.9.1.el7.x86_64
2020-03-26 23:41:43,308 [myid:] - INFO [main:Environment@109] - Client environment:user.name=root
2020-03-26 23:41:43,309 [myid:] - INFO [main:Environment@109] - Client environment:user.home=/root
2020-03-26 23:41:43,309 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=/usr/local/zookeeper-3.5.3-beta
2020-03-26 23:41:43,309 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=52MB
2020-03-26 23:41:43,310 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=228MB
2020-03-26 23:41:43,310 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=57MB
2020-03-26 23:41:43,313 [myid:] - INFO [main:ZooKeeper@865] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5e8c92f4
2020-03-26 23:41:43,323 [myid:] - INFO [main:ClientCnxnSocket@236] - jute.maxbuffer value is 4194304 Bytes
Welcome to ZooKeeper!
2020-03-26 23:41:43,335 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1113] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2020-03-26 23:41:43,401 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@948] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:42446, server: localhost/0:0:0:0:0:0:0:1:2181
2020-03-26 23:41:43,412 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1381] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10009d728340001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
安装kafka
1、安装前准备
安装JDK
安装ZooKeeper
2、解压
shell> tar -zxvf kafka_2.12-2.2.2.tgz -C /usr/local/
3、配置
cd /usr/local/kafka_2.12-2.2.2/config/
vim server.properties
编辑以下内容
############################# Server Basics #############################
broker.id=0 # 集群的话,这个id会不同,也可以自定义
port=9092 # kafka的端口号,可以不配置此项,默认为9092
host.name=172.17.194.139 # 阿里云内网地址
advertised.host.name=123.56.162.92 # 阿里云外网映射地址
# 默认为/tmp/kafka-logs,可能会被删掉
log.dirs=/usr/local/kafka_2.12-2.2.2/logs
# 监听地址,ifconfig查看到的本机真实地址,单节点不配置也可以
# 如果部署在阿里云服务器上,此处不能配置为公网ip,会直接启动不了kafka
# 阿里云服务器上需要配置为:listeners=PLAINTEXT://:9092,或者内网IP
listeners=PLAINTEXT://172.17.194.139:9092
# 打开注释,设置为对外可访问的地址,可以是主机名,也可以是ip地址,但一定是连接kafka的其他机器可以访问到的地址,简单来说就果是云主机并且直接通过外网访问kafka(一般不这么做,但是如果这么做了),需要设置成弹性ip或绑定的域名
# advertised.listeners=PLAINTEXT://tianranll.cn:9092
# 如果部署在本地虚拟机中,此处可以不开注释;
# 如果部署在云服务器中,需要配置此处ip为公网ip
advertised.listeners=PLAINTEXT://123.56.162.92:9092
# zookeeper地址及端口号
# 如果是集群,可能是这样:hostname1:port1,hostname2:port2,hostname3:port3
# 如果是带有chroot:hostname1:port1,hostname2:port2,hostname3:port3/chroot/path
zookeeper.connect=172.17.194.139:2181 # zookeeper如果和kafka装在同一台机器上,设置为阿里云的内网IP即可。
4、启动&停止
# 启动
cd /usr/local/kafka_2.12-2.2.2/
nohup bin/kafka-server-start.sh config/server.properties
如果使用kafka-manager,需要开启JMX_PORT
shell> JMX_PORT=9999 bin/kafka-server-start.sh -daemon config/server.properties
shell> bin/kafka-server-stop.sh
5、测试
1、jps,确认kafka是否存在(或者ps查看kafka相关进程)
2、zookeeper查看注册信息
shell> cd /usr/local/apache-zookeeper-3.5.8-bin/
shell> bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /brokers/ids
[0]
[zk: localhost:2181(CONNECTED) 1] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://VM_2_24_centos:9092"],"jmx_port":-1,"host":"VM_2_24_centos","timestamp":"1585396767598","port":9092,"version":4}
3、kafka命令
创建topic
shell> bin/kafka-topics.sh --create --bootstrap-server 123.56.162.92:9092 --replication-factor 1 --partitions 1 --topic test
查看topic
shell> bin/kafka-topics.sh --list --bootstrap-server 123.56.162.92:9092:9092
test
生产者发送消息
shell> bin/kafka-console-producer.sh --broker-list 123.56.162.92:9092:9092 --topic test
>Hello
>World
消费者消费消息
shell> bin/kafka-console-consumer.sh --bootstrap-server 123.56.162.92:9092 --topic test --from-beginning
Hello
创建启动脚本
cd /usr/local//usr/local/kafka_2.12-2.2.2
vim kafkastart.sh
#!/bin/sh
#启动zookeeper
/usr/local/apache-zookeeper-3.5.8-bin/bin/zkServer.sh start &
sleep 3 #等3秒后执行
#守护进程启动kafka
nohup /usr/local/kafka_2.12-2.2.2/bin/kafka-server-start.sh /usr/local/kafka_2.12-2.2.2/config/server.properties > /dev/null 2>&1 &
创建关闭脚本
vim kafkastop.sh
#!/bin/sh
#关闭zookeeper
/usr/local/apache-zookeeper-3.5.8-bin/bin/zkServer.sh stop &
sleep 3 #等3秒后执行
#关闭kafka
/usr/local/kafka_2.12-2.2.2/bin/kafka-server-stop.sh &
添加脚本执行权限
#添加脚本执行权限
chmod +x kafkastart.sh
chmod +x kafkastop.sh
设置脚本开机自动执行
vi /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/kafka/kafkastart.sh & #设置开机自动在后台运行脚本
:wq! #保存退出
sh /usr/local/kafka/kafkastart.sh #启动kafka
sh /usr/local/kafka/kafkastop.sh #关闭kafka