我正在尝试使用kafka的kafka.zk.EmbeddedZookeeper
和kafka.server.KafkaServer
返回kafka.utils.TestUtils/createServer
来运行kafka服务器以进行测试。
但是我遇到了试图发送消息超时的障碍,并返回KafkaProducer$Future
失败。下面是我正在使用的kafka版本。下面的代码是Clojure与Kafka库互操作。
[org.apache.kafka/kafka_2.11 "0.10.0.1"]
[org.apache.kafka/kafka-clients "0.10.1.0"]
这是我能走多远。
此SO question表示传递正确的Time
对象非常重要。但MockTime看似合理的实施。有人之前解决了这个问题吗?
;; 1. Create Zookeeper
(require '[clojure.test :refer :all]
'[kafkaesque.topics :as kt]
'[kafkaesque.utils :as ku]
'[clojure.pprint :refer [pprint]])
(import '[java.nio.file Files]
'[kafka.zk EmbeddedZookeeper]
'[kafka.server KafkaServer KafkaConfig]
'[kafka.utils TestUtils Time MockTime])
(def zk-config {:zkhost "127.0.0.1"})
(def topic-name "client-test")
(def ^EmbeddedZookeeper zkServer (EmbeddedZookeeper.))
;; 2. Create Kafka Broker
(def zk-connect-str (str "127.0.0.1" ":" (.port zkServer)))
(def zku ((ZkUtils/apply (ZkUtils/createZkClient zk-connect-str 10000 8000) false)))
(def brokerhost "127.0.0.1")
(def brokerport "9092")
(def ^KafkaConfig config (KafkaConfig. {"zookeeper.connect" zk-connect-str
"broker.id" "0"
"log.dirs" (.toString
(.toAbsolutePath
(Files/createTempDirectory
"kafka-" (make-array java.nio.file.attribute.FileAttribute 0))))
"listeners" (str "PLAINTEXT://" brokerhost ":" brokerport)}))
(def ^Time mock (MockTime.))
(def ^KafkaServer kafkaServer (TestUtils/createServer config mock))
;; 3. Create a Topic
(kt/create! zku topic-name 1 1 {})
(kt/topic-exists? zku topic-name) ;; returns true
;; 4. Create a Producer and ProducerRecord
(def producer-a (kc/producer {"bootstrap.servers" "127.0.0.1:9092"
"acks" "all"
"retries" "0"
"batch.size" "16384"
"linger.ms" "1"
"buffer.memory" "33554432"
"key.serializer" "org.apache.kafka.common.serialization.StringSerializer"
"value.serializer" "org.apache.kafka.common.serialization.StringSerializer"}))
(def message-key "k1")
(def message-value "foobar")
(def record-a (kc/producer-record topic-name 0 message-key message-value))
;; 5. Send a message
(def send-result (kc/send! producer-a record-a)) ;; Times out, and returns a KafkaProducer$Future failure.
答案 0 :(得分:0)
感谢@amethystic指出我的阅读障碍:)问题是版本不匹配。我使用 org.apache.kafka / kafka_2.11 “0.10.0.1”,与 org.apache.kafka / kafka-clients <强> “0.10.1.0”即可。
我将所有版本移至“0.10.1.0”,一切都按预期工作。
希望这有帮助。