- 项目摘要:
本内容是NewRelic与springboot进行整合的一个简单入门案例。
- 项目具体实施:
- 在newrelic官网注册账号(免费期为15-30天):https://newrelic.com,注册完选择APM,选择java,复制生成的key,保存好。
- 进入https://docs.newrelic.com/docs/agents/java-agent?toc=true网址选择一种方案,这里选择gradle方案。
- 新建一个springboot项目,使用gradle。
- 完整build.gradle示例如下:
plugins {
id 'org.springframework.boot' version '2.2.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id "de.undercouch.download" version "3.4.3"
}
group = 'com.yuji'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
task downloadNewrelic(type: Download) {
mkdir 'newrelic'
src 'https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip'
dest file('newrelic')
}
task unzipNewrelic(type: Copy) {
from zipTree(file('newrelic/newrelic-java.zip'))
into rootDir
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile group: 'com.newrelic.agent.java', name: 'newrelic-agent', version: '5.13.0'
compile group: 'com.newrelic.agent.java', name: 'newrelic-api', version: '5.13.0'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
- 刷新这个build.gradle文件,等待相关jar包导入完成后,命令行进入该项目下,执行:
./gradlew downloadNewrelic
./gradlew unzipNewrelic
- 此时项目下多了一个newrelic文件夹,打开newrelic.yml将刚才保存的key复制进去,并且修改 app_name的名称。
- 执行./gradlew build,此时打包自己的springboot项目。接着执行:
java -javaagent:newrelic/newrelic.jar -jar build/libs/yourapp-0.0.1-SNAPSHOT.jar
此时登录https://newrelic.com网站尝试是否可以搜索该应用,则整合完成。
- 将低5步整合到Eclipse中,右键springboot主方法,选择run as,选择run configuration,这里注意,确保选择的是刚才的主方法,然后选择(x)=Arguments选项,在VM arguments中填入如下内容(替换成自己的路径):
对于mac系统:
-Dnewrelic.config.file=yourpath/newrelic.yml
-javaagent:yourpath/newrelic.jar
对于win系统:
-javaagent:yourpath/newrelic/newrelic.jar
如果报错,就将上一句也加进去。此时点击run,会发现同样newrelic.jar也正确加载了。
- 参照该https://docs.newrelic.com/docs/agents/java-agent/custom-instrumentation/java-agent-api-example-program网址编写一个自定义字段发送到newrelic,这里,假设每请求一次就发送一次消息。则我们创建一个包controller包,新建一个类,比如Rest.java,则参照代码如下:
package com.yuji.controller;
import java.util.Random;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
@RestController
public class Rest {
@GetMapping("/hello")
@Trace(dispatcher = true)
public String hello() {
NewRelic.setTransactionName(null, "/store");
String userId = "张三";
NewRelic.setUserName(userId);
NewRelic.addCustomParameter("userId", userId);
NewRelic.incrementCounter("Custom/Promotion");
try {
long millisToSleep = new Random().nextInt(5000);
Thread.sleep(millisToSleep);
NewRelic.recordResponseTimeMetric("Custom/RandomSleep", millisToSleep);
} catch (InterruptedException e) {
NewRelic.noticeError(e, false);
}
return "NewRelic API example servlet";
}
}
- 此时重新开启springboot项目,执行一次http://8080/hello
- 在newrelic网站选择INSIGHTS,输入:select userId FROM Transation,点击Run,结果如下图:
- 此时,springboot和NewRelic基础入门基本完成。其他进阶内容参阅NewRelic官网。另外,Httpcore也可以和NewRelic可以整合,整合过程和当前这种方式类似。
- 参考链接:
- NewRelic官网: https://newrelic.com
- springboot与NewRelic整合: https://www.jianshu.com/p/595636f30d77