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

イベント駆動マイクロサービスアーキテクチャ / Event-Driven Microservi...

disc99
April 27, 2019

イベント駆動マイクロサービスアーキテクチャ / Event-Driven Microservices Architecture

disc99

April 27, 2019
Tweet

More Decks by disc99

Other Decks in Technology

Transcript

  1. ਐΊํ w લ൒dཧ࿦ฤd w ϦΫΤετϨεϙϯεͷ՝୊ w ΠϕϯτۦಈΞʔΩςΫνϟ w Πϕϯτϕʔε࿈ܞ w

    υϝΠϯΠϕϯτ w ৗறܕͱΠϕϯτۦಈܕ w τϥϯβΫγϣϯ w ޙ൒d࣮ફฤd w BTPWJFXϙΠϯταʔϏε w ઓུ w ઓज़
  2. υϝΠϯΠϕϯτ w υϝΠϯʹؔ͢Δग़དྷࣄΛද͢σʔλ w ଟ͘Ϗδωεϧʔϧ͕ద߹͢ΔՄೳੑ͕͋Δ w ʙͱ͖ɺ΋ͦ͠͏ͳͬͨΒʙɺʙͷ৔߹ɺͳͲ w ιʔείʔυͱϏδωεϩδοΫͷҰக w

    ϚΠΫϩαʔϏε ڥք͚ͮΒΕͨίϯςΩετ ͔Βൃੜ͢ΔΠϕϯτ΋υϝΠϯΠϕϯτͱͳΔ Մೳੑ͕ߴ͍ w ϚΠΫϩαʔϏεؒͷ࿈ܞʹޮՌత
  3. ϝοηʔδϒϩʔΧʔ w ඇಉظͰΠϕϯτͱͳΔσʔλΛϝοηʔδͱͯ͠഑৴ w αʔϏεؒͰΦϒβʔόύλʔϯͷ࣮ݱ w ΠϕϯτόεɺΠϕϯτϓϩηοαʔɺϝοηʔδΩϡʔͳͲͱ΋ݺ͹ΕΔ w ୅දతͳ੡඼ w

    3BCCJU.2ɺ,BGLBɺ"NB[PO424ɺ(PPHMF1VC4VCɺ"[VSF&WFOU )VCT w ੡඼ʹΑͬͯɺಛੑ͕͋ΔͷͰ༻్ʹ߹Θͤͯબ୒ w ॏෳഉআɺॱংอূɺ༰ྔ੍ݶɺӬଓԽɺύϑΥʔϚϯεɺϓϩτίϧɺ ϦτϥΠʜ
  4. ৗறܕͱΠϕϯτۦಈܕ w ΠϕϯτۦಈॲཧΛ࣮ߦͰ͖ΔϥϯλΠϜʹΑΓ αʔόͷΑ͏ͳৗற؀ڥ͕ෆཁʹͳΔ w Πϕϯτۦಈ'BB4ͷ׆༻ w ίετɺεέʔϥϏϦςΟͷ޲্
 ྫ "84-BNCEB


      ݄݅ɺ(#ඵ·Ͱແྉ
 σϑΥϧτಉ࣮࣌ߦ਺ w όον΋Πϕϯτۦಈʹஔ͖׵͑Մೳ w ߋʹ'BB4ӡ༻ $/$'4FSWFSMFTT8IJUFQBQFSW
  5. 4BHB w ϚΠΫϩαʔϏεͰ෼ࢄτϥϯβΫγϣϯ ಛʹϑΣʔζίϛο τ ͸σϝϦοτ͕ଟ͍ w ඇಉظϝοηʔδϯάʹΑΓαʔϏεؒͰϫʔΫϑϩʔΛߏ ஙɺσʔλͷҰ؏ੑΛҡ࣋ w

    τϥϯβΫγϣϯϝοηʔδΛར༻͠ɺଞαʔϏεΛॱ࣍ߋ ৽ w ݁Ռ͸Ԡ౴ϝοηʔδʹͯऔಘ w Τϥʔ࣌͸ิਖ਼τϥϯβΫγϣϯ Πϕϯτ ʹΑΔϩʔϧ όοΫ w ݁Ռ੔߹ੑΛڐ༰͢Δ͜ͱͰɺૄ݁߹ͳαʔϏεؒτϥϯβΫ γϣϯΛ࣮ݱ͕Ͱ͖Δ͕ෳࡶੑ΋্͕Δ
  6. 4BHB ίϨΦάϥϑΟܕ w ϝϦοτ w Πϕϯτൃߦ͕γϯϓϧ w αʔϏεಉ͕͓࢜ޓ͍Λ஌Δඞཁ͕ͳ͘ૄ݁߹ w σϝϦοτ

    w 4BHBͷશମ೺Ѳ͕ࠔ೉ w ॥؀ࢀরͷѻ͍͕೉͍͠ w খن໛ͳ4BHBͰ͋Ε͹ޮՌత w ෳࡶͳτϥϯβΫγϣϯΛ؅ཧ͢Δʹ͸ΦʔέετϨʔ γϣϯܕ͕޲͍͍ͯΔ ύϒϦογϡ αϒεΫϥΠϒ
  7. 4BHB ΦʔέετϨʔγϣϯܕ w ϝϦοτ w ॥؀ґଘ͕ͳ͘γϯϓϧ w 4BHBͷશମΛ؅ཧͰ͖Δ w ֤αʔϏε͸࣮ߦฦ৴ͷΈʹͳΓෳࡶੑ͕Լ͕Δ

    w ϩʔϧόοΫ͕༰қ w σϝϦοτ w ΦʔέετϨʔλʔʹϏδωεϩδοΫ͕ूத͠΍͍͢
 ʢີ݁߹ͯ͠͠·͏Մೳੑ͕͋Δʣ w ΦʔέετϨʔλʔΛ׬݁ʹఆٛͰ͖Δ࢓૊Έ͕ඞཁ w খن໛ͳ4BHBҎ֎Ͱ͸༗ޮͳՄೳੑ͕ߴ͍ ύϒϦογϡ αϒεΫϥΠϒ ᶃ ᶄ ᶅ ᶆᶊᶎ ᶇ ᶈ ᶉ ᶍ ᶌ ᶋ
  8. ϙΠϯτͱΠϕϯτۦಈΞʔΩςΫνϟ Πϕϯτۦಈ આ໌ ஷΊΔ ˓ ɾߋ৽ॲཧͰՄ༻ੑ͕ॏཁ ɾϦΞϧλϜੑ͸௿͍ ࢖͏ ˚ ɾߋ৽ॲཧͰՄ༻ੑ͕ॏཁ

    ɾόϦσʔγϣϯ΍τϥϯβΫγϣϯ΋ॏཁ ɾϦΞϧλΠϜੑ͸ൺֱతߴ͍
 ʢϏδωεཁ݅࣍ୈڐ༰͞ΕΔՄೳੑ͋Γʣ ֬ೝ͢Δ º ɾࢀরॲཧͰɺো֐Λڐ༰Ͱ͖ΔՄೳੑ͕͋Δ ɾϨΠςϯγʔͱϦΞϧλϜੑ͕ॏཁ
  9. ϙΠϯταʔϏε w ձһͷϙΠϯτػೳ ஷΊΔɾ࢖͏ɾ֬ೝ͢Δ Λѻ͏
 %%%ͷڥք͚ͮΒΕͨίϯςΩετ  w ஷΊΔ৚݅ͷ؅ཧػೳΛ࣋ͪɺࠓޙ΋֦ு͍ͯ͘͠ w

    ྫձһొ࿥ϙΠϯτɺ஍ҬݶఆϙΠϯτɺϙΠϯτΩϟϯϖʔϯ  w ϙΠϯτΛ׆༻ͨ͠ࢪࡦ࣮ݱͷͨΊɺ֦ுੑ΍ॊೈੑ͕ٻΊΒΕΔ ࢀߟྫͰࠓޙͷαʔϏεల։Λ໿ଋ͢Δ΋ͷͰ͸͋Γ·ͤΜ
  10. ϙΠϯταʔϏεͷϙΠϯτ w ֦ுੑɾૄ݁߹ w ֤αʔϏε͸ϙΠϯτͷίΞυϝΠϯʢఏڙ৚݅ɺܭࢉϩδοΫʣΛ஌Δඞཁ͕ͳ͍ w ֤αʔϏε͸ࣗ਎ͷυϝΠϯΠϕϯτΛύϒϦογϡ͢Δ͜ͱΛܖ໿ w طʹΠϕϯτ͕ఏڙ͞Ε͍ͯΕ͹ɺϙΠϯταʔϏε୯ମͰαʔϏεͷػೳ֦ு͕Մೳ w

    Մ༻ੑ w ඇಉظ࿈ܞͷͨΊϙΠϯταʔϏε͕ఀࢭ͍ͯͯ͠΋֤αʔϏε΁ͷӨڹΛ࠷খݶʹͰ͖Δ w ো֐෮ؼͨ࣌͠఺ͰΠϕϯτͷ࠶ಡΈࠐΈ͕Մೳ w ಉظॲཧ͸αʔϏεϨϕϧͷ(SBDFGVM%FHSBEBUJPOΛҙࣝ͢Δ w ϙΠϯτ֬ೝͳͲͷಉظ ߴτϥϑΟοΫͳॲཧ͸ݺͼग़͠ݩͰରࡦΛݕ౼
  11. 4JOHMFTPVSDFPGUSVUI w ୯Ұͷ(JUϦϙδτϦʹ֤αʔϏεؒͷΠϯλʔϑΣΠεΛ 1SPUPDPM#V⒎FSTͰఆٛ w ίϯγϡʔϚʔܖ໿ͷ࣮ݱ w Πϕϯτϕʔε ඇಉظ NFTTBHFͱͯ͠ఆٛ

    w ϦΫΤετϨεϙϯε ओʹಉظ H31$ͷTFSWJDFͱͯ͠ఆٛ w $*$%ʹΑΔϥΠϒϥϦ഑৴ w ϦϙδτϦʹϓογϡ͢Δͱ$*$%Λ࣮ߦ͠ϥΠϒϥϦͱͯ͠ ࣗಈ഑৴ w ഑৴͞ΕͨϥΠϒϥϦΛಡΈࠐΉ͚ͩͰαʔϏεؒ࿈ܞΛૉ ૣ͘ɾ҆શʹ࣮ݱ ࣗಈੜ੒
  12. 1SPUPDPM#V⒎FST w γϯϓϧ w ੩తܕ෇͚ w ଟݴޠରԠ w ܰྔ w

    ϝοηʔδϯάϛυϧ΢ΣΞʹ͸༰ྔ੍ݶ͕͋Δ ৔߹͕ଟ͍ w ࣄۀ੒௕ɺϢʔβ૿ՃʹΑΓϝοηʔδ਺͸૿Ճ ͢Δ w +40/4DIFNBΑΓϝϦοτ͕େ͖͔ͬͨ message PlanReservedEvent { string event_id = 1; string event_type = 2; google.protobuf.Timestamp event_create_time = 3; int32 member_id = 4; int32 plan_no = 5; int32 price = 6; // ... }
  13. 4QSJOH$MPVE4USFBN w ࣾ಺ͷΞϓϦέʔγϣϯ͸΄΅+BWB 4QSJOH#PPU  w 4QSJOHͷΠϕϯτۦಈϚΠΫϩαʔϏεϑϨʔϜϫʔ Ϋ w ύϒϦογϟʔαϒεΫϥΠόʹΑΔΠϕϯτ഑

    ৴ w ίϯγϡʔϚʔάϧʔϓ w ύʔςΟγϣϯαϙʔτ w 4QSJOH#PPUͱͷ౷߹ 4PVSDF 4JOL 1SPDFTTFS .FTTBHF #SPLFS 4PVSDFc1SPDFTTFSc4JOL .FTTBHF #SPLFS
  14. ύϒϦογϟʔͱαϒεΫϥΠόͷ࣮૷ w ύϒϦογϟʔ w ഑৴͞ΕͨϥΠϒϥϦ 1SPUPDPM #V⒎FST ͷΠϕϯτੜ੒ w 4QSJOH$MPVE4USFBNʹΑΓ

    .FTTBHFͱͯ͠ૹ৴ @EnableBinding(Source.class) class Publisher { Source source; public void publish(Plan plan) { var event = PlanReservedEvent.newBuilder() .setEventId(UUID.randomUUID().toString()) .setEventType(PlanReservedEvent.getDescriptor().getFullName()) .setEventCreateTime(now()) .setMemberId(plan.memberId()) // ... .build(); var message = MessageBuilder.withPayload(event) .setHeader("eventType", eventType(event)) .build(); source.output().send(message); } }
  15. ύϒϦογϟʔͱαϒεΫϥΠόͷ࣮૷ w αϒεΫϥΠό ϙΠϯταʔϏε  w ഑৴͞ΕͨϥΠϒϥϦ 1SPUPDPM #V⒎FST ͷΠϕϯτλΠϓͰϑΟϧλʔ

    w Πϕϯτ୯ҐͰυϝΠϯϩδοΫΛ࣮૷ @EnableBinding(Sink.class) class Subscriber { @StreamListener(value=Sink.INPUT, condition="headers['eventType']=='plan.v1.PlanReservedEvent'") public void handle(PlanReservedEvent event) { // ... } @StreamListener(value=Sink.INPUT, condition="headers['eventType']=='plan.v1.PlanChangedEvent'") public void handle(PlanChangedEvent event) { // ... } @StreamListener(value=Sink.INPUT, condition="headers['eventType']=='plan.v1.PlanCanceledEvent'") public void handle(PlanCanceledEvent event) { // ... } }
  16. υϝΠϯۦಈઃܭ w ࣾ಺Ͱ޿͘࠾༻͞Ε͍ͯΔ w αʔϏε͝ͱͷ੹຿ͷࢦ਑ ڥք͚ͮΒΕͨίϯςΩετ  w νʔϜ։ൃͰઃܭࢥ૝ʹڞ௨ೝࣝΛ࣋ͪ΍͍͢ w

    ϙΠϯτʹؔ͢ΔෳࡶͳϏδωεϩδοΫɺϧʔϧΛૉ௚ʹදݱͰ͖Δ w มߋ༰қੑͷ޲্ w υϝΠϯΠϕϯτͱ૬ੑ͕ྑ͍
  17. $234ͱΠϕϯτιʔγϯά w ࣄۀ֦େͱڞʹϙΠϯτͷ෇༩ϩδοΫ͸ෳࡶʹͳΓɺޙ͔ΒͰ΋मਖ਼͕ඞཁ w ෇༩཰ºձһଐੑº঎඼ºՁ֨ºൢച۠෼ºΩϟϯϖʔϯºߪೖཤྺºʜ w ಡΈࠐΈॲཧ͸֤ϖʔδ୯ҐͰݺͼग़͞ΕΔͨΊɺߴτϥϑΟοΫ w ձһͷอ࣋ϙΠϯτɺߪೖ࣌֫ಘͰ͖ΔϙΠϯτ w

    ձһ͕֬ೝ͢ΔҎ֎ʹ΋ܦཧ΍؂ࠪͷҝʹɺաڈͷཤྺ΋ؚΊϙΠϯτૢ࡞Λ࢒ ͓ͯ͘͠ඞཁ͕͋Δ w σʔλ͕ΠϕϯτϕʔεͰ࿈ܞ͞Ε͍ͯΔ ෭࡞༻ͷ͋ΔෳࡶͳϏδωεϩδοΫʹରͯ͠ɺ$234ͷ $PNNBOE͕༗ޮ
  18. $234ͱΠϕϯτιʔγϯά w ࣄۀ֦େͱڞʹϙΠϯτͷ෇༩ϩδοΫ͸ෳࡶʹͳΓɺޙ͔ΒͰ΋मਖ਼͕ඞཁ w ෇༩཰ºձһଐੑº঎඼ºՁ֨ºൢച۠෼ºΩϟϯϖʔϯºߪೖཤྺºʜ w ಡΈࠐΈॲཧ͸֤ϖʔδ୯ҐͰݺͼग़͞ΕΔͨΊɺߴτϥϑΟοΫ w ձһͷอ࣋ϙΠϯτɺߪೖ࣌֫ಘͰ͖ΔϙΠϯτ w

    ձһ͕֬ೝ͢ΔҎ֎ʹ΋ܦཧ΍؂ࠪͷҝʹɺաڈͷཤྺ΋ؚΊϙΠϯτૢ࡞Λ࢒ ͓ͯ͘͠ඞཁ͕͋Δ w σʔλ͕ΠϕϯτϕʔεͰ࿈ܞ͞Ε͍ͯΔ ύϑΥʔϚϯεΛཁٻ͞ΕΔ৔໘Ͱ͸ɺ$PNNBOEͱ 2VFSZΛ෼ׂ͠ɺνϡʔχϯάͷ༨஍Λ֬อ
  19. $234ͱΠϕϯτιʔγϯά w ࣄۀ֦େͱڞʹϙΠϯτͷ෇༩ϩδοΫ͸ෳࡶʹͳΓɺޙ͔ΒͰ΋मਖ਼͕ඞཁ w ෇༩཰ºձһଐੑº঎඼ºՁ֨ºൢച۠෼ºΩϟϯϖʔϯºߪೖཤྺºʜ w ಡΈࠐΈॲཧ͸֤ϖʔδ୯ҐͰݺͼग़͞ΕΔͨΊɺߴτϥϑΟοΫ w ձһͷอ࣋ϙΠϯτɺߪೖ࣌֫ಘͰ͖ΔϙΠϯτ w

    ձһ͕֬ೝ͢ΔҎ֎ʹ΋ܦཧ΍؂ࠪͷҝʹɺաڈͷཤྺ΋ؚΊϙΠϯτૢ࡞Λ࢒ ͓ͯ͘͠ඞཁ͕͋Δ w σʔλ͕ΠϕϯτϕʔεͰ࿈ܞ͞Ε͍ͯΔ աڈͷཤྺΛશͯ࣋ͪɺݱࡏঢ়ଶΛ࣮ݱ͢Δ͜ͱͰɺࠓޙ ͷ֦ு΍ɺ৴པੑΛ୲อ
  20. ෭࡞༻ͷগͳ͍ΦϒδΣΫτࢦ޲ w +BWB ΦϒδΣΫτࢦ޲ ঢ়ଶมԽ ෭࡞༻ ͱ ͸ݶΒͳ͍ w ΦϒδΣΫτࢦ޲Ͱ΋Πϛϡʔλϒϧͳઃܭ

    ͸ॏཁ w ΠϛϡʔλϒϧͳΦϒδΣΫτ w ΠϕϯτɺϞσϧɺόϦϡʔΦϒδΣΫ τɺϑΝʔετΫϥείϨΫγϣϯʜ w ύϑΥʔϚϯεݒ೦͕͋Ε͹ঢ়ଶมԽΛڐ༰ class Point { Integer value; Point(Integer value) { this.value = value; } // void add(Point point) { // value = value + point.value; // } Point add(Point point) { return new Point(value + point.value); } } มߋ͞ΕͨΦϒδΣΫτΛ৽͘͠ฦ͢ ঢ়ଶมԽΛߦ͏ϝιου͸࢖༻͠ͳ͍
  21. γϯϓϧͰࣺͯ΍͍͢ΞʔΩςΫνϟ w ࣄۀ੒௕ͷ଎౓Λߟ͑Δͱૣ͚Ε͹ɺ೥ɺେ఍೥΋ܦͭͱϨΨγΟγεςϜͱͳΓ΍͍͢ w ࣜ೥ભٶΛલఏʹ͓ͯ͘͠
 ෆ࣮֬ੑͷߴ͍௕ظతϝϯςφϯεੑΑΓɺϨΨγΠԽͨ͠ͱ͖ͷࣺͯ΍͢͞  w ڞ༗ϥΠϒϥϦɺڞ༗%#ͷېࢭ w

    $234΍ΠϕϯτιʔγϯάΛ+BWBͰ࣮ݱ͢ΔͳΒ"YPO'SBNFXPSL΍"LLBɺ&WFOUVBUFͳͲ͕બ୒ࢶ w લఏ஌ࣝɺֶशίετ͕ߴ͘ɺ্ཱͪ͛ɺӡ༻໘ͰΦʔόʔϔου͕ߴ͘ɺҰ౓ґଘ͢Δͱޙ໭Γ΋೉͍͠ w ࠷খݶͷػೳͷΈࣗલͰ༻ҙ w $234΍Πϕϯτιʔγϯάͷલఏ஌͕ࣝͳ͍ਓ΋গͳ͘ͳ͍ w 3FBEͱ8SJUFͷ෼཭ɺΠϕϯτϋϯυϥʔɺύλʔϯϚονɺϦϓϨΠͳͲ
  22. ࢀߟ w ϚΠΫϩαʔϏεΞʔΩςΫνϟ 4BN/FXNBO  w .JDSPTFSWJDFT1BUUFSOT $ISJT3JDIBSETPO  w

    ࣮ઓυϝΠϯۦಈઃܭ 7BVHIO7FSOPO  w ਐԽతΞʔΩςΫνϟ /FBM'PSEɺ3FCFDDB1BSTPOTɺ1BUSJDL,VB  w ෼ࢄγεςϜσβΠϯύλʔϯ #SFOEBO#VSOT  w 4PGUXBSF"SDIJUFDUVSF1BUUFSOT .BSL3JDIBSET