如何分析在云代工厂上运行的 spring boot webapp 的内存?

pri*_*sad 2 java memory-profiling cloud-foundry spring-boot

我有一个在内部云代工厂空间上运行的 Spring Boot 应用程序。我想监视此 Web 应用程序的堆栈和堆内存,以查找源自代码的 StackOverflow 异常。

分析应用程序的最佳方法是什么。

wil*_*oop 5

1. 使用 JMX 设置运行您的应用

  • 要在本地机器上测试分析,请theapp使用以下 JMX 配置运行,然后转到最后一步“3. VisualVM 配置”

    java \
    -Dcom.sun.management.jmxremote=true \
    -Djava.rmi.server.hostname=localhost \
    -Dcom.sun.management.jmxremote.port=9999 \
    -Dcom.sun.management.jmxremote.rmi.port=9999 \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false \
    -jar theapp.jar ...
    
    Run Code Online (Sandbox Code Playgroud)
  • 要将 JMX 配置传递到theappCF 中,请使用JBP_CONFIG_JMX环境变量(或JAVA_OPTS使用上述参数)manifest.yml

    applications:
      - name: theapp
        buildpack: java_buildpack
        env:
          JBP_CONFIG_JMX: "{enabled: true, port: 9999}"
          # JAVA_OPTS: "-Dcom.sun.management.jmxremote=true -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.rmi.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
    
    Run Code Online (Sandbox Code Playgroud)

2. 创建到应用程序容器的 SSH 隧道

cf ssh -N -T -L 9999:localhost:9999 theapp
Run Code Online (Sandbox Code Playgroud)

3.VisualVM 配置

  • 下载VisualVM
  • 运行 VisualVM: visualvm --jdkhome "$JAVA_HOME"
  • 添加 JMX 连接: localhost:9999


您还可以使用PuTTY创建 SSH 隧道

  • 检索并显示应用程序的 guid:
    java \
    -Dcom.sun.management.jmxremote=true \
    -Djava.rmi.server.hostname=localhost \
    -Dcom.sun.management.jmxremote.port=9999 \
    -Dcom.sun.management.jmxremote.rmi.port=9999 \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false \
    -jar theapp.jar ...
    
    Run Code Online (Sandbox Code Playgroud)
  • 获取 ssh 客户端的一次性密码:

    applications:
      - name: theapp
        buildpack: java_buildpack
        env:
          JBP_CONFIG_JMX: "{enabled: true, port: 9999}"
          # JAVA_OPTS: "-Dcom.sun.management.jmxremote=true -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.rmi.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
    
    Run Code Online (Sandbox Code Playgroud)
  • 创建 PuTTY SSH 连接:

    • 主机名: theapp.your.cloud
    • 港口: 2222
    • 用户名: cf:<app-guid>/<app-instance-index>@ssh.your.cloud
      • 例子: cf:12345678-1234-1234-1234-123456789012/0@ssh.your.cloud
    • 密码: <ssh-code>
      • 例子: PolSkAjEzyKtrUdnA
    • 连接/SSH/隧道/:
      • 源端口: 9999
      • 目的地: 127.0.0.1:9999