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
800
AWS SAMを使ったIaC and CI/CD
bulbulpaul
4
3.1k
サーバーレスAPIをKotlinで開発してみよう!
bulbulpaul
0
890
KotlessではじめるServerlessアプリケーション開発
bulbulpaul
0
560
Kotlin v1.3 Features
bulbulpaul
1
200
なぜYahoo!カレンダーはPHPからKotlinへ技術移行を進めるのか
bulbulpaul
3
5.6k
Other Decks in Programming
See All in Programming
Quine, Polyglot, 良いコード
qnighy
4
610
gopls を改造したら開発生産性が高まった
satorunooshie
8
270
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
380
生成 AI を活用した toitta 切片分類機能の裏側 / Inside toitta's AI-Based Factoid Clustering
pokutuna
0
700
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
160
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
290
イベント駆動で成長して委員会
happymana
1
250
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
480
Realtime API 入門
riofujimon
0
130
Ethereum_.pdf
nekomatu
0
350
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
95
5.2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
15
2k
The Language of Interfaces
destraynor
154
24k
Speed Design
sergeychernyshev
24
580
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Become a Pro
speakerdeck
PRO
25
5k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Designing Experiences People Love
moore
138
23k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Why Our Code Smells
bkeepers
PRO
334
57k
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ͷΈͰेʹ։ൃՄೳͰ͢ɻ • બࢶ͕͋Δ͜ͱ͕ඇৗʹॏཁͰ ݱνʔϜͷঢ়گʹ߹ΘͤͯબͿͱྑ͍Ͱ͢ɻ
͋Γ͕ͱ͏͍͟͝·ͨ͠