我有一个仅由一个经纪人组成的本地kafka集群。 我有两个Akka流:
一个流会定期生成一条包含当前时间戳的消息,并将其保存到kafka主题
另一个流已订阅此主题并计算延迟。
似乎仅在流中的下一条消息生成时才生成消息。这是每两秒发布一条消息(以毫秒为单位)时观察到的延迟:
total: 2019. Before persist: 2001, after: 18
total: 2019. Before persist: 2002, after: 17
total: 2019. Before persist: 2002, after: 17
total: 2019. Before persist: 2002, after: 17
total: 2019. Before persist: 2002, after: 17
total: 2019. Before persist: 2002, after: 17
total: 2020. Before persist: 2002, after: 18
total: 2019. Before persist: 2001, after: 18
total: 2019. Before persist: 2002, after: 17
total: 2019. Before persist: 2002, after: 17
total: 2019. Before persist: 2002, after: 17
如果我将周期增加到5秒,则延迟会相应增加(+ -5000 ms)。
以下是产生的akka流代码:
val producer =
Source.fromIterator(() => Iterator.continually())
.throttle(1, 5.second)
.map { value =>
val currentTime = System.currentTimeMillis()
new ProducerRecord[ByteBuffer, ByteBuffer]("kafka-streams-input", ByteBuffer.wrap(currentTime.toString.getBytes))
}
.runWith(Producer.plainSink(KafkaClient.producerSettings))