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
はてなにおけるマイクロサービスとScala / Microservices and Scala...
Search
aereal
October 08, 2016
Programming
6
34k
はてなにおけるマイクロサービスとScala / Microservices and Scala at Hatena
Scala関西Summit 2016 (
http://summit.scala-kansai.org/
) の発表資料です。
aereal
October 08, 2016
Tweet
Share
More Decks by aereal
See All by aereal
How to send distibuted traces to Datadog using build own OpenTelemetry-Lambda distribution
aereal
3
180
好きな技術《コト》で、 生きていく技術 / life with what you like
aereal
5
2.1k
qron: Cloud Native Cron Alternativeの今
aereal
2
1.9k
自動作曲入門 / introduction to programatic music composition
aereal
1
530k
はてなブログ タグとCDK / The epic of AWS CDK and Hatena Blog Tag
aereal
3
200k
はてなブログ タグの技術選択 / The technical details of Hatena Blog Tag
aereal
3
200k
ブログサービスのHTTPS化を支えたAWSで作るピタゴラスイッチ / The construction of large scale TLS certificates management system with AWS
aereal
3
400k
AWSではてなブログの常時HTTPS配信をバーンとやる話 / The Epic of migration from HTTP to HTTPS on Hatena Blog with AWS
aereal
14
17k
ScalaとPerlでMicroservices in production / Building microservices with Perl and Scala in production
aereal
0
5.3k
Other Decks in Programming
See All in Programming
DjangoNinjaで高速なAPI開発を実現する
masaya00
0
490
[KR] Server Driven Compose With Firebase
skydoves
2
170
クラウドサービスの 利用コストを削減する技術 - 円安の真南風を感じて -
pyama86
3
250
Go製CLIツールGatling Commanderによる負荷試験実施の自動化
okmtz
3
680
Quarto Clean Theme
nicetak
0
220
Applied NLP in the Age of Generative AI
inesmontani
PRO
3
1.2k
型付きで行うVSCode拡張機能開発 / VSCode Meetup #31
mazrean
0
230
CSC305 Lecture 01
javiergs
PRO
1
140
"Swarming" をコンセプトに掲げるアジャイルチームのベストプラクティス
boykush
2
210
ポケモンで考えるコミュニケーション / Communication Lessons from Pokémon
mackey0225
4
160
ECS向けのドリフト検知機構を実装してみた
tkikuc
0
280
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
6
260
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
324
23k
Practical Orchestrator
shlominoach
185
10k
Building Adaptive Systems
keathley
37
2.1k
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.9k
GitHub's CSS Performance
jonrohan
1030
450k
Faster Mobile Websites
deanohume
304
30k
Building a Scalable Design System with Sketch
lauravandoore
459
32k
A better future with KSS
kneath
236
17k
Docker and Python
trallard
40
3k
4 Signs Your Business is Dying
shpigford
180
21k
Designing for humans not robots
tammielis
249
25k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
249
21k
Transcript
ͯͳʹ͓͚Δ ϚΠΫϩαʔϏεͱScala id:aereal
ࣗݾհ • id:aereal • גࣜձࣾͯͳ (2012ʙ) • ͯͳϒοΫϚʔΫ • ͯͳϒϩά
• Perl, Ruby, Scala, Shell Script
ΞδΣϯμ • ৽ (ϚΠΫϩ) αʔϏεʹ͍ͭͯ • ͳͥϚΠΫϩαʔϏε͔ • ͳͥScala͔ •
DDDͷ࣮ફ
DISCLAIMER • ։ൃதͷ༰ؚ͕·Ε·͢ • ࣮ࡍͷϦϦʔε࣌ʹมߋ͞Ε͍ͯΔՄೳੑ͋Γ
৽αʔϏεʹ͍ͭͯ
http://hatenablog.com/
“ͯͳϒϩάΛͬͱຊ֨తʹ͍͍ͨํͷͨ Ίʹɺ༗ྉϓϥϯʮͯͳϒϩάProʯΛ༻ҙ͠ ͓ͯΓ·͢ɻࠂͷඇදࣔಠࣗυϝΠϯػೳ ͳͲɺͯͳϒϩάΛΑΓศརʹ͓͍͍ͨͩ ͚·͢ɻ” http://hatenablog.com/guide/pro
ͯͳϙΠϯτ ΫϨδοτΧʔυ ίϯϏχৼࠐ ۜߦৼࠐ ༻ https://www.hatena.ne.jp/shop/point/list
http://hatena.g.hatena.ne.jp/hatena/20150529/1432869070
վम or ࡞Γ͠ • طଘͷγεςϜϞϊϦγοΫ • ܾࡁͯͯ͠ͳϙΠϯτΛνϟʔδ͢Δ • ͯͳϙΠϯτΛͬͯߪೖ͢Δ •
༷Λཧ͢Δ͍͍ػձͰ͋Δ
ܾࡁߦαʔϏε (֎෦) ͯͳͷαʔϏε (ϒοΫϚʔΫͳͲ) ݱߦܾࡁγεςϜ ϙΠϯτཧ ڞ༗Ϟδϡʔϧ ༗ྉϓϥϯཧ ґଘ ͯͳͷαʔϏε
ґଘ ͯͳͷαʔϏε ґଘ etc. ΞΧϯτ
ݱߦܾࡁγεςϜͷߏ • ܾࡁγεςϜ: WebΞϓϦέʔγϣϯ • APIͳ͘HTMLͷϑΥʔϜ͕͋ΔͷΈ • ڞ༗Ϟδϡʔϧ: ܾࡁߦαʔϏεͱ௨৴͢Δ࣮ •
ͨ͘͞ΜͷϦϙδτϦ͔Βґଘ͞Ε͍ͯΔ • ڞ༗DBΛૢ࡞͢Δ
ݱߦܾࡁγεςϜͷվम • ΄ͱΜͲݱ࣮తͰͳ͍ • ޓੑʹؔ͢ΔεΩʔϜ͕ͳ͍ • શޙํޓ or die •
ෳͷυϝΠϯ͕ೖΓཚΕ͍ͯΔ • ݱࡏͷ༷ʹӨڹΛڧ͘ड͚Δ • վળ͕·ΘΒͳ͍
ϑϧεΫϥον
৽ܾࡁγεςϜ • ܾࡁߦαʔϏεͷήʔτΣΠͱͯ͠ಇ͘ • ͯͳϒϩάProͷ༩ͳͲ֤αʔϏεʹҠৡ • JSON over HTTPͳAPIͷΈΛఏڙ͢Δ •
ରSSKDs (= small set of known developers)
৽ܾࡁγεςϜ ߏਤ ܾࡁߦαʔϏε (֎෦) ͯͳͷαʔϏε ༗ྉϓϥϯͷ ঢ়ଶཧ ৽ܾࡁγεςϜ
چ ৽
ͳͥϚΠΫϩαʔϏε͔
ϚΠΫϩαʔϏεͰ࡞Δ͔ • 1ʙ3ষʹ͔͚ͯϝϦοτ ʮ༏ΕͨαʔϏεʯʹ͍ͭͯ ड़ΒΕ͍ͯΔ • ϚΠΫϩαʔϏεʹΑͬͯ αʔϏεΑΓΑ͘ͳΔͷ͔ • ΨόφϯεͳͲɺ
ίετ͑ΔͩΖ͏͔ https://www.oreilly.co.jp/books/9784873117607/
ૄ݁߹ͱߴڽूੑ 1. ϞϊϦγοΫͰखΛೖΕͮΒ͍ (ີ݁߹) 2. ৽ͨʹ༗ྉϓϥϯΛಋೖ͍ͨ͠αʔϏε࠶࣮ 3. υϝΠϯ͕ࣝྲྀग़ (ڽूੑ) 4.
࣋ଓՄೳʹ࡞Γ (ͳ͓͠) ͍ͨ • είʔϓΛখ͘͞໌ྎʹอͭ • ૄ݁߹ (ʰϚΠΫϩαʔϏεΞʔΩςΫνϟʱ3.2.1) • ߴڽूੑ
(ʰϚΠΫϩαʔϏεΞʔΩςΫνϟʱ3.2.2) • ׂ౷࣏͢Δ • ٕज़ҟ࣭ੑ (͍ͭ·ͰPerlݫ͍͠) • σϓϩΠͷϥΠϑαΠΫϧΛૣΊΔ
νʔϜؒ࿈ܞͷ Ή͔ͣ͠͞ͱઓ͏
ΠϯηϓγϣϯσοΩΛ࡞Δ • ۙ͝ॴ͞ΜΛ໌Β͔ʹ͢Δ • ϝϯςͲ͜ʹҾ͖ܧ͙ͷ͔ • ԿΛ༏ઌ͢Δͷ͔ (ఘΊΔͷ͔) Λ͖ͬΓͤ͞Δ •
είʔϓɺ࣭ɺ༧ࢉɺεέδϡʔϧ • ʰΞδϟΠϧαϜϥΠʱ
ͳͥϚΠΫϩαʔϏε͔: ·ͱΊ • ࠓճϚΠΫϩαʔϏεͰ͍ͬͯ͘ • ಘΒΕΔϝϦοτ > ͏ίετ • ΨόφϯεͳͲෆ҆ͳʹखΛଧͭ
• ΠϯηϓγϣϯσοΩ • ૣ͔͘Βר͖ࠐΉ
ͳͥScala͔
࣮ݴޠΛܾΊΔ • ͯͳͷબࢶͱͯ͠Perl, Go, ͦͯ͠Scala • ৽ܾࡁγεςϜͷίΞυϝΠϯʹٕज़ͦͷͷ ؚ·Εͳ͍ • ϦεΫΛͱͬͯଞͷ৽ͨͳݴޠΛ࠾༻͢Δ
ϝϦοτബ͍
࣮ݴޠΛܾΊΔ • ͳʹ͔ࢦඪ͕΄͍͠ • ؾ͍࣋ͪΖ͍Ζ͋Δ • ʮ͖͔ͩΒʯͰ͍͍͚Ͳ…… • ͲΜͳPros/ConsΛݟͨͷ͔
ඇػೳཁٻΛఆΊΔ • ISO 9126 • functionality: ػೳੑ • reliability: ৴པੑ
• usability: ༻ੑ • efficiency: ޮੑ • maintainability: อकੑ • portability: Ҡ২ੑ
ඇػೳཁٻΛఆΊΔ • ISO 9126 • functionality: ػೳੑ • reliability: ৴པੑ
• usability: ༻ੑ • efficiency: ޮੑ • maintainability: อकੑ • portability: Ҡ২ੑ
ݴޠΛબͿ্ͰٻΊΔͷ • ๛͔ͳදݱྗ • ෳࡶͳυϝΠϯΛաෆͳ͘දݱ͍ͨ͠ • ֶशۂઢͷ؇͔͞ • ։ൃεϐʔυΛૣΊʹߴΊ҆ఆ͍ͤͨ͞ •
কདྷɺ։ൃ͢Δਓʹ͘͞͠
ෳࡶͳυϝΠϯ • ঢ়ଶߟྀ͖͢ม͕ೖΓΜͰ͍Δ • ΫϨδοτΧʔυͷ༗ޮظݶ͕Ε͍ͯͨΒ? • ܾࡁߦαʔϏε͕མ͍ͪͯͨΒ?
ঢ়ଶΛྻڍܕͰදݱ͢Δ sealed trait State object State { case object Requested
extends State case object Paid extends State case object Failed extends State } ※࣮ࡍ͏ͪΐͬͱ͍Ζ͍Ζ͋Γ·͢
ঢ়ଶΛྻڍܕͰදݱ͢Δ case class Payment(state: State) { def charged: Boolean =
state match { case State.Paid => true case _ => false } }
ঢ়ଶΛྻڍܕͰදݱ͢Δ • sealedम০͢ΔͱmatchࣜͰཏੑνΣοΫͰ͖Δ • ͱΓ͏Δঢ়ଶʹ໊͍ͭͯલΛ༩͑ΒΕΔ • ʮPaid͔FailedʹͳͬͨΒͦͷPayment ऴ͠·͢Ͷʯ
Scalaͷֶशۂઢ……? • ؇͔Ͱͳ͍! (ݸਓͷݟղ) • ͔ͯ͠͠ͳͰࢧԉ͢ΔڥΛ͖͑ͯͨ • ࣄྫ (Mackerel, ͯͳϒοΫϚʔΫϦχϡʔΞϧ)
• ͯͳڭՊॻ github.com/hatena/Hatena-Textbook • ScalaͷఆੴΛ୳Δձ
ScalaͷఆੴΛ୳Δձ • ϥΠϒϥϦ࣮ύλʔϯͷݟΛ࣋ͪدΔ • ScalaΛ͍ͬͯΔνʔϜͷ༗ࢤͰ։࠵ • Mackerel • ϒοΫϚʔΫϦχϡʔΞϧ •
৽ܾࡁγεςϜ
ఆੴΛ୳ΔձͰͨ͜͠ͱ • ϨΠϠߏ • ΞϓϦέʔγϣϯ • υϝΠϯ • Πϯϑϥ •
બΜͩWAF/ORMͱͦͷഎܠ
ͳͥScala͔: ·ͱΊ • ৽ܾࡁγεςϜ͕ٻΊΔཁ݅Λຬͨͯ͘͠Εͦ͏ • ػೳੑ • อकੑ • ScalaΛॻ͘ΤϯδχΞΛҭͯΔΛ͖͑ͯͨ
• ScalaͷఆੴΛ୳ΔձɺڭՊॻ
DDDͷ࣮ફ
DDD͍ͨ͠ • υϝΠϯΤΩεύʔτͷ͕ࣝ ͦͷ··མͱ͠ࠐ·ΕͨαʔϏεʹ͍ͨ͠ • ཁૉٕज़ (Πϯϑϥ) ͱՄೳʹ͍ͨ͠ • ࠷ѱ·ͨ࡞Γ͢͜ͱʹͳͬͯ
ΑΓ௧ΈΛগͳ͍ͨ͘͠
ݱߦܾࡁγεςϜͷল • ํ͕పఈ͞Εͳ͔ͬͨ • ʮApp͏ͩΊͩɺServiceͰΓͳ͓ͦ͏ʯ • ͷPerl൛Active RecordతORM • ϞσϧɺαʔϏεɺϦϙδτϦ͕ᕒવҰମ
• Perlͷݶք • ΠϯλʔϑΣʔε͕ͳ͍ • ΧϓηϧԽ͕ۃΊͯࠔ
ͯͳϒοΫϚʔΫ in Scala
[PR] PerlͰΠέͯΔ։ൃ͍ͯ͠·͢ Perlͷ্ʹࡾ ʙͣͬͱΠέͯΔαʔϏεΛ࡞Γଓ͚Δٕज़ʙ http://yapcasia.org/2015/talk/show/de9e7a1e-136d-11e5-a9fc- d9f87d574c3a
DDDؒ
ͯͳࣾͰ։࠵ͨ͠ DDDษڧձͷ༷ࢠΛ͝հ͠·͢ http://developer.hatenastaff.com/entry/2015/08/20/170300
ΤϦοΫɾΤϰΝϯεͷ υϝΠϯۦಈઃܭ http://www.shoeisha.co.jp/book/detail/9784798126708
࣮ફυϝΠϯۦಈઃܭ http://www.shoeisha.co.jp/book/detail/9784798131610
ࣾDDDษڧձ
ࣾDDDษڧձ • ۙͳέʔεʹ͍ͭͯٞ • ৽͘͠࡞ΔͳΒɺ͍·͔Βվળ͢ΔͳΒ • ֎ͷੈքͷݟΛڞ༗ • ʮScalaͩͱ͜͏͍͏ػೳ͕͋ͬͯ……ʯ •
ʮHaskellͩͱ……ʯ • ʮRubyͷ˓˓ͱ͍͏ϥΠϒϥϦ……ʯ
DDDͷ࣮ફͱ࡞ઓ
Μͩͱ͜ΖɾݟͲ͜Ζ • ΤϯςΟςΟͷදݱ • ґଘੑͷೖ (DI) • cake pattern •
αϒϓϩδΣΫτԽ • layering violationΛ͙
ΤϯςΟςΟͷදݱ
ΤϯςΟςΟͷදݱ • ΤϯςΟςΟૉͳcase class • ORMSlick • tarao/slick-jdbc-extensionΛͬͯੜSQLͰҾ͍ͯ case classϚοϐϯά
ΤϯςΟςΟͷදݱ • ΤϯςΟςΟ = ID + υϝΠϯϞσϧ • ܕύϥϝʔλԽ͞Ε͍ͯΔ •
υϝΠϯϞσϧʹରͯ͠ڞม • ಉ͡IDܕ͕ͩυϝΠϯϞσϧ͕ҟͳΔ ΤϯςΟςΟΛఆٛͰ͖Δ
ίʔυྫ 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)
ίʔυྫ 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
ίʔυྫ 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) ܕΛڞมʹ
ίʔυྫ 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ͷܕͷαϒΫϥεͰ͋Δ ܕύϥϝʔλ੍
cake pattern
ΠϯλʔϑΣʔεΛఆٛ package repo trait AccountComponent { def accountLoader: AccountLoader trait
AccountLoader }
࣮ package infra.db trait AccountComponent extends repo.AccountComponent { def accountLoader:
AccountLoader = AccountDB object AccountDB extends AccountLoader { ... } }
ґଘͷએݴͱ༻ package app trait AccountApp { self: repo.AccountComponent => accountLoader.find(...)
}
ґଘͷೖ package main object Root extends app.AccountApp with infra.db.AccountComponent
cake pattern • ίϯύΠϧλΠϜͰDI͞ΕΔ • Playͷίϯτϩʔϥcake componentͱͯ͠߹
DDDͷ࣮ફ: ·ͱΊ • ࣾษڧձͰʰ࣮ફDDDʱΛྠಡɺݟΛڞ༗ • զʑʹͱͬͯͪΐ͏ͲΑ͍DDDΛݟ͚ͭΔ • ScalaͷྗΛ׆͔࣮ͨ͠ύλʔϯͷൃݟ
·ͱΊ • ۜͷؙͳ͍! • ཁ݅ʹ͍͋ͬͯΔ͔ߟ͑Δ • ཁ݅ʹ͋ΘͤͯΞϨϯδ͍ͯ͘͠ • ʮͪΐ͏Ͳ͍͍DDDʯ •
ʮͪΐ͏Ͳ͍͍ϚΠΫϩαʔϏεʯ • ࣌ʹࣃΛ৯͍͠Δ