0% found this document useful (0 votes)
22 views46 pages

Cadec2023 Spring

The document discusses Spring Boot 3 and Spring Framework 6. It covers migration, native compilation which can compile Spring Boot apps into standalone executables for faster startup times and less memory usage. Observability is also mentioned. Native compilation times have improved since last year but still not fast enough for a TDD loop.

Uploaded by

alqodra.ae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views46 pages

Cadec2023 Spring

The document discusses Spring Boot 3 and Spring Framework 6. It covers migration, native compilation which can compile Spring Boot apps into standalone executables for faster startup times and less memory usage. Observability is also mentioned. Native compilation times have improved since last year but still not fast enough for a TDD loop.

Uploaded by

alqodra.ae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

SPRING BOOT 3

SPRING FRAMEWORK 6

MAGNUS LARSSON

CADEC 2023.01.19 & 2023.01.25 |


CALLISTAENTERPRISE.SE
AGENDA

• Overview
• Migration
• Native Compile
• Observability
• Summary
OVERVIEW

• The evolution of Java

New Requirements
• Faster startup
- Shorten warmup
APP APP
APP APP APP APP • Less memory #1 #2
#1 #2 #3 #4 JVM JVM
• Scalability APP
JAVA EE #4
- Scale to zero
APP
JVM
JAVA VM • Observability #3
JVM

Application Servers Distributed Systems

2000
2023
OVERVIEW Emerging OpenJDK projects:
- CRaC, Amber, Valhalla, Leyden, and Panama

• The evolution of Java

APP APP APP APP


APP APP
#1 #2 #3 #4
#1 #2
JAVA EE
JVM JVM
APP
JAVA VM Java SE 9 Java SE 10 OpenJDK Graal VM Java SE 19
#4
APP
- Project Loom, JVM
- Modules - Docker - Native compile #3
Virtual Threads JVM
(preview)

2017 2018 2019 2022


OVERVIEW

• The evolution of Spring

APP APP APP APP


#1 #2 #3 #4 APP APP
#1 #2
JAVA EE
JVM JVM
APP
JAVA VM #4
Spring Boot 1 Spring Boot 2 Spring Boot 3 APP
JVM
#3
Spring Framework 4 Spring Framework 5 Spring Framework 6
JVM
• Convention over • Reactive programming • Native Compile
Configuration model • Observability THIS PRESENTATION
• Make JAR nor WAR • Production features • Virtual Threads
• Dependency Mgmt, actuators
NEXT PRESENTATION
starters and bom health, monitoring, metrics

2014 2018 2022


AGENDA

• Overview
• Migration
• Native Compile
• Observability
• Summary
MIGRATION

• Java 17 baseline
• Jakarta EE: Package rename: javax à jakarta
• E.g. JPA: javax.persistence.* à jakarta.persistence.*

• Deprecated code in 2.x removed


tasks.withType(JavaCompile) {
options.compilerArgs += ['-Xlint:deprecation']

• Breaking changes etc


• Spring Boot 3.0 Migration Guide
• Spring Security 6.0 Migration Guide

• Importance of end-to-end black-box tests


• Run them before and after the migration!
AGENDA

• Overview
• Migration
• Native Compile
• Observability
• Summary
NATIVE COMPILE For details on GraalVM Native
Image, see this presentation
• Problem definition from Cadec 2021 - GraalVM Native Image
NATIVE COMPILE

• Concerns from Cadec 2021


NATIVE COMPILE

• Outcome from Cadec 2021

• Is it better now?
NATIVE COMPILE

• With Spring Boot 3 and Spring Framework 6


- Compile Spring Boot applications into standalone executables, called a native image
- Uses GraalVM native-image compiler
» New build module Spring AOT
» Supersedes Spring Native

• Benefits
- Shorter startup times
- No warm-up required
- Less memory required
- Fit for scaling up and down
» Even to zero
NATIVE COMPILE

• Spring AOT
- Creates and inspects an ApplicationContext
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(ProductServiceApplication.class, args);

- Closed world assumption


» Classpath fixed and Spring Beans are defined at build time
» Minimize memory footprint
- Generates start-up code Replaces the slow
» Creates a static ApplicationContext reflection based startup
» Programmatic registration of Spring Beans
NATIVE COMPILE

• Spring AOT
- Generates native configuration

• Recall from Cadec 2021:


- GraalVM native-image compiler transforms Java bytecode to an executable image
- Can’t figure out dynamic behavior
▸E.g. use of reflection, dynamic proxies, and local resources
- Described in a native configuration
NATIVE COMPILE

• Spring AOT
- Generates native configuration
- Sample of generated native configuration

- If Spring AOT fails, we can add native hints


» E.g. JSON mapping with Jackson
@RegisterReflectionForBinding({Event.class, Product.class})

» Sample error message:


Error: No serializer found for class se.magnus.util.event.Event
This appears to be a native image, in which case you may need to configure reflection
NATIVE COMPILE

https://spring.io/blog/2021/12/09/new-aot-engine-brings-spring-native-to-the-next-level
NATIVE COMPILE

• More on Spring AOT

- AOT tests
» Builds a native image and runs tests inside it
./gradlew nativeTest
» Detects missing Spring Beans and Reflection metadata
» Best to run in a CI/CD build pipeline

- Use AOT start-up code with Java VM (a.k.a AOT mode)


» Shorten startup time in Java VM with ≈20%
java -Dspring.aot.enabled=true -jar app.jar

» Log output
... Starting AOT-processed ProductServiceApplication using Java 17.0.5 …
NATIVE COMPILE

• Stability over time


- Spring ecosystem - 3PP libraries
» Spring AOT smoke tests » GraalVM reachability metadata
NATIVE COMPILE

• How?
- Add GraalVM’s build plugin:
plugins {
id 'org.graalvm.buildtools.native' version '0.9.18'

- Build a native image: ./gradlew nativeCompile


» Requires GraalVM JDK and native-image compiler
» OS & HW specific, e.g. macOS and ARM64

- Build Docker image: ./gradlew bootBuildImage


» HW specific, e.g. Intel x86_64

• Ongoing work
- GraalVM - Cross-compilation support
- Paketo buildpacks - Add support for ARM64
- Paketo buildpacks - 2023 Roadmap
- Callista blog post - Docker images on ARM64
TEST: NATIVE COMPILE

• System landscape from the 2ed of my book


- Migrated to Spring Boot 3

Product MongoDB
[microservice]

Product Composite --- Recommendation MongoDB


[microservice] [microservice]

Review
The microservice landscape [microservice]
MySQL
[System boundary]
TEST: NATIVE COMPILE TIMES

• Compile times with ./gradlew nativeCompile


- Minimal Spring Boot app
Finished generating 'demo' in 32,3s.
[native-image-plugin] Native Image written to: /Users/magnus/

- The Product service from the test landscape


Finished generating 'product-service' in 1m 41s.
[native-image-plugin] Native Image written to: /Users/magnus/

- Not fast enough for a TDD – loop, but sufficient for a CI/CD build pipeline

- But significantly better than 2021


TEST: STARTUP TIMES

• Java VM microservices
- Started ProductServiceApplication in 4.988 seconds
- Started ProductCompositeServiceApplication in 5.495 seconds
- Started ReviewServiceApplication in 5.442 seconds
- Started RecommendationServiceApplication in 4.886 seconds

Native image app


• Native image microservices starts up 25 times
faster than JVM app
- Started ProductCompositeServiceApplication in 0.148 seconds
- Started RecommendationServiceApplication in 0.198 seconds
- Started ProductServiceApplication in 0.184 seconds
- Started ReviewServiceApplication in 0.229 seconds
TEST: MEMORY USAGE AFTER STARTUP

• Java VM microservices
- review-1 239.2MiB
- product-composite-1 216.5MiB
- product-1 212.6MiB
- recommendation-1 215.5MiB Native image app
requires less memory
to startup.
• Native image microservices
- product-1 78.53MiB But what happens over time?
- recommendation-1 78.55MiB
- product-composite-1 55.84MiB
- review-1 70.14MiB
TEST: RESOURCE USAGE OVER TIME

• Test scope

getProduct

Product MongoDB
[microservice]

getCompositeProduct getRecommendations

Product Composite --- Recommendation MongoDB


[microservice] [microservice]

getReviews

Review
The microservice landscape [microservice]
MySQL
[System boundary]
TEST: RESOURCE USAGE OVER TIME

• Test setup

• 10 000 users
• 2-3 sec getProduct
Product MongoDB
sleep time [microservice]
• ≈ 4000 req/s Gatling
Heap sizes tested 10 000 000
• 2 min warmup • 512 MB products
• 10 min test run • 256 MB
• ≈ 2 160 000 requests

The microservice landscape


[System boundary]
TEST: RESOURCE USAGE OVER TIME

• Test results
Test case No of calls RSS mem CPU time Threads 50 % 95 %
(MB) (min:sec) (ms) (ms)
Native, 256 MB 2 159 056 206 20:54 42 2 11
JVM, 256 MB 2 162 800 235 15:43 54 2 5
Native, 512 MB 2 158 443 220 20:23 42 2 9
JVM, 512 MB 2 163 090 347 15:52 54 2 5
NATIVE COMPILE

• Reiterate the concerns from Cadec 2021

• Simply add GraalVM’s build plugin

• Experimental Spring Native


replaced by Spring AOT
• Tests can run in AOT mode
• In general, much less of a problem
• When needed, use Spring annotations
• Worst case, use
GraalVM’s Tracing Agent
• Spring smoke test project
• GraalVM reachability metadata project • Native compile in a minute or two
NATIVE COMPILE

• Summary
- Concerns from Cadec 2021 mitigated with Spring Boot 3

- Startup: Native 25 times faster than JVM


- Memory: Native beats JVM
- CPU: JVM Hotspot beats native

- Try it out, if start-up time is important!


AGENDA

• Overview
• Migration
• Native Compile
• Observability
• Summary
?
OBSERVABILITY

• Observability = Logging + Tracing + Metrics

APP APP
#1 #2
JVM JVM
APP
#4
APP
#3 JVM
JVM
OBSERVABILITY
APP APP APP
#1 #2 #3
• Observability = Logging + Tracing + Metrics WHAT HAPPENED?
OBSERVABILITY
APP APP APP
#1 #2 #3
• Observability = Logging + Tracing + Metrics PROCESSING TIME?
OBSERVABILITY
APP APP APP
#1 #2 #3
• Observability = Logging + Tracing + Metrics RESOURCE USAGE?
OBSERVABILITY

• Observability in Spring Framework 6.0


- Logs and Metrics already supported in 5.0
- Tracing: New module, Micrometer Tracing
» Based on spring-cloud-sleuth
» Traces are reported as a trace tree of spans based on OpenTelemetry
» Contexts based on W3C Trace Context
OBSERVABILITY

• Tracing in Spring Framework 6.0


- Built-in support
» Creates traces for incoming requests, if missing
» Propagates to outgoing requests
» Supports both synchronous and asynchronous requests
Bridge Observability API
» Propagates to logs
to OpenTelemetry
- Dependencies

Report to a Zipkin
» Support for alternative Tracer Implementations compatible tracer

- No auto propagation (yet) for reactive libraries, e.g. Spring WebFlux


» Spring Boot 3 Webflux project missing traceId and spanId in logs
» Context Propagation Library
OBSERVABILITY

• Tracing in Spring Framework 6.0


- Programmatically
» Spring abstraction Observation
» Custom spans and contexts can be created

CONTEXT KEY VALUE


CONTEXT NAME
DEMO: OBSERVABILITY

getProduct

Product MongoDB
[microservice]

getCompositeProduct getReviews

Product Composite --- Review MySQL


[microservice] [microservice]

getRecommendations

Recommendation
The microservice landscape [microservice]
MongoDB
[System boundary]
DEMO: OBSERVABILITY

getCompositeProduct getReviews

Product Composite Review MySQL


[microservice] [microservice]
Reviews
createCompositeProduct metrics [topic] metrics
Client

[topic] The Grafana stack & Prometheus


is not the only alternative; see
CNCF Landscape for Observability!
[tracing] [logs] [metrics] (covering 145 products)

The microservice landscape


[System boundary]
DEMO: OBSERVABILITY

LOGS

TRACES

METRICS
DEMO: OBSERVABILITY

CONTEXT NAME
CONTEXT KEY VALUE
DEMO: OBSERVABILITY
AGENDA

• Overview
• Migration
• Native Compile
• Observability
• Summary
SUMMARY

• With Spring Boot 3, a new foundation is in place


- Expect a lot of improvements to come over the following years…

• Migration
- Upgrade to Java 17 and jakarta package names
- Remove deprecated code

• Native Compile
- Use if start-up time is important
- Test and build native images in CI/CD build pipeline
- Reduce startup times running Java VM in AOT-mode

• Observability
- Built-in auto-configuration for tracing
- One interface, Observation, to abstract them all
- One dashboard to observe them all
QUESTIONS?

LEGACY WARNING
BASED ON
[email protected]
SPRING BOOT 2
MAGNUSLARSSONCALLISTA
QUESTIONS?

3.0

Mid-year 2023
(hopefully…)

THIRD
[email protected]

MAGNUSLARSSONCALLISTA

You might also like