Spring Boot部署NewRelic的Java探针记录

本文介绍如何在SpringBoot项目中集成NewRelic Java探针进行性能监控。通过修改pom.xml文件添加依赖、配置插件及打包启动应用程序等步骤实现。适用于使用MacOS、IDEA和Maven构建工具的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、需求

  • 在Spring Boot项目中加入NewRelic的Java探针
  • 环境:Mac OS
  • IDE:IDEA
  • 构建工具:maven

二、pom.xml

pom文件修改如下:

  1. 增加NewRelic Agent依赖
  2. 在spring-boot-maven-plugin中注明main class
  3. 把NewRelic Agent jar打包进最终jar
  4. 在manifest中增加Premain-Class等属性
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

....

  <dependencies>
    <dependency>
      <groupId>com.newrelic.agent.java</groupId>
      <artifactId>newrelic-agent</artifactId>
      <version>3.41.0</version>
      <scope>provided</scope>
    </dependency>
....
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <mainClass>com.xxx.SpringBootNewRelicApplication</mainClass>
          </configuration>
      </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeArtifactIds>newrelic-agent</includeArtifactIds>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Premain-Class>com.newrelic.bootstrap.BootstrapAgent</Premain-Class>
                        <Can-Redefine-Classes>true</Can-Redefine-Classes>
                        <Can-Retransform-Classes>true</Can-Retransform-Classes>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
  </build>

  ....
</project>

三、启动

// 打包
mvn package
// 启动
java -javaagent:/path/to/newrelic/newrelic.jar -jar target/YourApp.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello2mao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值