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. Micronaut Ͱ࢝ΊΔ Server Side Kotlin @bulbulpaul #jjug_ccc #ccc_i4 2019/11/23 JJUG

    CCC Fall 2019
  2. #jjug #ccc_i4 Yahoo!カレンダー
 Server Side Engineer @bulbulpaul Fav. Python, Kotlin

    Kansai.kt Organizer ポール
  3. None
  4. using

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

    ͳͥKotlin, Micronaut ͳͷ͔ • Kotlin, Micronaut Λ࢖ͬͯΞϓϦέʔγϣϯΛ࡞Δ [࿩͞ͳ͍͜ͱ] • Kotlinͷࡉ͔ͳݴޠ࢓༷
  6. #jjug #ccc_i4 Disclaimer 2019/11/23 ࣌఺Ͱͷ࠷৽൛͕ϕʔεͰ͢ɻ • Kotlin : 1.3.60 •

    micronaut : 1.2.5
  7. Kotlin

  8. Έͳ͞Μʹ࣭໰Ͱ͢

  9. Kotlin࢖ͬͯ·͔͢ʁ

  10. #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
  11. #jjug #ccc_i4 Kotlin Β͠͞ͱ͸ Kotlin͸࣮༻ੑΛॏཁࢹ͍ͯ͠ΔݴޠͰ͢ɻ • ؆ܿ͞ < Մಡੑ •

    දݱͷ๛͔͞ < ࠶ར༻ੑ • ಠࣗੑ < ૬ޓӡ༻ੑ • ݈શੑ < ҆શੑ Ҿ༻ݩ: Kotlin Conf 2018 Keynote P21
  12. αʔόʔαΠυؔ࿈͸
 Ͳ͏͍ͬͨঢ়گͳͷ͔

  13. #jjug #ccc_i4 Serverside Frameworks and more…

  14. #jjug #ccc_i4 Server Side Frameworks جຊతʹJavaͷFramework͸ͦͷ··࢖͑·͢ɻ େ͖ͳҧ͍ͱͯ͠͸”KotlinΒ͍͠”ίʔυ͕
 ॻ͚Δ͔Ͳ͏͔ɻe.g. NullSafe, DSL౳

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

    Spek kotlintest: https://github.com/kotlintest/kotlintest Spek: https://www.spekframework.org/
  16. #jjug #ccc_i4 ORM ͪ͜Β΋جຊతʹJavaͷ౳͕ͦͷ··࢖͑·͢ɻ Pure KotlinͳORM͸Exposed, etc.. JavaͷϥΠϒϥϦͰKotlin αϙʔτ͍ͯ͠Δ෺ͩͱ •

    Doma • JOOQ • etc…
  17. #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/
  18. ͳͥࠓճ͸
 Micronaut ͷ࿩ʁ

  19. #jjug #ccc_i4 ࠷ۙͷτϨϯυ • ϞϊϦε͔ΒϚΠΫϩαʔϏε • ίϯςφԽ΍Ϋϥ΢υωΠςΟϒԽ • k8s, FaaS,

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

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

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

    ͱ͸͍͑DI, AOP΋ศར͔ͩΒ࢖͍͍ͨ • ศར͚ͩͲখ͞ͳػೳ࣌ʹFaaS౳΁Ҡ؅ͣ͠Β͍ • ϝϞϦͷϑοτϓϦϯτ͕େ͖͘ͳΓ͕ͪ • ݸʑʹׂΓ౰ͯΔϦιʔε͕େ͖͘ͳΔ
  23. ͋Δఔ౓ىಈ΋ૣͯ͘ DI, AOP౳΋࢖͑ͯ KotlinͰ࣮༻ϨϕϧͷFW?

  24. Micronaut

  25. #jjug #ccc_i4 Ҿ༻: https://micronaut.io/

  26. #jjug #ccc_i4 Micronaut JVMϕʔεͷϑϧελοΫͳFrameworkʢҎԼ, FWʣ OSS, ϥΠηϯε͸(Apache License 2.0) ։ൃݩ͸Object

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

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

    String { return "Hello World"; } } ࢀߟݩ: https://micronaut.io/
  29. #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() } }
  30. #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() } } })
  31. First Step. Install & Create App and IDE Setting

  32. #jjug #ccc_i4 Install Micronaut # sdk man $ sdk install

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

    -> 
 Compiler -> Annotation Processors • “Enable annotation processing” Λ༗ޮԽ
  35. ͜Μͳײ͡ͰMicronautͱ KotlinͰ։ൃͷ४උ͕ Ͱ͖·͢

  36. Ͱ͸࣮ࡍʹ ؆୯ͳAPIΛ ࡞ͬͯΈ·͠ΐ͏

  37. Demo

  38. Ͱ΋…

  39. ΋ͬͱ
 KotlinΒ͘͠ ॻ͚ΔͷͰ͸ʁ

  40. Micronaut Ͱ΋ એݴతʹॻ͚ͳ͍͔ͳʁ

  41. ͨͱ͑͹…
 RoutingΛ Annotation͔Β DSLͱ͔

  42. Micronaut with Ktor

  43. #jjug #ccc_i4 Ktor • JetBrains੡ͷͱͯ΋ܰྔͳϑϨʔϜϫʔΫ • DI, ϩΪϯά, ςϯϓϨʔτΤϯδϯ, ORM౳͸ແ͍

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

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

    { call.respondText("HELLO WORLD!") } } Ktor ͷDSL ͳRouting Ҿ༻ݩ: https://ktor.io/ એݴతʹ͔͚ͯ෼͔Γ΍͍͢ 3PVUJOH͔Β࣮૷Λ௥͍΍͍͢
  46. #jjug #ccc_i4 Micronaut x Ktor • Micronaut͸૊ΈࠐΈαʔόʔͱͯ͠
 Netty౳ͷΞϓϦέʔγϣϯαʔόʔΛ࢖͏ • ͦͷؒʹKtorͷ࢓૊ΈΛೖΕͯಈ͔͢

    • ݱࡏ͸v1.0.0-M2
  47. #jjug #ccc_i4 ಈ࡞Πϝʔδ • Micronautͷ૊ΈࠐΈαʔόʔͷ෦෼ͷؒʹ
 KtorΛೖΕࠐΉܗ

  48. #jjug #ccc_i4 ಈ࡞Πϝʔδ • Micronautͷ૊ΈࠐΈαʔόʔͷ෦෼ͷؒʹ
 KtorΛೖΕࠐΉܗ

  49. #jjug #ccc_i4 ಈ࡞Πϝʔδ • Micronautͷ૊ΈࠐΈαʔόʔͷ෦෼ͷؒʹ
 KtorΛೖΕࠐΉܗ ϦΫΤετͷϥΠϑαΠΫϧ͸ ,UPS͕ѻ͏

  50. Second Step. Apply Ktor support

  51. #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/
  52. #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)) } })
  53. Demo

  54. ·ͱΊ

  55. #jjug #ccc_i4 ·ͱΊ • ͦΕͧΕͷϝϦοτΛ׆͔͠ͳ͕Β
 Server Side KotlinΛ͢Δ͜ͱ͕Ͱ͖·͢ɻ • ΋ͪΖΜ૊ΈࠐΈαʔόʔͷKtorΛ࢖Θͣʹ


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