Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Micronaut で始める Server Side Kotlin
Search
BulBulPaul
November 23, 2019
Programming
5
1.6k
Micronaut で始める Server Side Kotlin
JJUG CCC Dall 2019 での登壇資料です。
BulBulPaul
November 23, 2019
Tweet
Share
More Decks by BulBulPaul
See All by BulBulPaul
AWS Updates - App Dev & AI/ML -
bulbulpaul
0
110
re:Invent re:Cap / AWS Lambda Updates
bulbulpaul
1
290
ぼくたちは Java アプリケーションの起動速度をどこまで縮められるか
bulbulpaul
14
7.6k
あらためて、AWS SDK for Java 入門
bulbulpaul
2
810
AWS SAMを使ったIaC and CI/CD
bulbulpaul
4
3.1k
サーバーレスAPIをKotlinで開発してみよう!
bulbulpaul
0
890
KotlessではじめるServerlessアプリケーション開発
bulbulpaul
0
570
Kotlin v1.3 Features
bulbulpaul
1
200
なぜYahoo!カレンダーはPHPからKotlinへ技術移行を進めるのか
bulbulpaul
3
5.6k
Other Decks in Programming
See All in Programming
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
690
みんなでプロポーザルを書いてみた
yuriko1211
0
280
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
930
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
140
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Macとオーディオ再生 2024/11/02
yusukeito
0
370
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1366
200k
The Cost Of JavaScript in 2023
addyosmani
45
6.8k
Unsuck your backbone
ammeep
668
57k
The Cult of Friendly URLs
andyhume
78
6k
GitHub's CSS Performance
jonrohan
1030
460k
Six Lessons from altMBA
skipperchong
27
3.5k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Rails Girls Zürich Keynote
gr2m
94
13k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Designing for Performance
lara
604
68k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Transcript
Micronaut Ͱ࢝ΊΔ Server Side Kotlin @bulbulpaul #jjug_ccc #ccc_i4 2019/11/23 JJUG
CCC Fall 2019
#jjug #ccc_i4 Yahoo!カレンダー Server Side Engineer @bulbulpaul Fav. Python, Kotlin
Kansai.kt Organizer ポール
None
using
#jjug #ccc_i4 Agenda [͢͜ͱ] • Server Side Kotlin ͷ࠷ۙͷಈ •
ͳͥKotlin, Micronaut ͳͷ͔ • Kotlin, Micronaut ΛͬͯΞϓϦέʔγϣϯΛ࡞Δ [͞ͳ͍͜ͱ] • Kotlinͷࡉ͔ͳݴޠ༷
#jjug #ccc_i4 Disclaimer 2019/11/23 ࣌Ͱͷ࠷৽൛͕ϕʔεͰ͢ɻ • Kotlin : 1.3.60 •
micronaut : 1.2.5
Kotlin
Έͳ͞Μʹ࣭Ͱ͢
Kotlinͬͯ·͔͢ʁ
#jjug #ccc_i4 ੩తܕ͚ͷΦϒδΣΫτࢦϓϩάϥϛϯάݴޠ KotlinOSS, Kotlin Foundation (JetBrains, Googleʣ͕ཧ ࣮ߦڥҎԼͷ4ͭ •
JVM • Android • Native(iOS, MacOS, Android, Windows, Linux, WebAssembly) • JavaScript ࢀߟ: Kotlin Foundation https://kotlinlang.org/foundation/kotlin-foundation.html
#jjug #ccc_i4 Kotlin Β͠͞ͱ Kotlin࣮༻ੑΛॏཁࢹ͍ͯ͠ΔݴޠͰ͢ɻ • ؆ܿ͞ < Մಡੑ •
දݱͷ๛͔͞ < ࠶ར༻ੑ • ಠࣗੑ < ૬ޓӡ༻ੑ • ݈શੑ < ҆શੑ Ҿ༻ݩ: Kotlin Conf 2018 Keynote P21
αʔόʔαΠυؔ࿈ Ͳ͏͍ͬͨঢ়گͳͷ͔
#jjug #ccc_i4 Serverside Frameworks and more…
#jjug #ccc_i4 Server Side Frameworks جຊతʹJavaͷFrameworkͦͷ··͑·͢ɻ େ͖ͳҧ͍ͱͯ͠”KotlinΒ͍͠”ίʔυ͕ ॻ͚Δ͔Ͳ͏͔ɻe.g. NullSafe, DSL
Kotlin࠾༻ࣄྫͱͯ͠ެࣜʹαϙʔτΛ͍ͯ͠Δ SpringɺJetBrainsͷKtor͕ଟ͍ɻ ࠓճͷMicronaut ެࣜʹKotlinαϙʔτʂ
#jjug #ccc_i4 Test Frameworks ͪ͜ΒجຊతʹJUnit͕ͦͷ··͑·͢ɻ Pure KotlinͳςετϥΠϒϥϦͩͱҎԼ͕༗໊ॴ • KotlinTest •
Spek kotlintest: https://github.com/kotlintest/kotlintest Spek: https://www.spekframework.org/
#jjug #ccc_i4 ORM ͪ͜ΒجຊతʹJavaͷ͕ͦͷ··͑·͢ɻ Pure KotlinͳORMExposed, etc.. JavaͷϥΠϒϥϦͰKotlin αϙʔτ͍ͯ͠Δͩͱ •
Doma • JOOQ • etc…
#jjug #ccc_i4 Other tools ։ൃΛ͢Δ্ͰඞཁͳπʔϧྨҰ௨Γἧ͍ͬͯ·͢ɻ Pure KotlinͰͳ͍ϥΠϒϥϦগ͠Ͱ͋Δ͕Kotlinαϙʔτ͕ ૿͖͍͑ͯͯΔɻe.g. Doma, Jooby,
VERT.X KotlinͷϥΠϒϥϦҎԼΛࢀর͢Δͱྑ͍Ͱ͢ɻ Awsome Kotlin https://github.com/KotlinBy/awesome-kotlin Kotlinؔ࿈ͷϥΠϒϥϦٕज़ใͷLinkू https://kotlin.link/
ͳͥࠓճ Micronaut ͷʁ
#jjug #ccc_i4 ࠷ۙͷτϨϯυ • ϞϊϦε͔ΒϚΠΫϩαʔϏε • ίϯςφԽΫϥυωΠςΟϒԽ • k8s, FaaS,
PaaS, etc.. • ͦΕʹΑΔӡ༻։ൃʹ ඞཁʹͳͬͯ͘Δٕज़มΘΔ
#jjug #ccc_i4 Kotlin x SpringͰͷྫ • SpringBoot x Kotlin •
؆୯ʹॻ͚ͯศརʂ • ެࣜʹKotlin Support͍ͯ͠Δ҆͠શʹॻ͖͍͢ • ҰํͰFWͷಛੑ্ͷ՝ͱͯ͠ • ىಈ͕͍ʢىಈ࣌ͷScanDI, AOPͷղܾʣ • ϝϞϦͷϑοτϓϦϯτ͕େ͖͘ͳΓ͕ͪ
#jjug #ccc_i4 Kotlin x SpringͰͷྫ • SpringBoot x Kotlin •
؆୯ʹॻ͚ͯศརʂ • ެࣜʹKotlin Support͍ͯ͠Δ҆͠શʹॻ͖͍͢ • ҰํͰFWͷಛੑ্ͷ՝ͱͯ͠ • ىಈ͕͍ʢىಈ࣌ͷScanDI, AOPͷղܾʣ • ϝϞϦͷϑοτϓϦϯτ͕େ͖͘ͳΓ͕ͪ
#jjug #ccc_i4 Kotlin x SpringͰͷྫ • ىಈ͕͍ • Φʔτεέʔϧ࣌ʹىಈ͕͍ͷࠔΔ •
ͱ͍͑DI, AOPศར͔ͩΒ͍͍ͨ • ศར͚ͩͲখ͞ͳػೳ࣌ʹFaaSҠͣ͠Β͍ • ϝϞϦͷϑοτϓϦϯτ͕େ͖͘ͳΓ͕ͪ • ݸʑʹׂΓͯΔϦιʔε͕େ͖͘ͳΔ
͋Δఔىಈૣͯ͘ DI, AOP͑ͯ KotlinͰ࣮༻ϨϕϧͷFW?
Micronaut
#jjug #ccc_i4 Ҿ༻: https://micronaut.io/
#jjug #ccc_i4 Micronaut JVMϕʔεͷϑϧελοΫͳFrameworkʢҎԼ, FWʣ OSS, ϥΠηϯε(Apache License 2.0) ։ൃݩObject
Computing(OCI) OCIͷGrails։ൃνʔϜ͕։ൃͨ͠৽͍͠FW ϚΠΫϩαʔϏεͳΞϓϦέʔγϣϯΛૣ͘ ༰қʹ։ൃ͘͢͢͠ΔFW
#jjug #ccc_i4 Micronaut ͷಛ • ίϯύΠϧ࣌ͷDI, AOP →ىಈ࣌ͷεΩϟϯղܾෆཁͳҝɺىಈ͕ૣ͍ • Reactive
ProgramingͷϑΝʔετΫϥεαϙʔτ(Reactor, RxJava) • GraalVMͰͷωΠςΟϒίϯύΠϧαϙʔτ • GraphQL,gRPC,Swagger/OpenAPI,Security,kafka,flyway,etc.. • CLI͕ఏڙ͞Ε͍ͯͯɺ৽ن࡞࣌CLIΛ͏ sdk man, homebrew ͰΠϯετʔϧՄೳ
#jjug #ccc_i4 APIͷίʔυྫ @Controller("/hello") class HelloController { @Get("/") fun index():
String { return "Hello World"; } } ࢀߟݩ: https://micronaut.io/
#jjug #ccc_i4 DIͷίʔυྫ interface Engine { val cylinders: Int fun
start(): String } @Singleton class V8Engine : Engine { override var cylinders = 8 override fun start(): String { return "Starting V8" } } Ҿ༻: https://docs.micronaut.io/ JSR 303ͷAnnotationΛαϙʔτ @Singleton class Vehicle( private val engine: Engine) { fun start(): String { return engine.start() } }
#jjug #ccc_i4 ςετίʔυྫʢSpekʣ Ҿ༻: https://docs.micronaut.io/ object HelloControllerSpec : Spek({ describe("HelloController
Suite") { var embeddedServer: EmbeddedServer = ApplicationContext.run(EmbeddedServer::class.java) var client: HttpClient = HttpClient.create(embeddedServer.url) it("test /hello responds Hello World") { var rsp: String = client.toBlocking().retrieve("/hello") assertEquals("Hello World", rsp) } afterGroup { client.close() embeddedServer.close() } } })
First Step. Install & Create App and IDE Setting
#jjug #ccc_i4 Install Micronaut # sdk man $ sdk install
micronaut # homebrew $ brew install micronaut
#jjug #ccc_i4 Create project # mn create-app {pkgname.appname} —lang kotlin
$ mn create-app \ com.merrylab.example.jjugccc2019\ —lang kotlin | Generating Kotlin project... | Application created at /Users/bulbulpaul/ dev/example-code/jjugccc2019
#jjug #ccc_i4 IDE Settings • Project ΛImport Preferences -> Build
-> Compiler -> Annotation Processors • “Enable annotation processing” Λ༗ޮԽ
͜Μͳײ͡ͰMicronautͱ KotlinͰ։ൃͷ४උ͕ Ͱ͖·͢
Ͱ࣮ࡍʹ ؆୯ͳAPIΛ ࡞ͬͯΈ·͠ΐ͏
Demo
Ͱ…
ͬͱ KotlinΒ͘͠ ॻ͚ΔͷͰʁ
Micronaut Ͱ એݴతʹॻ͚ͳ͍͔ͳʁ
ͨͱ͑… RoutingΛ Annotation͔Β DSLͱ͔
Micronaut with Ktor
#jjug #ccc_i4 Ktor • JetBrainsͷͱͯܰྔͳϑϨʔϜϫʔΫ • DI, ϩΪϯά, ςϯϓϨʔτΤϯδϯ, ORMແ͍
• ෦తʹ Netty, Jetty, Tomcat Λಈ͔ͤΔ • ಛతͳΈͱͯ͠ pipeline • Kotlin ͷ Coroutine ʹରԠ
#jjug #ccc_i4 Ktor ͷDSL ͳRouting routing { get("/") { call.respondText("Hello
World!") } get("/demo") { call.respondText("HELLO WORLD!") } } Ҿ༻ݩ: https://ktor.io/
#jjug #ccc_i4 routing { get("/") { call.respondText("Hello World!") } get("/demo")
{ call.respondText("HELLO WORLD!") } } Ktor ͷDSL ͳRouting Ҿ༻ݩ: https://ktor.io/ એݴతʹ͔͚͔ͯΓ͍͢ 3PVUJOH͔Β࣮Λ͍͍͢
#jjug #ccc_i4 Micronaut x Ktor • MicronautΈࠐΈαʔόʔͱͯ͠ NettyͷΞϓϦέʔγϣϯαʔόʔΛ͏ • ͦͷؒʹKtorͷΈΛೖΕͯಈ͔͢
• ݱࡏv1.0.0-M2
#jjug #ccc_i4 ಈ࡞Πϝʔδ • MicronautͷΈࠐΈαʔόʔͷ෦ͷؒʹ KtorΛೖΕࠐΉܗ
#jjug #ccc_i4 ಈ࡞Πϝʔδ • MicronautͷΈࠐΈαʔόʔͷ෦ͷؒʹ KtorΛೖΕࠐΉܗ
#jjug #ccc_i4 ಈ࡞Πϝʔδ • MicronautͷΈࠐΈαʔόʔͷ෦ͷؒʹ KtorΛೖΕࠐΉܗ ϦΫΤετͷϥΠϑαΠΫϧ ,UPS͕ѻ͏
Second Step. Apply Ktor support
#jjug #ccc_i4 micronaut-ktor ΛՃ // apply Kotlin Runtime Support implementation("io.micronaut.kotlin:micronaut-
kotlin-runtime:1.0.0.M2") implementation("io.micronaut.kotlin:micronaut- ktor:1.0.0.M2") val ktorVersion: String by project implementation("io.ktor:ktor-server-netty: $ktorVersion") implementation("io.ktor:ktor-jackson: $ktorVersion") Ҿ༻ݩ: https://ktor.io/
#jjug #ccc_i4 Ktor ͷDSL ͳRouting @Singleton class Route(private val eventListService:
EventListService) : KtorRoutingBuilder( { get("/") { call.respond("Hello. JJUG CCC Fall 2019!") } get("/eventlist") { val eventList = eventListService.eventList() call.respond(ApiRespoonse(Status.SUCESS, eventList)) } })
Demo
·ͱΊ
#jjug #ccc_i4 ·ͱΊ • ͦΕͧΕͷϝϦοτΛ׆͔͠ͳ͕Β Server Side KotlinΛ͢Δ͜ͱ͕Ͱ͖·͢ɻ • ͪΖΜΈࠐΈαʔόʔͷKtorΛΘͣʹ
MicronautͷΈͰेʹ։ൃՄೳͰ͢ɻ • બࢶ͕͋Δ͜ͱ͕ඇৗʹॏཁͰ ݱνʔϜͷঢ়گʹ߹ΘͤͯબͿͱྑ͍Ͱ͢ɻ
͋Γ͕ͱ͏͍͟͝·ͨ͠