- 項目摘要:
本內容是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