【问题标题】:Monitoring a heavily loaded Java application监控负载很重的 Java 应用程序
【发布时间】:2018-01-30 22:09:02
【问题描述】:

我想知道获取应用程序性能指标的标准做法是什么,最好是在 Java 中。目前我们有一个定期安排的任务来收集系统指标。通常,此任务未按时安排,导致指标在该时间不可用,从而导致监控仪表板被破坏[如果是折线图,则会出现间隙]。

通常,当应用程序性能不佳时,我们希望所有指标都可用。但我们观察到,我们无法收集任何指标[因为应用程序非常繁忙]

【问题讨论】:

  • 广泛而有趣的问题。
  • 一种好方法是将instrumentation 添加到您的应用程序中,以便它向收集这些值的系统报告其性能,然后您可以离线分析性能。这是一个持续的过程,因此有助于保持性能历史记录。如前所述,广泛但有趣,是重度使用应用程序的必备品。

标签: java monitoring


【解决方案1】:
  1. 您可以使用名为 top-threads 的工具,可在此处找到:https://bitbucket.org/pjtr/topthreads

    它的作用是为您提供有关目标 JVM 加载的每个类和线程的每个使用详细信息(RAM、CPU 等)。

    这个的用法可以在上面的页面上找到

  2. 您可以使用位于 java lib 目录中 Tools.jar 文件中的 Sun 库将代理加载到目标 VM 中。

加载代理如下所示:

/**
 * Hooks/Attaches to the process's VM with a given PID
 * 
 * @return true if the hook/attach was successful
 */
public boolean hook() {
    try {
        return (vm = VirtualMachine.attach(Long.toString(pid))) != null;
    } catch (AttachNotSupportedException | IOException e) {
        e.printStackTrace();
        return false;
    }
}

/**
 * Loads a thread agent which can debug the running threads and classes of this process
 * @param agent - the location of the agent jar file
 * @param options - the options/arguments to pass into the agents main method
 */
public boolean loadAgent(String agent, String options) {
    try {
        vm.loadAgent(agent, options);
        return true;
    } catch (AgentLoadException | AgentInitializationException | IOException e) {
        e.printStackTrace();
        return false;
    }
}

一个代理主类看起来像这样......

另外,请注意:创建代理 jar 时,您必须在清单文件中指定 Agent-main,其中包含要从中加载代理的 agentmain 方法的类的位置。

public class Agent {
    /**
     * An Object Lock for thread sync's if neccessary */
    public static final Object LOCK = new Object();

   /**
    * Starts the agent with this agent main
    * 
    * @param agentArgs
    *            - the agent args being passed into this method
    * @param inst
    *            - the instrumentation instrument that is passed into this
    *            method
    */
    public static void agentmain(String agentArgs, Instrumentation inst) {
          //Do whatever you want to the target VM here, hacky, but eh, use at your own risk, it is included in java itself...
    }
}

代理清单文件的描述

Manifest-Version: 1.0
Agent-Class: packageNameThatWillBeDifferent.Agent
Created-By: 1.8.0_101 (Oracle Corporation)

【讨论】:

  • 企业应用程序通常对使用代理有严格的限制!
  • 那就试试顶级线程吧!非常好用,如果需要执行方面的帮助可以给我留言
【解决方案2】:

无论这是“企业”应用程序还是在 JVM 中提供服务的小型服务。您需要深入了解此垃圾收集运行时的基本运行状况详细信息。那就是内存池(堆、堆外),最突出的 GC 统计信息。

仅从主机视图收集进程指标(CPU 使用率、驻留集大小 (RSS)、IO)并不能帮助您真正了解 JVM 的功能,也不了解代码中的热点在哪里。

如果您无权访问代码,那么代理可能是获得 JVM 洞察力的唯一机会。否则,您真的应该使用上面提到的一些著名的 Java 指标库来检测 JVM。

在我看来,最广泛使用的 Java 指标库是 Dropwizard Metrics。

虽然我有点偏见,但我还是建议您看看Micrometer 项目。您可以在几行代码中配置一组基本的 JVM 运行时指标,以基本了解 JVM 运行时行为。如果这样做了,您可以通过对它们进行计时来开始检测代码中的热点。 Micrometer 为各种已建立的监控系统提供了大量的指标导出器。 (Prometheus、InfluxDb、Graphite、...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2011-02-28
    • 1970-01-01
    • 2013-08-26
    • 2010-10-15
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多