Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Micronaut で始める Server Side Kotlin

BulBulPaul
November 23, 2019

Micronaut で始める Server Side Kotlin

JJUG CCC Dall 2019 での登壇資料です。

BulBulPaul

November 23, 2019
Tweet

More Decks by BulBulPaul

Other Decks in Programming

Transcript

  1. #jjug #ccc_i4 Agenda [࿩͢͜ͱ] • Server Side Kotlin ͷ࠷ۙͷಈ޲ •

    ͳͥKotlin, Micronaut ͳͷ͔ • Kotlin, Micronaut Λ࢖ͬͯΞϓϦέʔγϣϯΛ࡞Δ [࿩͞ͳ͍͜ͱ] • Kotlinͷࡉ͔ͳݴޠ࢓༷
  2. #jjug #ccc_i4 ੩తܕ෇͚ͷΦϒδΣΫτࢦ޲ϓϩάϥϛϯάݴޠ Kotlin͸OSS, Kotlin Foundation (JetBrains, Googleʣ͕؅ཧ ࣮ߦ؀ڥ͸ҎԼͷ4ͭ •

    JVM • Android • Native(iOS, MacOS, Android, Windows, Linux, WebAssembly) • JavaScript ࢀߟ: Kotlin Foundation https://kotlinlang.org/foundation/kotlin-foundation.html
  3. #jjug #ccc_i4 Kotlin Β͠͞ͱ͸ Kotlin͸࣮༻ੑΛॏཁࢹ͍ͯ͠ΔݴޠͰ͢ɻ • ؆ܿ͞ < Մಡੑ •

    දݱͷ๛͔͞ < ࠶ར༻ੑ • ಠࣗੑ < ૬ޓӡ༻ੑ • ݈શੑ < ҆શੑ Ҿ༻ݩ: Kotlin Conf 2018 Keynote P21
  4. #jjug #ccc_i4 Server Side Frameworks جຊతʹJavaͷFramework͸ͦͷ··࢖͑·͢ɻ େ͖ͳҧ͍ͱͯ͠͸”KotlinΒ͍͠”ίʔυ͕
 ॻ͚Δ͔Ͳ͏͔ɻe.g. NullSafe, DSL౳

    Kotlin࠾༻ࣄྫͱͯ͠͸ެࣜʹαϙʔτΛ͍ͯ͠Δ
 SpringɺJetBrains੡ͷKtor͕ଟ͍ɻ
 ࠓճͷMicronaut ΋ެࣜʹKotlinαϙʔτʂ
  5. #jjug #ccc_i4 Test Frameworks ͪ͜Β΋جຊతʹJUnit౳͕ͦͷ··࢖͑·͢ɻ Pure KotlinͳςετϥΠϒϥϦͩͱҎԼ͕༗໊ॴ • KotlinTest •

    Spek kotlintest: https://github.com/kotlintest/kotlintest Spek: https://www.spekframework.org/
  6. #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/
  7. #jjug #ccc_i4 ࠷ۙͷτϨϯυ • ϞϊϦε͔ΒϚΠΫϩαʔϏε • ίϯςφԽ΍Ϋϥ΢υωΠςΟϒԽ • k8s, FaaS,

    PaaS, etc.. • ͦΕʹΑΔӡ༻΍։ൃʹ
 ඞཁʹͳͬͯ͘Δٕज़΋มΘΔ
  8. #jjug #ccc_i4 Kotlin x SpringͰͷྫ • SpringBoot x Kotlin •

    ؆୯ʹॻ͚ͯศརʂ • ެࣜʹKotlin Support΋͍ͯ͠Δ҆͠શʹॻ͖΍͍͢ • ҰํͰFWͷಛੑ্ͷ՝୊ͱͯ͠ • ىಈ͕஗͍ʢىಈ࣌ͷScan΍DI, AOPͷղܾʣ • ϝϞϦͷϑοτϓϦϯτ͕େ͖͘ͳΓ͕ͪ
  9. #jjug #ccc_i4 Kotlin x SpringͰͷྫ • SpringBoot x Kotlin •

    ؆୯ʹॻ͚ͯศརʂ • ެࣜʹKotlin Support΋͍ͯ͠Δ҆͠શʹॻ͖΍͍͢ • ҰํͰFWͷಛੑ্ͷ՝୊ͱͯ͠ • ىಈ͕஗͍ʢىಈ࣌ͷScan΍DI, AOPͷղܾʣ • ϝϞϦͷϑοτϓϦϯτ͕େ͖͘ͳΓ͕ͪ
  10. #jjug #ccc_i4 Kotlin x SpringͰͷྫ • ىಈ͕஗͍ • Φʔτεέʔϧ࣌౳ʹ΋ىಈ͕஗͍ͷ͸ࠔΔ •

    ͱ͸͍͑DI, AOP΋ศར͔ͩΒ࢖͍͍ͨ • ศར͚ͩͲখ͞ͳػೳ࣌ʹFaaS౳΁Ҡ؅ͣ͠Β͍ • ϝϞϦͷϑοτϓϦϯτ͕େ͖͘ͳΓ͕ͪ • ݸʑʹׂΓ౰ͯΔϦιʔε͕େ͖͘ͳΔ
  11. #jjug #ccc_i4 Micronaut JVMϕʔεͷϑϧελοΫͳFrameworkʢҎԼ, FWʣ OSS, ϥΠηϯε͸(Apache License 2.0) ։ൃݩ͸Object

    Computing(OCI) OCIͷGrails։ൃνʔϜ͕։ൃͨ͠৽͍͠FW ϚΠΫϩαʔϏεͳΞϓϦέʔγϣϯΛૣ͘
 ༰қʹ։ൃ͠΍͘͢͢ΔFW
  12. #jjug #ccc_i4 Micronaut ͷಛ௃ • ίϯύΠϧ࣌ͷDI, AOP
 →ىಈ࣌ͷεΩϟϯ΍ղܾෆཁͳҝɺىಈ͕ૣ͍ • Reactive

    ProgramingͷϑΝʔετΫϥεαϙʔτ(Reactor, RxJava) • GraalVMͰͷωΠςΟϒίϯύΠϧ΋αϙʔτ • GraphQL,gRPC,Swagger/OpenAPI,Security,kafka,flyway,etc.. • CLI͕ఏڙ͞Ε͍ͯͯɺ৽ن࡞੒࣌͸CLIΛ࢖͏
 sdk man, homebrew ౳ͰΠϯετʔϧՄೳ
  13. #jjug #ccc_i4 APIͷίʔυྫ @Controller("/hello") class HelloController { @Get("/") fun index():

    String { return "Hello World"; } } ࢀߟݩ: https://micronaut.io/
  14. #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() } }
  15. #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() } } })
  16. #jjug #ccc_i4 Install Micronaut # sdk man $ sdk install

    micronaut # homebrew $ brew install micronaut
  17. #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
  18. #jjug #ccc_i4 IDE Settings • Project ΛImport
 Preferences -> Build

    -> 
 Compiler -> Annotation Processors • “Enable annotation processing” Λ༗ޮԽ
  19. #jjug #ccc_i4 Ktor • JetBrains੡ͷͱͯ΋ܰྔͳϑϨʔϜϫʔΫ • DI, ϩΪϯά, ςϯϓϨʔτΤϯδϯ, ORM౳͸ແ͍

    • ಺෦తʹ Netty, Jetty, Tomcat Λಈ͔ͤΔ • ಛ௃తͳ࢓૊Έͱͯ͠ pipeline • Kotlin ͷ Coroutine ʹ΋ରԠ
  20. #jjug #ccc_i4 Ktor ͷDSL ͳRouting routing { get("/") { call.respondText("Hello

    World!") } get("/demo") { call.respondText("HELLO WORLD!") } } Ҿ༻ݩ: https://ktor.io/
  21. #jjug #ccc_i4 routing { get("/") { call.respondText("Hello World!") } get("/demo")

    { call.respondText("HELLO WORLD!") } } Ktor ͷDSL ͳRouting Ҿ༻ݩ: https://ktor.io/ એݴతʹ͔͚ͯ෼͔Γ΍͍͢ 3PVUJOH͔Β࣮૷Λ௥͍΍͍͢
  22. #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/
  23. #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)) } })
  24. #jjug #ccc_i4 ·ͱΊ • ͦΕͧΕͷϝϦοτΛ׆͔͠ͳ͕Β
 Server Side KotlinΛ͢Δ͜ͱ͕Ͱ͖·͢ɻ • ΋ͪΖΜ૊ΈࠐΈαʔόʔͷKtorΛ࢖Θͣʹ


    MicronautͷΈͰे෼ʹ։ൃ͸ՄೳͰ͢ɻ • બ୒ࢶ͕͋Δ͜ͱ͕ඇৗʹॏཁͰ
 ݱ৔΍νʔϜͷঢ়گʹ߹ΘͤͯબͿͱྑ͍Ͱ͢ɻ