当前位置:   article > 正文

JMX监控Kafka offset,kafka流量监控,kafka metrics

jmx监控kafka
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.metrics.KafkaMetric;
import org.apache.kafka.common.metrics.Metrics;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class KafkaCoreMetrics {

    public static void main(String[] args) {
        String jmxServiceURL = "service:jmx:rmi:///jndi/rmi://192.168.x.xx:9999/jmxrmi";
        JMXServiceURL jmxURL = null;
        JMXConnector jmxConnector = null;
        MBeanServerConnection jmxs = null;
        ObjectName objectName = null;
        Iterator sampleIter = null;
        Set sampleSet = null;

        try {
            jmxURL = new JMXServiceURL(jmxServiceURL);
            jmxConnector = JMXConnectorFactory.connect(jmxURL);
            jmxs = jmxConnector.getMBeanServerConnection();

            objectName = new ObjectName("kafka.server:type=BrokerTopicMetrics,name=TotalProduceRequestsPerSec,topic=*");
            sampleSet = jmxs.queryMBeans(null, objectName);
            sampleIter = sampleSet.iterator();
            while (sampleIter.hasNext()) {
                ObjectInstance sampleObj = (ObjectInstance) sampleIter.next();
                ObjectName sampleMbeanName = sampleObj.getObjectName();

                String value =  jmxs.getAttribute(sampleMbeanName, "Count").toString();
                System.out.println(sampleMbeanName.getCanonicalName() + "," + sampleMbeanName.getDomain() + "," + sampleMbeanName + "-----------va:" + value);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        } catch (ReflectionException e) {
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
        } catch (MBeanException e) {
            e.printStackTrace();
        }
        try {
            jmxConnector.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


 


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/952472
推荐阅读
相关标签
  

闽ICP备14008679号