我刚开始用一个简单的例子来读取 jmx 指标,并使用 HeapMemoryUsage、CPUTime 等的简单值来感受它。我需要尝试访问 kafka 服务器/消费者指标,特别是滞后,我可以看到它在 FetcherLagMetrics-ConsumerLag 下的 jconsole 应用程序中可见。但以编程方式我收到以下错误:
javax.management.InstanceNotFoundException: kafka.consumer:type=consumer-
fetch-manager-metrics
这告诉我消费者获取管理器指标是问题,因为它甚至在 jconsole 中也不存在。我将其更改为以下内容,但仍然是相同的问题:
consumerBean = jmxCon.getMBeanServerConnection().getAttribute(new
ObjectName("kafka.server:type=FetcherLagMetrics"),"ConsumerLag");
cd = (CompositeData) consumerBean;
尝试访问这些值的代码如下:
jmxCon.getMBeanServerConnection().invoke(new
ObjectName("java.lang:type=Memory"), "gc", null, null);
for (int i = 0; i < 100; i++) {
//get an instance of the HeapMemoryUsage Mbean
memoryMbean = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage");
cd = (CompositeData) memoryMbean;
//get an instance of the OperatingSystem Mbean
osMbean = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=OperatingSystem"),"ProcessCpuTime");
//get an instance of the kafka metrics Mbean
consumerBean = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("kafka.consumer:type=consumer-fetch-manager-metrics"),"MaxLag");
cd = (CompositeData) consumerBean;
consumerBeanII = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("kafka.server:type=FetcherLagMetrics,name=ConsumerLag"),"Lag");
System.out.println("Used memory: " + " " + cd.get("MaxLag") + " Used cpu: " + consumerBean); //print memory usage
tempMemory = tempMemory + Long.parseLong(cd.get("used").toString());
Thread.sleep(1000); //delay for one second
}
它在 consumerBean = 行失败 ......有人可以解释或提供使用 jmx /JMI 实现访问 kafka 指标的正确方法吗?
相关分类