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.5k
あらためて、AWS SDK for Java 入門
bulbulpaul
2
790
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
macOS でできる リアルタイム動画像処理
biacco42
4
1.6k
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.2k
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
240
Outline View in SwiftUI
1024jp
1
100
Honoの来た道とこれから
yusukebe
19
3k
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
JaSST 24 九州:ワークショップ(は除く)実践!マインドマップを活用したソフトウェアテスト+活用事例
satohiroyuki
0
260
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
Piniaの現状と今後
waka292
5
1.4k
Vue.js学習の振り返り
hiro_xre
2
130
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
7
430
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
590
Featured
See All Featured
Happy Clients
brianwarren
97
6.7k
How to Ace a Technical Interview
jacobian
275
23k
KATA
mclloyd
29
13k
The Language of Interfaces
destraynor
154
24k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
Designing for Performance
lara
604
68k
Writing Fast Ruby
sferik
626
61k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Typedesign – Prime Four
hannesfritz
39
2.4k
Designing Experiences People Love
moore
138
23k
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ͷΈͰेʹ։ൃՄೳͰ͢ɻ • બࢶ͕͋Δ͜ͱ͕ඇৗʹॏཁͰ ݱνʔϜͷঢ়گʹ߹ΘͤͯબͿͱྑ͍Ͱ͢ɻ
͋Γ͕ͱ͏͍͟͝·ͨ͠