一、编程手段获取监控指标:查看要监控哪个指标

|NO.Z.00090|——————————|BigDataEnd|——|Hadoop&kafka.V04|——|kafka.v04|编程手段获取监控指标|_java

二、监控指标项目准备主题数据

### --- 查看现有的主题

[root@hadoop01 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --describe --topic topic_x
Topic:topic_x PartitionCount:3 ReplicationFactor:2 Configs:
Topic: topic_x Partition: 0 Leader: 0 Replicas: 0,2 Isr: 0,2
Topic: topic_x Partition: 1 Leader: 1 Replicas: 1,0 Isr: 1,0
Topic: topic_x Partition: 2 Leader: 2 Replicas: 2,1 Isr: 2,1
### --- 向主题里面写入消息

~~~ # 准备10000条消息的数据文件
[root@hadoop01 ~]# for i in `seq 10000000`; do echo "hello yanqi $i" >> msg.txt; done

~~~ # 写入数据
[root@hadoop01 ~]# kafka-console-producer.sh --broker-list \
hadoop01:9092,hadoop02:9092 --topic topic_x < msg.txt

三、创建一个maven项目

### --- 创建一个maven项目:demo-14-kafka-jmx
### --- 导入pom.xml依赖

<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>

四、编程代码

### --- 代码实现:

package com.yanqi.kafka.demo.monitor;

import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

public class JMXMonitorDemo {

public static void main(String[] args) throws IOException, MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {

String jmxServiceURL = "service:jmx:rmi:///jndi/rmi://192.168.1.111:9581/jmxrmi";

JMXServiceURL jmxURL = null;
JMXConnector jmxc = null;
MBeanServerConnection jmxs = null;
ObjectName mbeanObjName = null;
Iterator sampleIter = null;
Set sampleSet = null;

// 创建JMXServiceURL对象,参数是
jmxURL = new JMXServiceURL(jmxServiceURL);
// 建立到指定URL服务器的连接
jmxc = JMXConnectorFactory.connect(jmxURL);
// 返回代表远程MBean服务器的MBeanServerConnection对象
jmxs = jmxc.getMBeanServerConnection();
// 根据传入的字符串,创建ObjectName对象
// mbeanObjName = new ObjectName("kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec");
// mbeanObjName = new ObjectName("kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec,topic=tp_eagle_01");
mbeanObjName = new ObjectName("kafka.server:type=BrokerTopicMetrics,name=BytesInPerSec");
// 获取指定ObjectName对应的MBeans
sampleSet = jmxs.queryMBeans(null, mbeanObjName);
// 迭代器
sampleIter = sampleSet.iterator();

if (sampleSet.isEmpty()) {

} else {
// 如果返回了,则打印信息
while (sampleIter.hasNext()) {
// Used to represent the object name of an MBean and its class name.
// If the MBean is a Dynamic MBean the class name should be retrieved from the MBeanInfo it provides.
// 用于表示MBean的ObjectName和ClassName
ObjectInstance sampleObj = (ObjectInstance) sampleIter.next();
ObjectName objectName = sampleObj.getObjectName();
// 查看指定MBean指定属性的值
String count = jmxs.getAttribute(objectName, "Count").toString();
System.out.println(count);
}
}

// 关闭
jmxc.close();
}

}

五、编译打印

### --- 编译打印

D:\JAVA\jdk1.8.0_231\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=52322:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\JAVA\jdk1.8.0_231\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_231\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_231\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_231\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_231\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_231\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_231\jre\lib\rt.jar;E:\NO.Z.10000——javaproject\NO.Z.00002.Hadoop\kafka_demo\demo-14-kafka-jmx\target\classes;C:\Users\Administrator\.m2\repository\org\apache\kafka\kafka-clients\1.0.2\kafka-clients-1.0.2.jar;C:\Users\Administrator\.m2\repository\org\lz4\lz4-java\1.4\lz4-java-1.4.jar;C:\Users\Administrator\.m2\repository\org\xerial\snappy\snappy-java\1.1.4\snappy-java-1.1.4.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar com.yanqi.kafka.demo.monitor.JMXMonitorDemo
89738656

Process finished with exit code 0
### --- 验证JMS监控到的值是否一致

~~~ 值一致,说明监控到的值和JMS监控到的值是一致的,监控到数据

|NO.Z.00090|——————————|BigDataEnd|——|Hadoop&kafka.V04|——|kafka.v04|编程手段获取监控指标|_kafka_02


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart

                                                                                                                                                   ——W.S.Landor