日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 获取kafka lag,聊聊kafka consumer offset lag的监控

發(fā)布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 获取kafka lag,聊聊kafka consumer offset lag的监控 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文主要討論一下kafka consumer offset lag的監(jiān)控

方案

利用官方的類庫

ConsumerOffsetChecker

ConsumerGroupCommand

利用官方的JMX

ConsumerOffsetChecker

在0.8.2.2版本如下

kafka_2.10-0.8.2.2-sources.jar!/kafka/tools/ConsumerOffsetChecker.scala

object ConsumerOffsetChecker extends Logging {

private val consumerMap: mutable.Map[Int, Option[SimpleConsumer]] = mutable.Map()

private val offsetMap: mutable.Map[TopicAndPartition, Long] = mutable.Map()

private var topicPidMap: immutable.Map[String, Seq[Int]] = immutable.Map()

private def getConsumer(zkClient: ZkClient, bid: Int): Option[SimpleConsumer] = {

//...

}

private def processPartition(zkClient: ZkClient,

group: String, topic: String, pid: Int) {

//...

}

private def processTopic(zkClient: ZkClient, group: String, topic: String) {

topicPidMap.get(topic) match {

case Some(pids) =>

pids.sorted.foreach {

pid => processPartition(zkClient, group, topic, pid)

}

case None => // ignore

}

}

private def printBrokerInfo() {

println("BROKER INFO")

for ((bid, consumerOpt)

consumerOpt match {

case Some(consumer) =>

println("%s -> %s:%d".format(bid, consumer.host, consumer.port))

case None => // ignore

}

}

def main(args: Array[String]) {

//...

try {

zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer)

val topicList = topics match {

case Some(x) => x.split(",").view.toList

case None => ZkUtils.getChildren(zkClient, groupDirs.consumerGroupDir + "/owners").toList

}

topicPidMap = immutable.Map(ZkUtils.getPartitionsForTopics(zkClient, topicList).toSeq:_*)

val topicPartitions = topicPidMap.flatMap { case(topic, partitionSeq) => partitionSeq.map(TopicAndPartition(topic, _)) }.toSeq

val channel = ClientUtils.channelToOffsetManager(group, zkClient, channelSocketTimeoutMs, channelRetryBackoffMs)

debug("Sending offset fetch request to coordinator %s:%d.".format(channel.host, channel.port))

channel.send(OffsetFetchRequest(group, topicPartitions))

val offsetFetchResponse = OffsetFetchResponse.readFrom(channel.receive().buffer)

debug("Received offset fetch response %s.".format(offsetFetchResponse))

offsetFetchResponse.requestInfo.foreach { case (topicAndPartition, offsetAndMetadata) =>

if (offsetAndMetadata == OffsetMetadataAndError.NoOffset) {

val topicDirs = new ZKGroupTopicDirs(group, topicAndPartition.topic)

// this group may not have migrated off zookeeper for offsets storage (we don't expose the dual-commit option in this tool

// (meaning the lag may be off until all the consumers in the group have the same setting for offsets storage)

try {

val offset = ZkUtils.readData(zkClient, topicDirs.consumerOffsetDir + "/%d".format(topicAndPartition.partition))._1.toLong

offsetMap.put(topicAndPartition, offset)

} catch {

case z: ZkNoNodeException =>

if(ZkUtils.pathExists(zkClient,topicDirs.consumerOffsetDir))

offsetMap.put(topicAndPartition,-1)

else

throw z

}

}

else if (offsetAndMetadata.error == ErrorMapping.NoError)

offsetMap.put(topicAndPartition, offsetAndMetadata.offset)

else {

println("Could not fetch offset for %s due to %s.".format(topicAndPartition, ErrorMapping.exceptionFor(offsetAndMetadata.error)))

}

}

channel.disconnect()

println("%-15s %-30s %-3s %-15s %-15s %-15s %s".format("Group", "Topic", "Pid", "Offset", "logSize", "Lag", "Owner"))

topicList.sorted.foreach {

topic => processTopic(zkClient, group, topic)

}

if (options.has("broker-info"))

printBrokerInfo()

for ((_, consumerOpt)

consumerOpt match {

case Some(consumer) => consumer.close()

case None => // ignore

}

}

catch {

case t: Throwable =>

println("Exiting due to: %s.".format(t.getMessage))

}

finally {

for (consumerOpt

consumerOpt match {

case Some(consumer) => consumer.close()

case None => // ignore

}

}

if (zkClient != null)

zkClient.close()

if (channel != null)

channel.disconnect()

}

}

}

缺點就是該類是給命令行調(diào)用的,每調(diào)用一次,就new一次zkClient,對于監(jiān)控用來說,不是太合適,需要改造一下,抽取zkClient出來

ConsumerGroupCommand

0.8.2.2以上版本使用ConsumerGroupCommand替代了ConsumerOffsetChecker

kafka_2.11-0.10.2.1-sources.jar!/kafka/admin/ConsumerGroupCommand.scala

object ConsumerGroupCommand extends Logging {

//...

def main(args: Array[String]) {

val opts = new ConsumerGroupCommandOptions(args)

if (args.length == 0)

CommandLineUtils.printUsageAndDie(opts.parser, "List all consumer groups, describe a consumer group, or delete consumer group info.")

// should have exactly one action

val actions = Seq(opts.listOpt, opts.describeOpt, opts.deleteOpt).count(opts.options.has _)

if (actions != 1)

CommandLineUtils.printUsageAndDie(opts.parser, "Command must include exactly one action: --list, --describe, --delete")

opts.checkArgs()

val consumerGroupService = {

if (opts.useOldConsumer) {

System.err.println("Note: This will only show information about consumers that use ZooKeeper (not those using the Java consumer API).\n")

new ZkConsumerGroupService(opts)

} else {

System.err.println("Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers).\n")

new KafkaConsumerGroupService(opts)

}

}

try {

if (opts.options.has(opts.listOpt))

consumerGroupService.listGroups().foreach(println(_))

else if (opts.options.has(opts.describeOpt)) {

val (state, assignments) = consumerGroupService.describeGroup()

val groupId = opts.options.valuesOf(opts.groupOpt).asScala.head

assignments match {

case None =>

// applies to both old and new consumer

printError(s"The consumer group '$groupId' does not exist.")

case Some(assignments) =>

if (opts.useOldConsumer)

printAssignment(assignments, false)

else

state match {

case Some("Dead") =>

printError(s"Consumer group '$groupId' does not exist.")

case Some("Empty") =>

System.err.println(s"Consumer group '$groupId' has no active members.")

printAssignment(assignments, true)

case Some("PreparingRebalance") | Some("AwaitingSync") =>

System.err.println(s"Warning: Consumer group '$groupId' is rebalancing.")

printAssignment(assignments, true)

case Some("Stable") =>

printAssignment(assignments, true)

case other =>

// the control should never reach here

throw new KafkaException(s"Expected a valid consumer group state, but found '${other.getOrElse("NONE")}'.")

}

}

}

else if (opts.options.has(opts.deleteOpt)) {

consumerGroupService match {

case service: ZkConsumerGroupService => service.deleteGroups()

case _ => throw new IllegalStateException(s"delete is not supported for $consumerGroupService.")

}

}

} catch {

case e: Throwable =>

printError(s"Executing consumer group command failed due to ${e.getMessage}", Some(e))

} finally {

consumerGroupService.close()

}

}

}

也是基于命令行來設計的

JMX

這個是利用kafka本身寫入的JMX的數(shù)據(jù),就不用額外在去像ConsumerOffsetChecker去自己連接再去獲取。比如

ObjectName oName = new ObjectName("kafka.producer:*");

Set metricsBeans = mBeanServer.queryNames(oName, null);

for (ObjectName mBeanName : metricsBeans) {

MBeanInfo metricsBean = mBeanServer.getMBeanInfo(mBeanName);

MBeanAttributeInfo[] metricsAttrs = metricsBean.getAttributes();

for (MBeanAttributeInfo metricsAttr : metricsAttrs) {

//get value

Object value = mBeanServer.getAttribute(mBeanName, metricsAttr.getName());

//process ...

}

}

小結(jié)

可以自己改造ConsumerOffsetChecker或者ConsumerGroupCommand,然后上報到statsd或者Prometheus。當然能利用JMX是最省事的了。

doc

總結(jié)

以上是生活随笔為你收集整理的java 获取kafka lag,聊聊kafka consumer offset lag的监控的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。