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
210
好きな技術《コト》で、 生きていく技術 / life with what you like
aereal
5
2.4k
qron: Cloud Native Cron Alternativeの今
aereal
2
2.1k
自動作曲入門 / 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.4k
Other Decks in Programming
See All in Programming
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
fs2-io を試してたらバグを見つけて直した話
chencmd
0
220
Security_for_introducing_eBPF
kentatada
0
110
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
710
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
2
460
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
return文におけるstd::moveについて
onihusube
1
690
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
Jakarta EE meets AI
ivargrimstad
0
230
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
BBQ
matthewcrist
85
9.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Facilitating Awesome Meetings
lara
50
6.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Making Projects Easy
brettharned
116
5.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
160
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ʯ •
ʮͪΐ͏Ͳ͍͍ϚΠΫϩαʔϏεʯ • ࣌ʹࣃΛ৯͍͠Δ