Kafka 的安裝及运行

Linux 网络服务器

CentOS 为例子。

Kafka 安裝

获得下载链接,免费下载后缓解压力。

$ wget https://mirror.bit.edu.cn/apache/kafka/2.5.0/kafka_2.12-2.5.0.tgz
$ tar -zxvf kafka_2.12-2.5.0.tgz && cd kafka_2.12-2.5.0
 

实际参照官方网站 Kafka - Quickstart。

运行 Kafka

Kafka 依靠 Zookeeper,因此必须先运作后面一种。Kafak 安装文件下内置了 ZooKeeper,可立即运作不必独立安裝。

$ bin/zookeeper-server-start.sh config/zookeeper.properties
 

运作 Zookeeper 时发觉设备上沒有 Java,出错信息内容以下:

$ bin/zookeeper-server-start.sh config/zookeeper.properties
/root/dev/kafka/bin/kafka-run-class.sh: line 315: exec: java: not found
 

因此必须先安裝 Java。

安裝 Java 全过程中假如 yum 报 https 不正确,提醒 404 repo 详细地址失效等,

yum install java 时的出错信息内容
$ yum install java-1.6.0-openjdk
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
base                                                                                                                                                                                                                                   | 3.6 kB  00:00:00
epel                                                                                                                                                                                                                                   | 4.7 kB  00:00:00
extras                                                                                                                                                                                                                                 | 3.4 kB  00:00:00
https://repo.Mongodb.org/yum/redhat/2.2/MongoDB-org/4.2/x86_64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
To address tHIS issue please refer to the below knowledge base article

https://access.redhat.com/articles/1320623

If above article doesn't help to resolve this issue please create a BUG on https://bugs.centos.org/



 One of the configured repositories failed (MongoDB Repository),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There Are a few ways to work "fix" this:

     1. Contact the upStream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Disable the repository, so yum won't use it by default. Yum will then
        just ignore the repository until you permanently enable it again or use
        --enablerepo for temporary usage:

            yum-config-Manager --disable mongodb-org-4.2

     4. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most Commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=mongodb-org-4.2.skip_if_unavailable=true

failure: repodata/repomd.xml from mongodb-org-4.2: [Errno 256] No more mirrors to try.
https://repo.mongodb.org/yum/redhat/2.2/mongodb-org/4.2/x86_64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
 

依据提醒,实行 yum-config-manager --disable mongodb-org-4.2 后再度安裝就成功了。

查验 Java 的安裝:

$ java -version
java version "1.6.0_38"
OpenJDK Runtime Environment (IcedTea6 1.13.10) (rhel-1.13.10.0.el7_2-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
 

另,假如必须卸载掉,实行 yum Remove java-1.6.0-openjdk

再度运作 bin/zookeeper-server-start.sh config/zookeeper.properties 发觉前边安裝的 Java 版本号不对,低了…

$ bin/zookeeper-server-start.sh config/zookeeper.properties
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/zookeeper/server/quorum/QuorumPeerMain : Unsupported major.minor version 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
	at java.Security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.NET.URLClassLoader.defineClass(URLClassLoader.java:277)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: org.apache.zookeeper.server.quorum.QuorumPeerMain. Program will exit.
 

依据提醒最少应当为 52 的版本号,依据 Unsupported major.minor version 52.0 [duplicate]得知 52 相匹配的词义化版本号。

另外,可根据 yum list available java\* 指令查看到 yum 上可安裝的版本号,找一个符合要求的安裝就可以,yum install java-1.8.0-openjdk

再度查验安裝:

$ java -version
openjdk version "1.8.0_71"
OpenJDK Runtime Environment (build 1.8.0_71-b15)
OpenJDK 64-Bit Server VM (build 25.71-b15, mixed mode)
 

运行 Kafka 服务项目

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

建立 Topic

$ bin/kafka-topics.sh --create --Bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
 

建立经营者

建立经营者推送信息

$ bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test
 

建立顾客

$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
 

这时发信息的地区有增加数据信息时,顾客处会即时获得到。

Mac 该设备

改动 Kafka 配备

$ vi /usr/local/etc/kafka/server.properties
 

listeners=PLAINTEXT://:9092 撤销其注解并改动成以下方式后储存:

# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
- listeners=PLAINTEXT://:9092
  listeners=PLAINTEXT://localhost:9092

# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
 

运行 zookeeper

$ brew services start zookeeper                                          
==> Successfully started `zookeeper` (label: homebrew.mxcl.zookeeper)
 

注: 根据 brew 和 zookeeper 自身的指令运作时,终止服务项目也必须相匹配的指令,即,brew services stop zookeeper 总是终止 brew 运行的服务项目。

运行 kafka

$ brew services start kafka                                                
==> Successfully started `kafka` (label: homebrew.mxcl.kafka)
 

上边是以服务项目方式长驻运行,假如临时性运行,可应用下边的指令:

$ zkServer start
$ kafka-server-start /usr/local/etc/kafka/server.properties
 

建立 Topic

$ kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
 

假如发生以下不正确:

Exception in thread "main" kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
	at kafka.zookeeper.ZooKeeperClient.$anonfun$waitUntilConnected$3(ZooKeeperClient.scala:262)
	at kafka.zookeeper.ZooKeeperClient.waitUntilConnected(ZooKeeperClient.scala:258)
	at kafka.zookeeper.ZooKeeperClient.<Init>(ZooKeeperClient.scala:119)
	at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1863)
	at kafka.admin.TopicCommand$ZookeeperTopicService$.apply(TopicCommand.scala:341)
	at kafka.admin.TopicCommand$.main(TopicCommand.scala:55)
	at kafka.admin.TopicCommand.main(TopicCommand.scala)
 
  • 查验 zookeeper 服务项目是不是一切正常:
$ zkServer status                                                        22:22:32
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.
 

如果是像上边那样,zookeeper 压根没运行取得成功。尽管 brew services start zookeeperzkServer start 都是会提醒运行取得成功,它是较为坑人的地区。

随后不要相信在网上列的那一系列很有可能缘故:

  • host 没绑
  • 端口占用
  • dataDir 文件目录不会有或管理权限不够
  • myid 文档缺少或內容不对
  • ...

这时必须做的是先停用服务项目,

$ brew services start zookeeper
#
$ zkServer stop
 

随后开启 /usr/local/etc/zookeeper/log4j.properties 环境变量看日意在哪,

$ cat /usr/local/etc/zookeeper/log4j.properties
log4j.rootCategory=WARN, zklog
log4j.appender.zklog = org.apache.log4j.RollingFileAppender
log4j.appender.zklog.File = /usr/local/var/log/zookeeper/zookeeper.log
log4j.appender.zklog.Append = true
log4j.appender.zklog.layout = org.apache.log4j.PatternLayout
log4j.appender.zklog.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
 

能够见到,日志文档在 /usr/local/var/log/zookeeper/zookeeper.log 里边,能够看一下里边是否有出错以协助清查运行的难题。

新开窗口应用 tail -f 开启日志,随后再次试着运行 zookeeper 能够见到以下輸出:

$ tail -n 50 -f /usr/local/var/log/zookeeper/zookeeper.log
2020-05-13 09:58:43 QuorumPeerMain [WARN] Either no config or no quorum defined in config, running  in standalone mode
2020-05-13 09:58:43 ContextHandler [WARN] o.e.j.s.ServletContextHandler@e45f292{/,null,UNAVAILABLE} contextPath ends with /*
2020-05-13 09:58:43 ContextHandler [WARN] Empty contextPath
2020-05-13 09:58:43 NIOServerCnxnFactory [ERROR] Thread Thread[main,5,main] died
java.lang.NoSuchMethodError: java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer;
	at org.apache.jute.BinaryOutputArcHive.stringToByteBuffer(BinaryOutputArchive.java:77)
	at org.apache.jute.BinaryOutputArchive.writeString(BinaryOutputArchive.java:107)
	at org.apache.zookeeper.data.Id.serialize(Id.java:50)
	at org.apache.jute.BinaryOutputArchive.writeRecord(BinaryOutputArchive.java:123)
	at org.apache.zookeeper.data.ACL.serialize(ACL.java:51)
	at org.apache.zookeeper.server.ReferenceCountedACLCache.serialize(ReferenceCountedACLCache.java:136)
	at org.apache.zookeeper.server.DataTree.serialize(DataTree.java:1218)
	at org.apache.zookeeper.server.util.SerializeUtils.serializeSnapshot(SerializeUtils.java:152)
	at org.apache.zookeeper.server.persistence.FileSnap.serialize(FileSnap.java:210)
	at org.apache.zookeeper.server.persistence.FileSnap.serialize(FileSnap.java:227)
	at org.apache.zookeeper.server.persistence.FileTxnSnapLog.save(FileTxnSnapLog.java:406)
	at org.apache.zookeeper.server.persistence.FileTxnSnapLog.restore(FileTxnSnapLog.java:248)
	at org.apache.zookeeper.server.ZKDatabase.loadDataBase(ZKDatabase.java:240)
	at org.apache.zookeeper.server.ZooKeeperServer.loadData(ZooKeeperServer.java:290)
	at org.apache.zookeeper.server.ZooKeeperServer.startdata(ZooKeeperServer.java:450)
	at org.apache.zookeeper.server.NIOServerCnxnFactory.startup(NIOServerCnxnFactory.java:764)
	at org.apache.zookeeper.server.ServerCnxnFactory.startup(ServerCnxnFactory.java:98)
	at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:144)
	at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:106)
	at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:64)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:128)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:82)
 

缘故就很明亮了, java 中出错找不着相匹配方式 。依据 Kafka with Zookeeper 3.5.7 Crash NoSuchMethodError: java.nio.ByteBuffer.flip() 这一相近难题下的回应,解决方案有两个:

  • 升級 java 到 1.9
  • 退级 zookeeper 到 3.14.x

看过下应用 brew 安裝后的版本号:

$ java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.252-b09, mixed mode)

$ brew info zookeeper
zookeeper: stable 3.5.7 (bottled), HEAD
Centralized server for distributed coordination of services
...
 

由于是该设备,升級 java 会较为非常容易实际操作些,立即:

$ brew cask install java
...
 

进行后查询 java 版本号,早已赶到了全新的 14.0:

$ java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1 7)
OpenJDK 64-Bit Server VM (build 14.0.1 7, mixed mode, sharing)
 

再度运行 zookeeper,

$ zkServer start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED
 

另外查询日志輸出:

2020-05-13 10:35:47 QuorumPeerMain [WARN] Either no config or no quorum defined in config, running  in standalone mode
2020-05-13 10:35:47 ContextHandler [WARN] o.e.j.s.ServletContextHandler@69b2283a{/,null,UNAVAILABLE} contextPath ends with /*
2020-05-13 10:35:47 ContextHandler [WARN] Empty contextPath
2020-05-13 10:35:47 ZooKeeperServerMain [ERROR] Unable to start AdminServer, exiting abnormally
org.apache.zookeeper.server.admin.AdminServer$AdminServerException: Problem starting AdminServer on address 0.0.0.0, port 8080 and command URL /commands
	at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:107)
	at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:138)
	at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:106)
	at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:64)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:128)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:82)
Caused by: java.io.IOException: Failed to bind to /0.0.0.0:8080
	at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:346)
	at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:308)
	at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
	at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.server.Server.doStart(Server.java:396)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:103)
	... 5 more
Caused by: java.net.BindException: Address already in use
	at java.base/sun.nio.ch.Net.bind0(Native Method)
	at java.base/sun.nio.ch.Net.bind(Net.java:479)
	at java.base/sun.nio.ch.Net.bind(Net.java:468)
	at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:220)
	at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:88)
	at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:342)
	... 12 more
 

能够见到,依然出错,但不正确早已发生变化。来看前边的流程起功效了。再看来新的出错,

错误报告里重要信息内容是 Problem starting AdminServer on address 0.0.0.0, port 8080 and command URL /commands

看来是端口占用的难题,查询 zookeeper 配备并加上 admin.serverPort 配备,改动默认设置的 8080 到别的端口号:

$ vi /usr/local/etc/zookeeper/zoo.cfg
 
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
  admin.serverPort=8089
 

再度试着运行并查询日志:

2020-05-13 10:56:13 QuorumPeerMain [WARN] Either no config or no quorum defined in config, running  in standalone mode
2020-05-13 10:56:13 ContextHandler [WARN] o.e.j.s.ServletContextHandler@69b2283a{/,null,UNAVAILABLE} contextPath ends with /*
2020-05-13 10:56:13 ContextHandler [WARN] Empty contextPath
2020-05-13 10:56:13 ZooKeeperServerMain [ERROR] Unexpected exception, exiting abnormally
java.net.BindException: Address already in use
	at java.base/sun.nio.ch.Net.bind0(Native Method)
	at java.base/sun.nio.ch.Net.bind(Net.java:479)
	at java.base/sun.nio.ch.Net.bind(Net.java:468)
	at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:220)
	at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:88)
	at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
	at org.apache.zookeeper.server.NIOServerCnxnFactory.configure(NIOServerCnxnFactory.java:687)
	at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:143)
	at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:106)
	at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:64)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:128)
	at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:82)
 

不正确早已升级变成 Address already in use。这一便是以前有起过 zookeeper 但忘记了关,无论如何,根据下边相匹配的指令关掉一下就可以:

$ brew services start zookeeper
#
$ zkServer stop
 

再度运行:

$ zkServer start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED
 

查询日志早已沒有异常了:

2020-05-13 11:57:01 QuorumPeerMain [WARN] Either no config or no quorum defined in config, running  in standalone mode
2020-05-13 11:57:02 ContextHandler [WARN] o.e.j.s.ServletContextHandler@69b2283a{/,null,UNAVAILABLE} contextPath ends with /*
2020-05-13 11:57:02 ContextHandler [WARN] Empty contextPath
 

再根据 zkServer status 查询 zookeeper 情况,顺利的话能够见到 Mode: standalone 字眼表明单例模式运行取得成功,由于沒有配群集因此是该设备单例模式。

$ zkServer status                                                            11:57:01
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
 

最终,能够取得成功建立 Topic 了:

$ kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic test.
 

建立经营者

建立经营者并转化成信息:

$ kafka-console-producer --broker-list localhost:9092 --topic test
> hello
 

之上指令实行后会进到互动cmd,每键入一行意味着一条信息。

建立顾客

消費信息:

$ kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
> hello
 

运行之后从序列起止处逐渐消費信息并輸出。

有关資源

  • Kafka - Quickstart
  • 在 MacOS 上安裝 Kafka
  • Apache Kafka Installation on Mac using Homebrew
  • Find (and kill) process locking port 3000 on Mac
  • ZooKepper Unable to start AdminServer, exiting abnormally

The text was updated successfully, but these errors were encountered:

评论(0条)

刀客源码 匿名评论