Spring Boot整合New Relic:如何规避Maven下载的newrelic.yml
New Relic 与 Spring Boot 正确集成配置方案
核心问题解决思路
- 阻止Maven解压New Relic依赖时自动生成无许可证密钥的
newrelic.yml - 让自定义配置的
newrelic.yml自动同步到构建目录 - 明确启动命令的正确路径与配置指定方式
1. 修改Maven依赖插件,排除默认配置文件
修改maven-dependency-plugin的配置,取消注释<excludes>节点,排除默认的newrelic.yml,避免它覆盖你的自定义配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>unpack-newrelic</id> <phase>package</phase> <goals> <goal>unpack-dependencies</goal> </goals> <configuration> <includeGroupIds>com.newrelic.agent.java</includeGroupIds> <includeArtifactIds>newrelic-java</includeArtifactIds> <!-- 排除默认的newrelic.yml,避免覆盖自定义配置 --> <excludes>**/newrelic.yml</excludes> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </execution> </executions> </plugin>
2. 配置Maven资源插件,同步自定义配置
添加maven-resources-plugin配置,将你放在src/main/resources/newrelic/newrelic.yml的自定义配置自动复制到target/newrelic目录,确保构建后配置生效:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>copy-newrelic-config</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/newrelic</outputDirectory> <resources> <resource> <directory>src/main/resources/newrelic</directory> <includes> <include>newrelic.yml</include> </includes> <filtering>false</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
3. 正确的应用启动命令
方式一:指定配置文件路径
启动时通过-Dnewrelic.config.file参数明确指定自定义配置文件的位置,避免New Relic加载默认路径的配置:
java -javaagent:./target/newrelic/newrelic.jar -Dnewrelic.config.file=./target/newrelic/newrelic.yml -jar ./target/your-app.jar
如果是部署到服务器,只要保证newrelic.jar和newrelic.yml与应用jar在同一目录,命令可简化为:
java -javaagent:./newrelic.jar -Dnewrelic.config.file=./newrelic.yml -jar ./your-app.jar
方式二:通过环境变量传递许可证密钥(可选)
如果不想维护newrelic.yml文件,也可以直接通过环境变量NEW_RELIC_LICENSE_KEY传递密钥,这样无需修改配置文件:
java -javaagent:./target/newrelic/newrelic.jar -DNEW_RELIC_LICENSE_KEY=your-license-key -Dnewrelic.app.name=your-app-name -jar ./target/your-app.jar
4. 关于单jar部署的说明
由于Java Agent需要独立的jar文件,无法直接打包到Spring Boot的可执行jar中,所以部署时需要将newrelic.jar、newrelic.yml和应用jar放在同一目录下,启动命令使用相对路径即可,无需依赖服务器的固定目录(如/opt)。
内容的提问来源于stack exchange,提问作者John Little
相关产品推荐
相关产品推荐

