kafka部署时出现的天坑

发布时间:2022-03-01 10:18:09 作者:yexindonglai@163.com 阅读(68)

前言

最近在部署kafka单节点时遇到的问题,想着自己弄个单节点演示以下, 好家伙,单启动的时候就报了很多错误;为防止在坑,故将错误信息记录下来;以防万一!

kafka版本

2.4.1

部署环境

阿里云服务器,系统为centos 7.9

解决kafka启动报错 :Socket server failed to bind to 123.57.207.179:9092: 无法指定被请求的地址.

控制台整体错误信息如下

  1. [2021-09-14 10:59:11,134] ERROR [KafkaServer id=0] Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
  2. org.apache.kafka.common.KafkaException: Socket server failed to bind to 123.57.207.179:9092: 无法指定被请求的地址.
  3. at kafka.network.Acceptor.openServerSocket(SocketServer.scala:632)
  4. at kafka.network.Acceptor.<init>(SocketServer.scala:508)
  5. at kafka.network.SocketServer.kafka$network$SocketServer$$createAcceptor(SocketServer.scala:271)
  6. at kafka.network.SocketServer$$anonfun$createDataPlaneAcceptorsAndProcessors$1.apply(SocketServer.scala:240)
  7. at kafka.network.SocketServer$$anonfun$createDataPlaneAcceptorsAndProcessors$1.apply(SocketServer.scala:238)
  8. at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
  9. at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
  10. at kafka.network.SocketServer.createDataPlaneAcceptorsAndProcessors(SocketServer.scala:238)
  11. at kafka.network.SocketServer.startup(SocketServer.scala:121)
  12. at kafka.server.KafkaServer.startup(KafkaServer.scala:263)
  13. at kafka.server.KafkaServerStartable.startup(KafkaServerStartable.scala:44)
  14. at kafka.Kafka$.main(Kafka.scala:84)
  15. at kafka.Kafka.main(Kafka.scala)
  16. Caused by: java.net.BindException: 无法指定被请求的地址
  17. at sun.nio.ch.Net.bind0(Native Method)
  18. at sun.nio.ch.Net.bind(Net.java:433)
  19. at sun.nio.ch.Net.bind(Net.java:425)
  20. at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
  21. at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
  22. at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
  23. at kafka.network.Acceptor.openServerSocket(SocketServer.scala:628)
  24. ... 12 more

原因

其实是kafka的监听地址有问题,不知道为啥,server.properties配置文件中的listeners=PLAINTEXT://123.57.66.105:9092选项
一定要使用内网ip,我这边使用的是公网ip地址(我用的是阿里云服务器),

解决步骤

1、先获取当前系统的内网ip地址

  1. [root@iZ2zee7r7jat94g6t1guhtZ ~]# ifconfig
  2. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  3. inet 172.21.227.173 netmask 255.255.240.0 broadcast 172.21.239.255
  4. inet6 fe80::216:3eff:fe06:1775 prefixlen 64 scopeid 0x20<link>
  5. ether 00:16:3e:06:17:75 txqueuelen 1000 (Ethernet)
  6. RX packets 4053113 bytes 1013685895 (966.7 MiB)
  7. RX errors 0 dropped 0 overruns 0 frame 0
  8. TX packets 3283585 bytes 722364027 (688.9 MiB)
  9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

可以看到。内网ip地址为:172.21.227.173

2、重新设置kafka的server.properties配置文件

接下来,重新设置kafka的server.properties配置文件,将公网ip改为内网ip,使用编辑器打开server.properties

  1. vim /usr/local/kafka/kafka_2.11-2.4.1/server.properties

找到listeners=PLAINTEXT://123.57.66.105:9092,将其改为内网ip

  1. listeners=PLAINTEXT://172.21.227.173:9092

3、重新启动kafka

启动kafka ,-daemon表示守护进程,也就是后台运行

  1. /usr/local/kafka/kafka_2.11-2.4.1/bin/kafka-server-start.sh -daemon /usr/local/kafka/kafka_2.11-2.4.1/config/server.properties

大功告成

解决报错 Timed out waiting for connection while in state: CONNECTING

在启动kafka时报错如下信息

  1. [2021-09-14 11:43:55,494] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
  2. kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
  3. at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply$mcV$sp(ZooKeeperClient.scala:259)
  4. at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:255)
  5. at kafka.zookeeper.ZooKeeperClient$$anonfun$kafka$zookeeper$ZooKeeperClient$$waitUntilConnected$1.apply(ZooKeeperClient.scala:255)
  6. at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:253)
  7. at kafka.zookeeper.ZooKeeperClient.kafka$zookeeper$ZooKeeperClient$$waitUntilConnected(ZooKeeperClient.scala:255)
  8. at kafka.zookeeper.ZooKeeperClient.<init>(ZooKeeperClient.scala:113)
  9. at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1857)
  10. at kafka.server.KafkaServer.kafka$server$KafkaServer$$createZkClient$1(KafkaServer.scala:374)
  11. at kafka.server.KafkaServer.initZkClient(KafkaServer.scala:399)
  12. at kafka.server.KafkaServer.startup(KafkaServer.scala:207)
  13. at kafka.server.KafkaServerStartable.startup(KafkaServerStartable.scala:44)
  14. at kafka.Kafka$.main(Kafka.scala:84)
  15. at kafka.Kafka.main(Kafka.scala)

原因

错误很明显,就是超时了,仔细看,里面报的错误是ZooKeeperClientTimeoutException异常,这就是代表kafka在连接zk的时候超时了;一开始以为是zk挂了,为了解决这个问题,我特意去看了zookeeper,发现是正在运行的,并没有出问题;后来才发现是kafka的配置错了;在kafka的server.properties配置文件中,有一个配置为:zookeeper.connect=123.57.207.179:2181,这个配置将kafka添加zk的节点上,但是我这里配的是外网ip(我用的是阿里云服务器),要解决这个问题其实很简单,只需要将外网ip改为内网ip即可;

解决步骤

1、获取内网ip

通过以下命令即可查看内网ip地址

  1. ifconfig
  2. # 运行后,得到的内网ip为: 172.21.227.173

2、重新设置kafka的server.properties配置文件

接下来,重新设置kafka的server.properties配置文件,将公网ip改为内网ip,使用编辑器打开server.properties

  1. vim /usr/local/kafka/kafka_2.11-2.4.1/server.properties

找到zookeeper.connect=123.57.207.179:2181,将其改为内网ip

  1. zookeeper.connect=172.21.227.173:2181

3、重新启动kafka

大功告成

java连接kafka超时 ZkTimeoutException: Unable to connect to zookeeper server within timeout: 6000

遇到这种问题有如下解决方案
1、 检查zk是否运行
2、 检查linux是否开放了9092端口(kafka默认端口)
3、 检查阿里云服务器是否配置了安全组(我的就是这个问题导致的),安全组可以配置能被外网访问的端口以及黑白名单;安全组配置如下:
在这里插入图片描述
在这里插入图片描述

关键字Kafka