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
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
900
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
1k
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
CSC509 Lecture 09
javiergs
PRO
0
140
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.8k
RubyLSPのマルチバイト文字対応
notfounds
0
120
Jakarta EE meets AI
ivargrimstad
0
240
Functional Event Sourcing using Sekiban
tomohisa
0
100
React への依存を最小にするフロントエンド設計
takonda
6
1.4k
Quine, Polyglot, 良いコード
qnighy
4
650
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
110
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Adopting Sorbet at Scale
ufuk
73
9.1k
A Philosophy of Restraint
colly
203
16k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Scaling GitHub
holman
458
140k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
RailsConf 2023
tenderlove
29
900
Typedesign – Prime Four
hannesfritz
40
2.4k
Six Lessons from altMBA
skipperchong
27
3.5k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
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ͷΈͰेʹ։ൃՄೳͰ͢ɻ • બࢶ͕͋Δ͜ͱ͕ඇৗʹॏཁͰ ݱνʔϜͷঢ়گʹ߹ΘͤͯબͿͱྑ͍Ͱ͢ɻ
͋Γ͕ͱ͏͍͟͝·ͨ͠