你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

入门(补充)

在以下部分中,了解如何获取特定技术环境的 Java 自动结构。

Azure 应用服务

有关详细信息,请参阅 Azure 应用服务和 Java 的应用程序监视

Azure Functions

有关详细信息,请参阅使用 Azure Monitor Application Insights 监视 Azure Functions

Azure Spring Apps

有关详细信息,请参阅使用 Azure Spring Apps 中的 Application Insights Java 进程内代理

容器

注意

使用 Spring Boot 本机映像应用程序时,请在 Spring Boot 本机映像 Java 应用程序中使用 Azure Monitor OpenTelemetry Distro/ Application Insights 项目,而不是 Application Insights Java 代理。

Docker 入口点

如果使用的是 exec 形式,请将参数 -javaagent:"path/to/applicationinsights-agent-3.5.2.jar" 添加到参数列表中 "-jar" 参数之前的某个位置,例如:

ENTRYPOINT ["java", "-javaagent:path/to/applicationinsights-agent-3.5.2.jar", "-jar", "<myapp.jar>"]

如果使用的是 shell 形式,请在 -jar 之前的某个位置添加 Java Virtual Machine (JVM) 参数 -javaagent:"path/to/applicationinsights-agent-3.5.2.jar",例如:

ENTRYPOINT java -javaagent:"path/to/applicationinsights-agent-3.5.2.jar" -jar <myapp.jar>

Docker 文件

Dockerfile 示例:

FROM ...

COPY target/*.jar app.jar

COPY agent/applicationinsights-agent-3.5.2.jar applicationinsights-agent-3.5.2.jar 

COPY agent/applicationinsights.json applicationinsights.json

ENV APPLICATIONINSIGHTS_CONNECTION_STRING="CONNECTION-STRING"
        
ENTRYPOINT["java", "-javaagent:applicationinsights-agent-3.5.2.jar", "-jar", "app.jar"]

在此示例中,从 agent 文件夹中复制 applicationinsights-agent-3.5.2.jarapplicationinsights.json 文件(可以选择计算机上的任何文件夹)。 这两个文件必须位于 Docker 容器中的同一文件夹中。

合作伙伴容器映像

如果使用的是无法修改的合作伙伴容器映像,请从外部将 Application Insights Java 代理 jar 装载到容器中。 为容器 JAVA_TOOL_OPTIONS=-javaagent:/path/to/applicationinsights-agent.jar 设置环境变量。

Spring Boot

有关详细信息,请参阅将 Azure Monitor Application Insights 与 Spring Boot 配合使用

Java 应用程序服务器

有关设置 Application Insights Java 代理的详细信息,请参阅 为 Java 启用 Azure Monitor OpenTelemetry。 以下部分提供了在不同应用程序服务器上配置 -javaagent:... JVM 参数时可能有用的详细信息。

Tomcat 8 (Linux)

通过 apt-get 或 yum 安装 Tomcat

如果通过 apt-getyum 安装了 Tomcat,则应有文件 /etc/tomcat8/tomcat8.conf。 将以下行添加到该文件的末尾:

JAVA_OPTS="$JAVA_OPTS -javaagent:path/to/applicationinsights-agent-3.5.2.jar"

通过下载并解压缩相关软件安装的 Tomcat

如果通过从 https://tomcat.apache.org 下载并解压缩相关软件安装了 Tomcat,则应有文件 <tomcat>/bin/catalina.sh。 在同一目录中创建包含以下内容的名为 <tomcat>/bin/setenv.sh 的新文件:

CATALINA_OPTS="$CATALINA_OPTS -javaagent:path/to/applicationinsights-agent-3.5.2.jar"

如果文件 <tomcat>/bin/setenv.sh 已存在,请修改该文件,将 -javaagent:path/to/applicationinsights-agent-3.5.2.jar 添加到 CATALINA_OPTS

Tomcat 8 (Windows)

从命令行运行 Tomcat

找到文件 <tomcat>/bin/catalina.bat。 在同一目录中创建包含以下内容的名为 <tomcat>/bin/setenv.bat 的新文件:

set CATALINA_OPTS=%CATALINA_OPTS% -javaagent:path/to/applicationinsights-agent-3.5.2.jar

引号不是必需的,但如果要包括引号,则正确的位置是:

set "CATALINA_OPTS=%CATALINA_OPTS% -javaagent:path/to/applicationinsights-agent-3.5.2.jar"

如果文件 <tomcat>/bin/setenv.bat 已存在,请修改该文件,将 -javaagent:path/to/applicationinsights-agent-3.5.2.jar 添加到 CATALINA_OPTS

将 Tomcat 作为 Windows 服务运行

找到文件 <tomcat>/bin/tomcat8w.exe。 运行该可执行文件,并将 -javaagent:path/to/applicationinsights-agent-3.5.2.jar 添加到 Java 选项卡下的 Java Options 中。

JBoss 企业应用程序平台 7

在 Red Hat JBoss 企业应用程序平台 (EAP) 7 中,可以设置独立服务器或域服务器。

独立服务器

-javaagent:path/to/applicationinsights-agent-3.5.2.jar 添加到文件 JBOSS_HOME/bin/standalone.conf (Linux) 或 JBOSS_HOME/bin/standalone.conf.bat (Windows) 中的现有 JAVA_OPTS 环境变量:

    JAVA_OPTS="-javaagent:path/to/applicationinsights-agent-3.5.2.jar -Xms1303m -Xmx1303m ..."
    ...

域服务器

-javaagent:path/to/applicationinsights-agent-3.5.2.jar 添加到 JBOSS_HOME/domain/configuration/host.xml 中的现有 jvm-options

...
<jvms>
    <jvm name="default">
        <heap size="64m" max-size="256m"/>
        <jvm-options>
            <option value="-server"/>
            <!--Add Java agent jar file here-->
            <option value="-javaagent:path/to/applicationinsights-agent-3.5.2.jar"/>
            <option value="-XX:MetaspaceSize=96m"/>
            <option value="-XX:MaxMetaspaceSize=256m"/>
        </jvm-options>
    </jvm>
</jvms>
...

如果在单个主机上运行多个托管服务器,则需要将 applicationinsights.agent.id 添加到每个 serversystem-properties

...
<servers>
    <server name="server-one" group="main-server-group">
        <!--Edit system properties for server-one-->
        <system-properties> 
            <property name="applicationinsights.agent.id" value="..."/>
        </system-properties>
    </server>
    <server name="server-two" group="main-server-group">
        <socket-bindings port-offset="150"/>
        <!--Edit system properties for server-two-->
        <system-properties>
            <property name="applicationinsights.agent.id" value="..."/> 
        </system-properties>
    </server>
</servers>
...

指定的 applicationinsights.agent.id 值必须是独一无二的。 使用该值在 Application Insights 目录下创建子目录。 每个 JVM 进程都需要自己的本地 Application Insights 配置和本地 Application Insights 日志文件。 此外,如果向中央收集器报告,则 applicationinsights.properties 文件由多个托管服务器共享,因此需要使用指定的 applicationinsights.agent.id 重写该共享文件中的 agent.id 设置。 如果需要重写每个托管服务器的 agent.rollup.id 设置,则可在服务器的 system-properties 中以类似方式指定 applicationinsights.agent.rollup.id

Jetty 9

将以下行添加到 start.ini

--exec
-javaagent:path/to/applicationinsights-agent-3.5.2.jar

Payara 5

-javaagent:path/to/applicationinsights-agent-3.5.2.jar 添加到 glassfish/domains/domain1/config/domain.xml 中的现有 jvm-options

...
<java-config ...>
    <!--Edit the JVM options here-->
    <jvm-options>
        -javaagent:path/to/applicationinsights-agent-3.5.2.jar>
    </jvm-options>
        ...
</java-config>
...

WebSphere 8

  1. 打开管理控制台。

  2. 转到“服务器”>“WebSphere 应用程序服务器”>“应用程序服务器”。 选择适当的应用程序服务器,然后选择“Java 和进程管理”>“进程定义”>“Java 虚拟机”

  3. Generic JVM arguments 中,添加以下 JVM 参数。

    -javaagent:path/to/applicationinsights-agent-3.5.2.jar
    
  4. 保存并重启应用程序服务器。

OpenLiberty 18

在服务器目录(例如 <openliberty>/usr/servers/defaultServer)中创建新文件 jvm.options,并添加以下行:

-javaagent:path/to/applicationinsights-agent-3.5.2.jar

其他

请参阅应用程序服务器文档,了解如何添加 JVM 参数。