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
300
ぼくたちは Java アプリケーションの起動速度をどこまで縮められるか
bulbulpaul
14
7.7k
あらためて、AWS SDK for Java 入門
bulbulpaul
2
830
AWS SAMを使ったIaC and CI/CD
bulbulpaul
4
3.2k
サーバーレスAPIをKotlinで開発してみよう!
bulbulpaul
0
910
KotlessではじめるServerlessアプリケーション開発
bulbulpaul
0
580
Kotlin v1.3 Features
bulbulpaul
1
200
なぜYahoo!カレンダーはPHPからKotlinへ技術移行を進めるのか
bulbulpaul
3
5.6k
Other Decks in Programming
See All in Programming
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
100
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
testcontainers のススメ
sgash708
1
120
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
190
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
From Translations to Multi Dimension Entities
alexanderschranz
2
130
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
130
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
Security_for_introducing_eBPF
kentatada
0
110
return文におけるstd::moveについて
onihusube
1
1.1k
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
790
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
335
57k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Building Applications with DynamoDB
mza
91
6.1k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Docker and Python
trallard
42
3.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Speed Design
sergeychernyshev
25
670
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Being A Developer After 40
akosma
87
590k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Git: the NoSQL Database
bkeepers
PRO
427
64k
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ͷΈͰेʹ։ൃՄೳͰ͢ɻ • બࢶ͕͋Δ͜ͱ͕ඇৗʹॏཁͰ ݱνʔϜͷঢ়گʹ߹ΘͤͯબͿͱྑ͍Ͱ͢ɻ
͋Γ͕ͱ͏͍͟͝·ͨ͠