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。 啟動 KafkaKafka 依賴 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
根據提示,執行 檢查 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)
另,如果需要解除安裝,執行 再次執行 $ 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 對應的語義化版本。 同時,可通過 再次檢查安裝: $ 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
將 # 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 自己的命令啟動時,停止服務也需要對應的命令,即, 啟動 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)
$ 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 zookeeper
# 或
$ zkServer stop
然後開啟 $ 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
可以看到,日誌檔案在 新開視窗使用 $ 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() 這個類似問題下的回答,解決辦法有兩個:
看了下使用 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
同時檢視日誌輸出:
可以看到,仍然報錯,但錯誤已經變了。看來前面的步驟起作用了。再來看新的報錯, 錯誤資訊裡關鍵資訊是 看來是埠占用的問題,檢視 zookeeper 配置並新增 $ 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
再次嘗試啟動並檢視日誌:
錯誤已經更新成了 $ 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
檢視日誌已經沒有異樣了:
再通過 $ 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
以上命令執行後會進入互動命令列,每輸入一行代表一條訊息。 建立消費者消費訊息: $ kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
> hello
啟動後會從佇列起始處開始消費訊息並輸出。 相關資源 |
The text was updated successfully, but these errors were encountered: |
Kafka 的安裝及啟動
相關文章
- kafka的安裝及使用Kafka
- kafka安裝及使用Kafka
- 安裝node及vue專案的啟動Vue
- Kafka應用實戰——Kafka安裝及簡單使用Kafka
- mac環境canal+mysql+kafka的安裝及使用MacMySqlKafka
- kafka的安裝與使用Kafka
- Linux 安裝kafkaLinuxKafka
- Kafka安裝-linuxKafkaLinux
- Linux安裝KafkaLinuxKafka
- postgresql的安裝和啟動方法SQL
- mongodb的安裝和啟動方法MongoDB
- kafka環境安裝及簡單使用(單機版)Kafka
- Supervisor安裝、配置、開啟啟動
- Mindoc安裝與啟動
- 安裝Kafka叢集Kafka
- 4-kafka安裝Kafka
- kafka 安裝到使用Kafka
- 安裝測試kafkaKafka
- kafka和zookeeper安裝Kafka
- Kafka2.8安裝Kafka
- 下載安裝webstrom及啟用Web
- Windows系統安裝及啟用Windows
- Ubuntu Nvidia driver驅動安裝及解除安裝Ubuntu
- uefi啟動U盤如何安裝系統_uefi啟動u盤安裝系統的方法
- windows 下 MongoDB 的安裝與啟動WindowsMongoDB
- PostgreSQL的安裝和啟動方法大全SQL
- kafka的認識、安裝與配置Kafka
- linux環境安裝——kafka安裝複習LinuxKafka
- Linux之換源、Tomcat及jdk的安裝配置和設定Tomcat自動啟動LinuxTomcatJDK
- Windows 安裝包啟動PostgresqlWindowsSQL
- uefi啟動隨身碟如何安裝系統_uefi啟動u盤安裝系統的方法
- Kafka SSL安裝與配置Kafka
- kafka 安裝部署,使用教程Kafka
- 快速安裝 kafka 叢集Kafka
- 使用 Bitnami Helm 安裝 KafkaKafka
- memcached安裝及開啟SASL驗證
- kafka原始碼剖析(二)之kafka-server的啟動Kafka原始碼Server
- Centos安裝PHP7及配置php-fpm開機啟動CentOSPHP