springBoot3.2 + jdk21 + GraalVM上手体验
SpringBoot2.x官方已经停止维护了,jdk8这次真的得换了🤣
- 可以参考官方文章进行体验:https://spring.io/blog/2023/09/09/all-together-now-spring-boot-3-2-graalvm-native-images-java-21-and-virtual
- 通过官方快速得到一个基于jdk21的项目:https://start.spring.io/
快速体验(二进制部署)
@RestController@SpringBootApplicationpublicclassDemoApplication{publicstaticvoidmain(String[] args){SpringApplication.run(DemoApplication.class, args);}@GetMapping("/customers")Collection<Customer>customers(){returnSet.of(newCustomer(1,"A"),newCustomer(2,"B"),newCustomer(3,"C"));}recordCustomer(Integer id,String name){}}
启动非常快,秒启动

压测环境内存占用大概
70MB
左右,空闲时在
20MB
左右(由于直接打成二进制文件了,不能再使用jconsole、arthas之类的进行监控了),性能上由于不需要JVM预热,性能启动即巅峰。
$ ab -c50-n10000 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: 028.02144
Processing: 156.74147
Waiting: 045.63145
Total: 1710.46149

快速体验(jar部署)
jar包占用只有19MB,已经不能算是小胖jar了😊

内存占用在压测时大概在200MB左右,空闲时在160MB左右。性能显然也不是启动即巅峰,可以看出其实还是需要进行JVM预热才能达到性能巅峰的
$ ab -c50-n10000 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: 038430.227004
Processing: 01490.481773
Waiting: 01288.761771
Total: 153439.0107011

对比golang
package main
import("encoding/json""flag""fmt""net/http")var port = flag.String("p","8080","please input port")funcmain(){
http.HandleFunc("/customers",func(writer http.ResponseWriter, request *http.Request){
data,_:= json.Marshal(request.URL)
writer.Write(data)})
e :=make(chanerror)gofunc(){
e <- fmt.Errorf("error[%v]", http.ListenAndServe(":"+*port,nil))}()
fmt.Println("http 服务器启动...")
fmt.Println(<-e)}
这里golang没有使用框架,仅使用标准库,所以内存占用较低,仅
10MB
左右,不过即使使用Gin之类的web框架,内存也不会超过20MB
$ ab -c50-n10000 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: 0216.52459
Processing: 0427.92460
Waiting: 0210.52459
Total: 1732.34462

结论
AOT-processed已经相对成熟,效果可以说非常惊艳,解决了
JVM
启动慢、需要预热、内存占用大等问题。
美中不足的是编译速度非常慢,笔者电脑是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在云原生大环境下已经取得了不错的进步的
版权归原作者 每天都要加油呀! 所有, 如有侵权,请联系我们删除。