the state of kotlin support in spring · 2020. 10. 15. · spring support for native executables...

52
Kotlin 1.4 Online Event October 15, 2020 @sdeleuze The State of Kotlin Support in Spring Sébastien Deleuze

Upload: others

Post on 21-Feb-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Kotlin 1.4 Online Event

October 15, 2020@sdeleuze

The State of Kotlin Support in SpringSébastien Deleuze

Page 2: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring is the server-side leader on the JVM

Source: a Picture of Java in 2020, JetBrains

Page 3: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

First class support for Java and Kotlin

Page 4: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

More Spring Boot projects generated with Kotlin each year.

Page 5: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Let’s focus on Kotlin today

Page 6: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Getting started

Page 7: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Start your project on https://start.spring.io

Page 8: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Minimal Spring Boot Kotlin application

Page 9: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Gradle Kotlin DSL

Page 10: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Also available in IDEs

IntelliJ IDEA Ultimate VS code

Page 11: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Follow the tutorial on https://spring.io/guides

Page 12: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring Framework documentation in Kotlin

Page 13: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Choose your style

Page 14: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Choose your web server stack

Do I need asynchronous

or reactive programming?

Spring MVCTomcat

Spring WebFluxwith Coroutines

Netty

No

Yes

Persistence Spring Data

JPAJDBCRedis

Mongo...

Spring Data Reactive

with Coroutines

R2DBCRedis

Mongo...

Persistence

Page 15: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Choose your programming model

Page 16: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Do you prefer annotations?

@RestController@RequestMapping("/api/article")class ArticleController(private val repository: ArticleRepository) {

@GetMapping("/") fun findAll() = repository.findAllByOrderByAddedAtDesc()

@GetMapping("/{slug}") fun findOne(@PathVariable slug: String) = repository.findBySlug(slug) ?: throw ResponseStatusException(NOT_FOUND)

}

Page 17: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Or functional APIs?

@Beanfun route(repository: ArticleRepository) = router { "/api/article".nest { GET("/") { ok().body(repository.findAllByOrderByAddedAtDesc()) } GET("/{slug}") { val slug = it.pathVariable("slug") val article = repository.findBySlug(slug) ?: throw ResponseStatusException(NOT_FOUND) ok().body(article) } }}

Page 18: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring supports both,so up to you.

Page 19: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Coroutines

Page 20: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Allow to go reactive with a great trade-off between imperative and functional programming.

Page 21: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Coroutines are the default way to go reactive in Spring with Kotlin.

Page 22: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

First class Coroutines support● Spring WebFlux● Spring MVC (new in Spring Boot 2.4● Spring Data Reactive● Spring Messaging (RSocket)● Spring Vault

Page 23: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Suspending functionsSpring MVC and WebFlux

@GetMapping("/api/banner")suspend fun suspendingEndpoint(): Banner { delay(10) return Banner("title", "Lorem ipsum")}

Page 24: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

FlowSpring MVC and WebFlux

@GetMapping("/banners")suspend fun flow(): Flow<Banner> = client.get() .uri("/messages") .accept(MediaType.TEXT_EVENT_STREAM) .retrieve() .bodyToFlow<String>() .map { Banner("title", it) }

Page 25: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Multiplatform

Page 26: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Multiplatform

Page 27: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

kotlinx.serialization

Page 28: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

kotlinx.serialization supportNew in Spring Boot 2.4

● More lightweight than Jackson● Designed for Kotlin● Multiplatform serialization● Allows same code for model and validation

across server, frontend and mobile!

implementation("org.springframework.boot:spring-boot-starter-web") { exclude(module = "spring-boot-starter-json")}implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")

Page 29: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Kotlin/JS

Page 30: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Kotlin/WASM has a huge potential

Page 31: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

RSocket

Page 32: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

RSocket

ReactiveStreams

RSocket

ReactiveStreams

ReactiveStreams

Page 33: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

RSocket .Python

.NET

Go

C++

JavaScriptReactive Streams

RSocket

Kotlin

Java

Reactor

Page 34: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

RSocket support in Spring messagingclass MessageHandler(private val builder: RSocketRequester.Builder) {

// ...

suspend fun stream(request: ServerRequest): ServerResponse { val requester = builder .dataMimeType(APPLICATION_CBOR) .connectTcpAndAwait("localhost", 9898) val replies = requester .route("bot.messages") .dataWithType(processor) .retrieveFlow<Message>() val broadcast = requester.route("bot.broadcast").retrieveFlow<Message>() val messages = flowOf(replies, processor.asFlow(), broadcast).flattenMerge() return ok().sse().bodyAndAwait(messages) }}

Page 35: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

rsocket-kotlin

Page 36: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Other key points

Page 37: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

100% of Spring Framework API with null-safety annotations→ no NPE for Spring applications written in Kotlin

Page 38: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

ConfigurationProperties data classes

@ConstructorBinding@ConfigurationProperties("blog")data class BlogProperties(val title: String, val banner: Banner) { data class Banner(val title: String? = null, val content: String)}

Page 39: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring Security Kotlin DSLNew in Spring Security 5.4

override fun configure(http: HttpSecurity) { http { authorizeRequests { authorize("/css/**", permitAll) authorize("/user/**", hasAuthority("ROLE_USER")) } formLogin { loginPage = "/log-in" } }}

Page 40: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring Boot native applicationsWith GraalVM native

Page 41: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring Boot application

EXE

Nativeexecutable

Lightweight container

image

Page 42: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

JVM and native executables offer different trade-offs

Page 43: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Instant startup and cheaper hosting

Spring Boot on Native,1 vCPU, 256M RAM

Spring Boot on JVM,4 vCPU, 1G RAM

Page 44: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring support for native executables

GraalVM fixesand improvements

Incubating support inspring-graalvm-native plugin

(Spring Boot 2 baseline)

First class supportin Spring Boot 3

Today Dec 2020Beta

Spring Boot 3release

Page 45: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Demo

Page 46: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

What’s next?Programmatic configuration for Spring Boot using a Kotlin DSL

Page 47: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring Fu is an incubator for a functional flavor of Spring Boot

KoFu JaFu

Page 48: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

What is the same than Spring Boot?● https://start.spring.io ● Based on Spring Boot infrastructure● Spring configuration for the JVM ecosystem● Dependency management● Starters● Actuators● Standalone executable JAR or container deployment

Page 49: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

What changes?

Conventions and automatic configuration

Annotations-based configuration

Reflection-based infrastructure

Production ready

Explicit declaration

Functional configuration

Lambda-based infrastructure

Incubating

Spring Boot regular flavor Spring Fu flavor

Page 50: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Spring Boot configured with KoFuval app = webApplication { beans { bean<SampleService>() bean<SampleHandler>() } webMvc { port = if (profiles.contains("test")) 8181 else 8080 router { val handler = ref<SampleHandler>() GET("/", handler::hello) GET("/api", handler::json) } converters { string() jackson { indentOutput = true } } }}

fun main() { app.run()}

Page 51: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

Linkshttps://start.spring.iohttps://spring.io/guides/tutorials/spring-boot-kotlin/ https://github.com/spring-projects-experimental/spring-graalvm-nativehttps://github.com/spring-projects-experimental/spring-fu

Page 52: The State of Kotlin Support in Spring · 2020. 10. 15. · Spring support for native executables GraalVM fixes and improvements Incubating support in spring-graalvm-native plugin

@sdeleuze

Thanks!Have a nice Kotlin!