Spring Boot部署NewRelic的Java探针记录

一、需求

  • 在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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容