问题背景:最近在做应用监控系统,使用jolokia+prometheus+grafana,其中jolokia负责获取JMX端点信息。
一、通过java.lang.management.ManagementFactory
类获取
java.lang.management.ManagementFactory
的成员变量是各种MXBean,我们可以直接获取起ObjectName,如下图:
/**
* String representation of the
* <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
*/
public final static String CLASS_LOADING_MXBEAN_NAME =
"java.lang:type=ClassLoading";
/**
* String representation of the
* <tt>ObjectName</tt> for the {@link CompilationMXBean}.
*/
public final static String COMPILATION_MXBEAN_NAME =
"java.lang:type=Compilation";
/**
* String representation of the
* <tt>ObjectName</tt> for the {@link MemoryMXBean}.
*/
public final static String MEMORY_MXBEAN_NAME =
"java.lang:type=Memory";
/**
* String representation of the
* <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
*/
public final static String OPERATING_SYSTEM_MXBEAN_NAME =
"java.lang:type=OperatingSystem";
/**
* String representation of the
* <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
*/
public final static String RUNTIME_MXBEAN_NAME =
"java.lang:type=Runtime";
/**
* String representation of the
* <tt>ObjectName</tt> for the {@link ThreadMXBean}.
*/
public final static String THREAD_MXBEAN_NAME =
"java.lang:type=Threading";
/**
* The domain name and the type key property in
* the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
* The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
* can be formed by appending this string with
* "<tt>,name=</tt><i>collector's name</i>".
*/
public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
"java.lang:type=GarbageCollector";
/**
* The domain name and the type key property in
* the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
* The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
* can be formed by appending this string with
* "<tt>,name=</tt><i>manager's name</i>".
*/
public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
"java.lang:type=MemoryManager";
/**
* The domain name and the type key property in
* the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
* The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
* can be formed by appending this string with
* <tt>,name=</tt><i>pool's name</i>.
*/
public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
"java.lang:type=MemoryPool";
每个成员变量上面的@link
注释都指向该MXBean,我们想获得具体的度量指标,可以点进去查看其成员变量,里面有get方法,再点进去看,就可以看到其具体属性值。例如查找已使用堆内存:
ManagementFactory
——>MemoryMXBean
——>getHeapMemoryUsage()
——>MemoryUsage.used
附1:堆内存参数的关系,详情见java.lang.management.MemoryUsage
的源码
* Below is a picture showing an example of a memory pool:
*
* <pre>
* +----------------------------------------------+
* + | +
* + | +
* +----------------------------------------------+
*
* |--------|
* init
* |---------------|
* used
* |---------------------------|
* committed
* |----------------------------------------------|
* max
* </pre>
附2:使用jolokia获取JMX端点信息的方式(SpringBoot需要引入jolokia-core使jolokia端点生效):
private void setPromethues(URI uri, String serviceId) throws MalformedObjectNameException, J4pException {
J4pClient j4pClient = new J4pClient(uri.toString() + "/jolokia");
// 获取String
J4pReadRequest request = new J4pReadRequest("java.lang:type=Memory", "HeapMemoryUsage");
J4pReadResponse response = j4pClient.execute(request);
Map<String, Long> values = response.getValue();
logger.info("response values : {}", JSON.toJSONString(values));
Long used = values.get("used");
}
二、通过jconsole工具获取
有时候为了更直观的获取ObjectName或者说验证我们之前获取的数据,我们可以使用jconsole工具获取。终端直接输入jconsole
,选择目标进程,直接连接:
VM概要展示了应用的基本信息,比较直观:
MBean显示了我们监控的具体信息,选择java.lang包,可以查看各种ObjectName,查看属性可以获取具体属性值。