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

はてなにおけるマイクロサービスとScala / Microservices and Scala at Hatena

aereal
October 08, 2016

はてなにおけるマイクロサービスとScala / Microservices and Scala at Hatena

Scala関西Summit 2016 (http://summit.scala-kansai.org/) の発表資料です。

aereal

October 08, 2016
Tweet

More Decks by aereal

Other Decks in Programming

Transcript

  1. ͸ͯͳʹ͓͚Δ
    ϚΠΫϩαʔϏεͱScala
    id:aereal

    View full-size slide

  2. ࣗݾ঺հ
    • id:aereal
    • גࣜձࣾ͸ͯͳ (2012ʙ)
    • ͸ͯͳϒοΫϚʔΫ
    • ͸ͯͳϒϩά
    • Perl, Ruby, Scala, Shell Script

    View full-size slide

  3. ΞδΣϯμ
    • ৽ (ϚΠΫϩ) αʔϏεʹ͍ͭͯ
    • ͳͥϚΠΫϩαʔϏε͔
    • ͳͥScala͔
    • DDDͷ࣮ફ

    View full-size slide

  4. DISCLAIMER
    • ։ൃதͷ಺༰ؚ͕·Ε·͢
    • ࣮ࡍͷϦϦʔε࣌ʹ͸มߋ͞Ε͍ͯΔՄೳੑ͋Γ

    View full-size slide

  5. ৽αʔϏεʹ͍ͭͯ

    View full-size slide

  6. http://hatenablog.com/

    View full-size slide

  7. “͸ͯͳϒϩάΛ΋ͬͱຊ֨తʹ࢖͍͍ͨํͷͨ
    Ίʹɺ༗ྉϓϥϯʮ͸ͯͳϒϩάProʯΛ༻ҙ͠
    ͓ͯΓ·͢ɻ޿ࠂͷඇදࣔ΍ಠࣗυϝΠϯػೳ
    ͳͲɺ͸ͯͳϒϩάΛΑΓศརʹ͓࢖͍͍ͨͩ
    ͚·͢ɻ”
    http://hatenablog.com/guide/pro

    View full-size slide

  8. ͸ͯͳϙΠϯτ
    ΫϨδοτΧʔυ
    ίϯϏχৼࠐ
    ۜߦৼࠐ
    ࢖༻
    https://www.hatena.ne.jp/shop/point/list

    View full-size slide

  9. http://hatena.g.hatena.ne.jp/hatena/20150529/1432869070

    View full-size slide

  10. վम or ࡞Γ௚͠
    • طଘͷγεςϜ͸ϞϊϦγοΫ
    • ܾࡁͯ͠͸ͯͳϙΠϯτΛνϟʔδ͢Δ
    • ͸ͯͳϙΠϯτΛ࢖ͬͯߪೖ͢Δ
    • ࢓༷Λ੔ཧ͢Δ͍͍ػձͰ΋͋Δ

    View full-size slide

  11. ܾࡁ୅ߦαʔϏε

    (֎෦)
    ͸ͯͳͷαʔϏε
    (ϒοΫϚʔΫͳͲ)
    ݱߦܾࡁγεςϜ
    ϙΠϯτ؅ཧ
    ڞ༗Ϟδϡʔϧ
    ༗ྉϓϥϯ؅ཧ
    ґଘ
    ͸ͯͳͷαʔϏε
    ґଘ
    ͸ͯͳͷαʔϏε
    ґଘ
    etc. ΞΧ΢ϯτ

    View full-size slide

  12. ݱߦܾࡁγεςϜͷߏ੒
    • ܾࡁγεςϜ: WebΞϓϦέʔγϣϯ
    • API͸ͳ͘HTMLͷϑΥʔϜ͕͋ΔͷΈ
    • ڞ༗Ϟδϡʔϧ: ܾࡁ୅ߦαʔϏεͱ௨৴͢Δ࣮૷
    • ͨ͘͞ΜͷϦϙδτϦ͔Βґଘ͞Ε͍ͯΔ
    • ڞ༗DBΛૢ࡞͢Δ

    View full-size slide

  13. ݱߦܾࡁγεςϜͷվम
    • ΄ͱΜͲݱ࣮తͰ͸ͳ͍
    • ޓ׵ੑʹؔ͢ΔεΩʔϜ͕ͳ͍
    • ׬શޙํޓ׵ or die
    • ෳ਺ͷυϝΠϯ͕ೖΓཚΕ͍ͯΔ
    • ݱࡏͷ࢓༷ʹӨڹΛڧ͘ड͚Δ
    • վળ͕·ΘΒͳ͍

    View full-size slide

  14. ϑϧεΫϥον

    View full-size slide

  15. ৽ܾࡁγεςϜ
    • ܾࡁ୅ߦαʔϏεͷήʔτ΢ΣΠͱͯ͠ಇ͘
    • ͸ͯͳϒϩάProͷ෇༩ͳͲ͸֤αʔϏεʹҠৡ
    • JSON over HTTPͳAPIͷΈΛఏڙ͢Δ
    • ର৅͸SSKDs (= small set of known developers)

    View full-size slide

  16. ৽ܾࡁγεςϜ

    ߏ੒ਤ
    ܾࡁ୅ߦαʔϏε

    (֎෦)
    ͸ͯͳͷαʔϏε
    ༗ྉϓϥϯͷ
    ঢ়ଶ؅ཧ
    ৽ܾࡁγεςϜ

    View full-size slide

  17. ͳͥϚΠΫϩαʔϏε͔

    View full-size slide

  18. ϚΠΫϩαʔϏεͰ࡞Δ͔
    • 1ʙ3ষʹ͔͚ͯϝϦοτ΍
    ʮ༏ΕͨαʔϏεʯʹ͍ͭͯ
    ड़΂ΒΕ͍ͯΔ
    • ϚΠΫϩαʔϏεʹΑͬͯ

    αʔϏε͸ΑΓΑ͘ͳΔͷ͔
    • ΨόφϯεͳͲɺ

    ίετ͸෷͑ΔͩΖ͏͔
    https://www.oreilly.co.jp/books/9784873117607/

    View full-size slide

  19. ૄ݁߹ͱߴڽूੑ
    1. ϞϊϦγοΫͰखΛೖΕͮΒ͍ (ີ݁߹)
    2. ৽ͨʹ༗ྉϓϥϯΛಋೖ͍ͨ͠αʔϏε͸࠶࣮૷
    3. υϝΠϯ஌͕ࣝྲྀग़ (௿ڽूੑ)
    4.

    View full-size slide

  20. ࣋ଓՄೳʹ࡞Γ (ͳ͓͠) ͍ͨ
    • είʔϓΛখ͘͞໌ྎʹอͭ
    • ૄ݁߹ (ʰϚΠΫϩαʔϏεΞʔΩςΫνϟʱ3.2.1)
    • ߴڽूੑ (ʰϚΠΫϩαʔϏεΞʔΩςΫνϟʱ3.2.2)
    • ෼ׂ౷࣏͢Δ
    • ٕज़ҟ࣭ੑ (͍ͭ·Ͱ΋Perl͸ݫ͍͠)
    • σϓϩΠͷϥΠϑαΠΫϧΛૣΊΔ

    View full-size slide

  21. νʔϜؒ࿈ܞͷ

    Ή͔ͣ͠͞ͱઓ͏

    View full-size slide

  22. ΠϯηϓγϣϯσοΩΛ࡞Δ
    • ۙ͝ॴ͞ΜΛ໌Β͔ʹ͢Δ
    • ϝϯς͸Ͳ͜ʹҾ͖ܧ͙ͷ͔
    • ԿΛ༏ઌ͢Δͷ͔ (ఘΊΔͷ͔) Λ͸͖ͬΓͤ͞Δ
    • είʔϓɺ඼࣭ɺ༧ࢉɺεέδϡʔϧ
    • ʰΞδϟΠϧαϜϥΠʱ

    View full-size slide

  23. ͳͥϚΠΫϩαʔϏε͔: ·ͱΊ
    • ࠓճ͸ϚΠΫϩαʔϏεͰ΍͍ͬͯ͘
    • ಘΒΕΔϝϦοτ > ෷͏ίετ
    • ΨόφϯεͳͲෆ҆ͳ఺ʹ΋खΛଧͭ
    • ΠϯηϓγϣϯσοΩ
    • ૣ͔͘Βר͖ࠐΉ

    View full-size slide

  24. ࣮૷ݴޠΛܾΊΔ
    • ͸ͯͳͷબ୒ࢶͱͯ͠͸Perl, Go, ͦͯ͠Scala
    • ৽ܾࡁγεςϜͷίΞυϝΠϯʹٕज़ͦͷ΋ͷ͸

    ؚ·Εͳ͍
    • ϦεΫΛͱͬͯଞͷ৽ͨͳݴޠΛ࠾༻͢Δ

    ϝϦοτ͸ബ͍

    View full-size slide

  25. ࣮૷ݴޠΛܾΊΔ
    • ͳʹ͔ࢦඪ͕΄͍͠
    • ؾ࣋ͪ͸͍Ζ͍Ζ͋Δ
    • ʮ޷͖͔ͩΒʯͰ΋͍͍͚Ͳ……
    • ͲΜͳPros/ConsΛݟͨͷ͔

    View full-size slide

  26. ඇػೳཁٻΛఆΊΔ
    • ISO 9126
    • functionality: ػೳੑ
    • reliability: ৴པੑ
    • usability: ࢖༻ੑ
    • efficiency: ޮ཰ੑ
    • maintainability: อकੑ
    • portability: Ҡ২ੑ

    View full-size slide

  27. ඇػೳཁٻΛఆΊΔ
    • ISO 9126
    • functionality: ػೳੑ
    • reliability: ৴པੑ
    • usability: ࢖༻ੑ
    • efficiency: ޮ཰ੑ
    • maintainability: อकੑ
    • portability: Ҡ২ੑ

    View full-size slide

  28. ݴޠΛબͿ্ͰٻΊΔ΋ͷ
    • ๛͔ͳදݱྗ
    • ෳࡶͳυϝΠϯΛաෆ଍ͳ͘දݱ͍ͨ͠
    • ֶशۂઢͷ؇΍͔͞
    • ։ൃεϐʔυΛૣΊʹߴΊ҆ఆ͍ͤͨ͞
    • কདྷɺ։ൃ͢Δਓʹ΋΍͘͞͠

    View full-size slide

  29. ෳࡶͳυϝΠϯ
    • ঢ়ଶ΍ߟྀ͢΂͖ม਺͕ೖΓ૊ΜͰ͍Δ
    • ΫϨδοτΧʔυͷ༗ޮظݶ͕੾Ε͍ͯͨΒ?
    • ܾࡁ୅ߦαʔϏε͕མ͍ͪͯͨΒ?

    View full-size slide

  30. ঢ়ଶΛྻڍܕͰදݱ͢Δ
    sealed trait State
    object State {
    case object Requested extends State
    case object Paid extends State
    case object Failed extends State
    }
    ※࣮ࡍ͸΋͏ͪΐͬͱ͍Ζ͍Ζ͋Γ·͢

    View full-size slide

  31. ঢ়ଶΛྻڍܕͰදݱ͢Δ
    case class Payment(state: State) {
    def charged: Boolean = state match {
    case State.Paid => true
    case _ => false
    }
    }

    View full-size slide

  32. ঢ়ଶΛྻڍܕͰදݱ͢Δ
    • sealedम০͢ΔͱmatchࣜͰ໢ཏੑνΣοΫͰ͖Δ
    • ͱΓ͏Δঢ়ଶʹ໊͍ͭͯલΛ༩͑ΒΕΔ
    • ʮPaid͔FailedʹͳͬͨΒͦͷPayment͸

    ऴ୺͠·͢Ͷʯ

    View full-size slide

  33. Scalaͷֶशۂઢ……?
    • ؇΍͔Ͱ͸ͳ͍! (ݸਓͷݟղ)
    • ͔͠͠͸ͯͳͰ͸ࢧԉ͢Δ؀ڥΛ੔͖͑ͯͨ
    • ࣄྫ (Mackerel, ͸ͯͳϒοΫϚʔΫϦχϡʔΞϧ)
    • ͸ͯͳڭՊॻ github.com/hatena/Hatena-Textbook
    • ScalaͷఆੴΛ୳Δձ

    View full-size slide

  34. ScalaͷఆੴΛ୳Δձ
    • ϥΠϒϥϦ΍࣮૷ύλʔϯͷ஌ݟΛ࣋ͪدΔ
    • ScalaΛ࢖͍ͬͯΔνʔϜͷ༗ࢤͰ։࠵
    • Mackerel
    • ϒοΫϚʔΫϦχϡʔΞϧ
    • ৽ܾࡁγεςϜ

    View full-size slide

  35. ఆੴΛ୳ΔձͰ࿩ͨ͜͠ͱ
    • ϨΠϠߏ੒
    • ΞϓϦέʔγϣϯ૚
    • υϝΠϯ૚
    • Πϯϑϥ૚
    • બΜͩWAF/ORMͱͦͷഎܠ

    View full-size slide

  36. ͳͥScala͔: ·ͱΊ
    • ৽ܾࡁγεςϜ͕ٻΊΔཁ݅Λຬͨͯ͘͠Εͦ͏
    • ػೳੑ
    • อकੑ
    • ScalaΛॻ͘ΤϯδχΞΛҭͯΔ౔৕Λ੔͖͑ͯͨ
    • ScalaͷఆੴΛ୳ΔձɺڭՊॻ

    View full-size slide

  37. DDD͍ͨ͠
    • υϝΠϯΤΩεύʔτͷ஌͕ࣝ

    ͦͷ··མͱ͠ࠐ·ΕͨαʔϏεʹ͍ͨ͠
    • ཁૉٕज़ (Πϯϑϥ૚) ͱ෼཭Մೳʹ͍ͨ͠
    • ࠷ѱ·ͨ࡞Γ௚͢͜ͱʹͳͬͯ΋

    ΑΓ௧ΈΛগͳ͍ͨ͘͠

    View full-size slide

  38. ݱߦܾࡁγεςϜͷ൓ল
    • ํ਑͕పఈ͞Εͳ͔ͬͨ
    • ʮApp͸΋͏ͩΊͩɺServiceͰ΍Γͳ͓ͦ͏ʯ
    • ಺੡ͷPerl൛Active RecordతORM
    • ϞσϧɺαʔϏεɺϦϙδτϦ͕ᕒવҰମ
    • Perlͷݶք
    • ΠϯλʔϑΣʔε͕ͳ͍
    • ΧϓηϧԽ͕ۃΊͯࠔ೉

    View full-size slide

  39. ͸ͯͳϒοΫϚʔΫ in Scala

    View full-size slide

  40. [PR]

    PerlͰ΋ΠέͯΔ։ൃ͍ͯ͠·͢
    Perlͷ্ʹ΋ࡾ೥

    ʙͣͬͱΠέͯΔαʔϏεΛ࡞Γଓ͚Δٕज़ʙ
    http://yapcasia.org/2015/talk/show/de9e7a1e-136d-11e5-a9fc-
    d9f87d574c3a

    View full-size slide

  41. ͸ͯͳࣾ಺Ͱ։࠵ͨ͠

    DDDษڧձͷ༷ࢠΛ͝঺հ͠·͢
    http://developer.hatenastaff.com/entry/2015/08/20/170300

    View full-size slide

  42. ΤϦοΫɾΤϰΝϯεͷ

    υϝΠϯۦಈઃܭ
    http://www.shoeisha.co.jp/book/detail/9784798126708

    View full-size slide

  43. ࣮ફυϝΠϯۦಈઃܭ
    http://www.shoeisha.co.jp/book/detail/9784798131610

    View full-size slide

  44. ࣾ಺DDDษڧձ

    View full-size slide

  45. ࣾ಺DDDษڧձ
    • ਎ۙͳέʔεʹ͍ͭͯٞ࿦
    • ৽͘͠࡞ΔͳΒɺ͍·͔Βվળ͢ΔͳΒ
    • ֎ͷੈքͷ஌ݟΛڞ༗
    • ʮScalaͩͱ͜͏͍͏ػೳ͕͋ͬͯ……ʯ
    • ʮHaskellͩͱ……ʯ
    • ʮRubyͷ˓˓ͱ͍͏ϥΠϒϥϦ͸……ʯ

    View full-size slide

  46. DDDͷ࣮ફͱ࡞ઓ

    View full-size slide

  47. ೰Μͩͱ͜ΖɾݟͲ͜Ζ
    • ΤϯςΟςΟͷදݱ
    • ґଘੑͷ஫ೖ (DI)
    • cake pattern
    • αϒϓϩδΣΫτԽ
    • layering violationΛ๷͙

    View full-size slide

  48. ΤϯςΟςΟͷදݱ

    View full-size slide

  49. ΤϯςΟςΟͷදݱ
    • ΤϯςΟςΟ͸ૉ๿ͳcase class
    • ORM͸Slick
    • tarao/slick-jdbc-extensionΛ࢖ͬͯੜSQLͰҾ͍ͯ

    case class΁Ϛοϐϯά

    View full-size slide

  50. ΤϯςΟςΟͷදݱ
    • ΤϯςΟςΟ = ID + υϝΠϯϞσϧ
    • ܕ΋ύϥϝʔλԽ͞Ε͍ͯΔ
    • υϝΠϯϞσϧʹରͯ͠ڞม
    • ಉ͡IDܕ͕ͩυϝΠϯϞσϧ͕ҟͳΔ

    ΤϯςΟςΟΛఆٛͰ͖Δ

    View full-size slide

  51. ίʔυྫ
    case class EntityKey[ID](
    val repr: String
    ) extends AnyVal
    abstract class Entity[K, +V](
    val id: EntityKey[K],
    val value: V
    )(implicit ev: V <:< K)

    View full-size slide

  52. ίʔυྫ
    case class EntityKey[ID](
    val repr: String
    ) extends AnyVal
    abstract class Entity[K, +V](
    val id: EntityKey[K],
    val value: V
    )(implicit ev: V <:< K)
    phantom type

    View full-size slide

  53. ίʔυྫ
    case class EntityKey[ID](
    val repr: String
    ) extends AnyVal
    abstract class Entity[K, +V](
    val id: EntityKey[K],
    val value: V
    )(implicit ev: V <:< K)
    ஋ܕΛڞมʹ

    View full-size slide

  54. ίʔυྫ
    case class EntityKey[ID](
    val repr: String
    ) extends AnyVal
    abstract class Entity[K, +V](
    val id: EntityKey[K],
    val value: V
    )(implicit ev: V <:< K)
    ஋ͷܕ͕IDͷܕͷαϒΫϥεͰ͋Δ

    ܕύϥϝʔλ੍໿

    View full-size slide

  55. cake pattern

    View full-size slide

  56. ΠϯλʔϑΣʔεΛఆٛ
    package repo
    trait AccountComponent {
    def accountLoader: AccountLoader
    trait AccountLoader
    }

    View full-size slide

  57. ࣮૷
    package infra.db
    trait AccountComponent
    extends repo.AccountComponent {
    def accountLoader: AccountLoader =
    AccountDB
    object AccountDB extends AccountLoader
    { ... }
    }

    View full-size slide

  58. ґଘͷએݴͱ࢖༻
    package app
    trait AccountApp {
    self: repo.AccountComponent =>
    accountLoader.find(...)
    }

    View full-size slide

  59. ґଘͷ஫ೖ
    package main
    object Root
    extends app.AccountApp
    with infra.db.AccountComponent

    View full-size slide

  60. cake pattern
    • ίϯύΠϧλΠϜͰDI͞ΕΔ
    • Playͷίϯτϩʔϥ΋cake componentͱͯ͠߹੒

    View full-size slide

  61. DDDͷ࣮ફ: ·ͱΊ
    • ࣾ಺ษڧձͰʰ࣮ફDDDʱΛྠಡɺ஌ݟΛڞ༗
    • զʑʹͱͬͯͪΐ͏ͲΑ͍DDDΛݟ͚ͭΔ
    • ScalaͷྗΛ׆͔࣮ͨ͠૷ύλʔϯͷൃݟ

    View full-size slide

  62. ·ͱΊ
    • ۜͷ஄ؙ͸ͳ͍!
    • ཁ݅ʹ͍͋ͬͯΔ͔ߟ͑Δ
    • ཁ݅ʹ͋ΘͤͯΞϨϯδ͍ͯ͘͠
    • ʮͪΐ͏Ͳ͍͍DDDʯ
    • ʮͪΐ͏Ͳ͍͍ϚΠΫϩαʔϏεʯ
    • ࣌ʹ͸ࣃΛ৯͍͠͹Δ

    View full-size slide