SpringBoot3.2 + jdk21 + GraalVM上手體驗
共 15462字,需瀏覽 31分鐘
·
2024-08-16 14:17
來源:juejin.cn/post/7307522662287851558
?? 歡迎加入小哈的星球,你將獲得: 專屬的項目實戰(zhàn) / 1v1 提問 / Java 學(xué)習(xí)路線 / 學(xué)習(xí)打卡 / 每月贈書 / 社群討論
新項目:《從零手?jǐn)]:仿小紅書(微服務(wù)架構(gòu))》 正在持續(xù)爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17..., 點擊查看項目介紹; 《從零手?jǐn)]:前后端分離博客項目(全棧開發(fā))》 2期已完結(jié),演示鏈接:http://116.62.199.48/; 截止目前,累計輸出 53w+ 字,講解圖 2330+ 張,還在持續(xù)爆肝中.. 后續(xù)還會上新更多項目,目標(biāo)是將 Java 領(lǐng)域典型的項目都整一波,如秒殺系統(tǒng), 在線商城, IM 即時通訊,Spring Cloud Alibaba 等等,戳我加入學(xué)習(xí),解鎖全部項目,已有1900+小伙伴加入
![]()
-
快速體驗(二進(jìn)制部署) -
快速體驗(jar部署) -
對比golang -
對比Rust -
結(jié)論
-
可以參考官方文章進(jìn)行體驗:spring.io/blog/2023/0… [1] -
通過官方快速得到一個基于jdk21的項目:start.spring.io/ [2]
快速體驗(二進(jìn)制部署)
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/customers")
Collection<Customer> customers() {
return Set.of(new Customer(1, "A"), new Customer(2, "B"), new Customer(3, "C"));
}
record Customer(Integer id, String name) {
}
}
啟動非常快,秒啟動
壓測環(huán)境內(nèi)存占用大概70MB左右,空閑時在20MB左右(由于直接打成二進(jìn)制文件了,不能再使用jconsole、arthas之類的進(jìn)行監(jiān)控了),性能上由于不需要JVM預(yù)熱,性能啟動即巔峰。
$ ab -c 50 -n 10000 http://localhost:8080/customers
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /customers
Document Length: 61 bytes
Concurrency Level: 50
Time taken for tests: 1.413 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1660000 bytes
HTML transferred: 610000 bytes
Requests per second: 7076.39 [#/sec] (mean)
Time per request: 7.066 [ms] (mean)
Time per request: 0.141 [ms] (mean, across all concurrent requests)
Transfer rate: 1147.15 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 8.0 2 144
Processing: 1 5 6.7 4 147
Waiting: 0 4 5.6 3 145
Total: 1 7 10.4 6 149
快速體驗(jar部署)
jar包占用只有19MB,已經(jīng)不能算是小胖jar了??
內(nèi)存占用在壓測時大概在200MB左右,空閑時在160MB左右。性能顯然也不是啟動即巔峰,可以看出其實還是需要進(jìn)行JVM預(yù)熱才能達(dá)到性能巔峰的
$ ab -c 50 -n 10000 http://localhost:8080/customers
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /customers
Document Length: 61 bytes
Concurrency Level: 50
Time taken for tests: 17.930 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1660000 bytes
HTML transferred: 610000 bytes
Requests per second: 557.72 [#/sec] (mean)
Time per request: 89.651 [ms] (mean)
Time per request: 1.793 [ms] (mean, across all concurrent requests)
Transfer rate: 90.41 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 38 430.2 2 7004
Processing: 0 14 90.4 8 1773
Waiting: 0 12 88.7 6 1771
Total: 1 53 439.0 10 7011
對比golang
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
)
var port = flag.String("p", "8080", "please input port")
func main() {
http.HandleFunc("/customers", func(writer http.ResponseWriter, request *http.Request) {
data, _ := json.Marshal(request.URL)
writer.Write(data)
})
e := make(chan error)
go func() {
e <- fmt.Errorf("error[%v]", http.ListenAndServe(":"+*port, nil))
}()
fmt.Println("http 服務(wù)器啟動...")
fmt.Println(<-e)
}
這里golang沒有使用框架,僅使用標(biāo)準(zhǔn)庫,所以內(nèi)存占用較低,僅10MB左右,不過即使使用Gin之類的web框架,內(nèi)存也不會超過20MB
$ ab -c 50 -n 10000 http://localhost:8080/customers
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /customers
Document Length: 161 bytes
Concurrency Level: 50
Time taken for tests: 1.380 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2790000 bytes
HTML transferred: 1610000 bytes
Requests per second: 7247.68 [#/sec] (mean)
Time per request: 6.899 [ms] (mean)
Time per request: 0.138 [ms] (mean, across all concurrent requests)
Transfer rate: 1974.71 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 16.5 2 459
Processing: 0 4 27.9 2 460
Waiting: 0 2 10.5 2 459
Total: 1 7 32.3 4 462
對比Rust
[dependencies]
actix-web = "4"
use actix_web::{get, App, HttpRequest, HttpResponse, HttpServer, Responder};
#[get("/customers")]
async fn echo(req: HttpRequest) -> impl Responder {
let url = req.uri().to_string();
HttpResponse::Ok().body(url)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(echo)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Actix-web空閑時內(nèi)存占用大概3MB左右,壓測時占用大概6MB左右
$ ab -c 50 -n 10000 http://localhost:8080/customers
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /customers
Document Length: 10 bytes
Concurrency Level: 50
Time taken for tests: 1.091 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 860000 bytes
HTML transferred: 100000 bytes
Requests per second: 9163.48 [#/sec] (mean)
Time per request: 5.456 [ms] (mean)
Time per request: 0.109 [ms] (mean, across all concurrent requests)
Transfer rate: 769.59 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 11.0 2 189
Processing: 0 3 7.0 3 190
Waiting: 0 2 7.0 2 189
Total: 2 5 13.1 4 193
rust雖然有非常厲害的零成本抽象,但作為代價其編譯時間會比較長(在實際項目中真的特別長??)
$ time cargo build
cargo build 213.00s user 23.08s system 258% cpu 1:31.39 total
結(jié)論
AOT-processed已經(jīng)相對成熟,效果可以說非常驚艷,解決了JVM啟動慢、需要預(yù)熱、內(nèi)存占用大等問題。
美中不足的是編譯速度非常慢,筆者電腦是2017款mac book pro編譯花費大概15分鐘左右
Finished generating 'demo' in 14m 33s.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15:45 min
[INFO] Finished at: 2023-12-01T17:00:21+08:00
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15:45 min [INFO] Finished at: 2023-12-01T17:00:21+08:00
[INFO] ------------------------------------------------------------------------
可以看出java在云原生大環(huán)境下已經(jīng)取得了不錯的進(jìn)步的
?? 歡迎加入小哈的星球,你將獲得: 專屬的項目實戰(zhàn) / 1v1 提問 / Java 學(xué)習(xí)路線 / 學(xué)習(xí)打卡 / 每月贈書 / 社群討論
新項目:《從零手?jǐn)]:仿小紅書(微服務(wù)架構(gòu))》 正在持續(xù)爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17..., 點擊查看項目介紹; 《從零手?jǐn)]:前后端分離博客項目(全棧開發(fā))》 2期已完結(jié),演示鏈接:http://116.62.199.48/; 截止目前,累計輸出 53w+ 字,講解圖 2330+ 張,還在持續(xù)爆肝中.. 后續(xù)還會上新更多項目,目標(biāo)是將 Java 領(lǐng)域典型的項目都整一波,如秒殺系統(tǒng), 在線商城, IM 即時通訊,Spring Cloud Alibaba 等等,戳我加入學(xué)習(xí),解鎖全部項目,已有1900+小伙伴加入
![]()
3. 架構(gòu)設(shè)計中如何應(yīng)對接口級故障?
最近面試BAT,整理一份面試資料《Java面試BATJ通關(guān)手冊》,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。
獲取方式:點“在看”,關(guān)注公眾號并回復(fù) Java 領(lǐng)取,更多內(nèi)容陸續(xù)奉上。
PS:因公眾號平臺更改了推送規(guī)則,如果不想錯過內(nèi)容,記得讀完點一下“在看”,加個“星標(biāo)”,這樣每次新文章推送才會第一時間出現(xiàn)在你的訂閱列表里。
點“在看”支持小哈呀,謝謝啦
