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

PHPerでもできる!マイクロサービス

 PHPerでもできる!マイクロサービス

PHPerKaigi2022 day2 trackB
近年注目を集めるマイクロサービスアーキテクチャ。GoやScalaのような華々しい言語で実装した話はよく語られていますが、もちろん我々PHPerの愛するPHPでもできるんです!
私は2018年〜PHPシステムのマイクロサービス化プロジェクトを始め、2021年現在もPHP製マイクロサービスを運用・進化させています。PHPシステムの中でマイクロサービスに向く機能・PHPマイクロサービスをどうやって作って運用するかまでお話しします。
【お品書き】
(1)マイクロサービスとは?
(2)PHPでマイクロサービスの作り方(設計、コード)
(3)AWSでPHPのマイクロサービスを動かすための仕組み

Hiromi Hishida

April 11, 2022
Tweet

More Decks by Hiromi Hishida

Other Decks in Programming

Transcript

  1. #Z!XFCUI"QSJM BU1)1FS,BJHJ
    1)1FSͰ΋Ͱ͖Δʂ
    ϚΠΫϩαʔϏε

    View full-size slide

  2. w 1)1ྺ೥ɻϑϦʔϥϯεˠΧϧςοτί
    ϛϡχέʔγϣϯζ։ൃ෦ɺݱࡏόοΫΤ
    ϯυνʔϜͷϓϨΠϯάϚωʔδϟʔɻ࠷
    ۙ͸ਓͷνʔϜͰ4ZNGPOZͱ
    "OHVMBSΛ࢖ͬͯεΫϥϜ։ൃͯ͠·
    ͢ɻ
    w 4ZNGPOZUIF'BTU5SBDL೔ຊޠ຋༁νʔ
    ϜࢀՃɺ4ZNGPOZ8PSMEొஃɻ࠷ॳʹ࢖
    ͑ͨϑϨʔϜϫʔΫ͕4ZNGPOZͩͬͨλΠ
    ϓɻ
    w ʮਓʹ࢖ΘΕΔγεςϜʯΛ࡞Γͨͯ͘ɺ
    ϝϯςφϏϦςΟͷߴ͍։ൃΛ໨ࢦͯ͠ν
    ʔϜϝϯόʔͱҰॹʹ೔ʑฃಆதʂҰॹʹ
    ؤுΓ͍ͨ஥ؒืूதʂ
    w NPUIFSPG

    View full-size slide

  3. ͜ͷUBMLͷର৅
    w ࣗࣾαʔϏεΛ࢝Ίͱ͢Δɺॳճ࡞ͬͨޙʹൺֱత௕ظؒӡ༻·ͰؔΘΔ
    8FCΞϓϦέʔγϣϯΛ։ൃ͍ͯ͠Δ1)1FS
    w ʢ௥ՃͰʣͳͥϚΠΫϩαʔϏεΞʔΩςΫνϟ͕஫໨͞ΕΔΑ͏ʹͳͬͨͷ
    ͔஌Γ͍ͨ1)1FS

    View full-size slide

  4. ϚΠΫϩαʔϏεͱ͸

    View full-size slide

  5. lϚΠΫϩαʔϏε͸ɺڠௐͯ͠ಈ࡞͢Δখ
    ن໛Ͱࣗ཯తͳαʔϏεͰ͢ɻz

    View full-size slide

  6. lϚΠΫϩαʔϏε͸ɺڠௐͯ͠ಈ࡞͢Δখ
    ن໛Ͱࣗ཯తͳαʔϏεͰ͢ɻz

    View full-size slide

  7. ڠௐ
    w ݱ୅ͷ8FCΞϓϦέʔγϣϯͰ͸୯Ұͷ࢓ࣄͰϏδωεతՁ஋͕ग़Δػೳ͕
    ׬੒͢Δ͜ͱ͸΄ͱΜͲͳ͍

    View full-size slide

  8. খن໛
    w िؒͰॻ͖௚ͤΔཻ౓

    View full-size slide

  9. ࣗ཯త
    w ಠཱͯ͠ϦϦʔεͰ͖Δ
    w ಠཱͯ͠มߋͰ͖Δ
    w ٕज़ʹґଘ͠ͳ͍"1*Ͱͭͳ͕Δ

    View full-size slide

  10. ྫωοτγϣοϓͷʮ஫จड෇ʯ
    ΧʔτJO
    ߪೖϘλϯ
    Άͪͬͱ
    ஫จσʔλ
    ొ࿥
    ܾࡁ
    ஫จ͓ྱ
    ϝʔϧૹ৴
    ड஫
    ϝʔϧૹ৴
    ࡏݿσʔλ
    ߋ৽

    View full-size slide

  11. ྫωοτγϣοϓͷʮ஫จड෇ʯ
    ΧʔτJO
    ߪೖϘλϯ
    Άͪͬͱ
    ஫จσʔλ
    ొ࿥
    ܾࡁ
    ஫จ͓ྱ
    ϝʔϧૹ৴
    ड஫
    ϝʔϧૹ৴
    ࡏݿσʔλ
    ߋ৽

    View full-size slide

  12. ྫωοτγϣοϓͷʮ஫จड෇ʯ
    ΧʔτJO
    ߪೖϘλϯ
    Άͪͬͱ
    ஫จσʔλ
    ొ࿥
    ܾࡁ
    ஫จ͓ྱ
    ϝʔϧૹ৴
    ड஫
    ϝʔϧૹ৴
    ࡏݿσʔλ
    ߋ৽

    View full-size slide

  13. ϞϊϦε
    w ϚΠΫϩαʔϏεʹରͯ͠ɺैདྷͷͭͷγεςϜ಺ʹ͢΂ͯͷػೳΛ૊Έࠐ
    ΜͰ։ൃ͞ΕͨϓϩδΣΫτΛݺͿ

    View full-size slide

  14. ϚΠΫϩαʔϏεͰԿ͕خ͍͠
    ͔ʁ

    View full-size slide

  15. ϚΠΫϩαʔϏεͰԿ͕خ͍͔͠ʁ
    w ٕज़తҟ࣭ੑ
    w ճ෮ੑ
    w εέʔϦϯά
    w σϓϩΠ༰қੑ
    w ަ׵Մೳੑ

    View full-size slide

  16. ϚΠΫϩαʔϏεͰԿ͕خ͍͔͠ʁ
    w ٕज़తҟ࣭ੑ
    w ճ෮ੑ
    w εέʔϦϯά
    w σϓϩΠ༰қੑ
    w ަ׵Մೳੑ
    ϝΠϯγεςϜ͕1)1͔ͩΒͬͯɺαʔϏε·Ͱ1)1͡Όͳͯ͘΋͍͍
    ো֐ͷൣғ͕ݶఆͰ͖ΔɻͭͷαʔϏε͕མͪͯ΋ଞͷॲཧʹ͸ӨڹΛٴ΅͞ͳ͍
    ۃ୺ʹෛՙͷߴ͍αʔϏε͚ͩߴੑೳͷαʔόʔΛར༻Ͱ͖Δɻॲཧ͢΂͖݅਺͕
    ଟ͍αʔϏε͚ͩଟ਺ىಈͰ͖Δ
    શମͰͳͭ͘ͷαʔϏε͚ͩσϓϩΠͰ͖Δ
    ͭͷαʔϏε͚ͩத਎ΛೖΕସ͑Δ͜ͱ͕Ͱ͖Δ

    View full-size slide

  17. ϚΠΫϩαʔϏεͰԿ͕خ͍͔͠ʁ
    w ಠཱͨ͠αʔόʔʹ͋ΔͷͰ
    w ͚ͦͩ͜ผͷ1)1όʔδϣϯͰ΋ྑ͍͠ɺผͷݴޠͰ΋ྑ͍
    w ͚ͦͩ͜εέʔϦϯάͰ͖Δ
    w શମΛσϓϩΠ͠ͳͯ͘΋͚ͦͩ͜ߋ৽Ͱ͖Δ

    View full-size slide

  18. 1)1ͰϚΠΫϩαʔϏεͷ࡞Γํ

    View full-size slide

  19. 1)1ͰϚΠΫϩαʔϏεͷ࡞Γํ

    View full-size slide

  20. 1)1ͰϚΠΫϩαʔϏεͷ࡞Γํ
    ϚΠΫϩαʔϏεԽ͕ඞཁͳ෦෼ͱඞཁͳ͍෦෼ɺϚΠΫϩαʔϏεʹ޲͘
    ෦෼ͱ޲͔ͳ͍෦෼Λߟ͑Δ
    ϚΠΫϩαʔϏεԽ͢Δ෦෼ΛʮϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ʯίʔυʹ͢
    Δ
    ϝΠϯγεςϜͱϚΠΫϩαʔϏεͱͷ઀ଓ෦෼ΛԿʹ͢Δ͔ߟ͑Δ
    ϚΠΫϩαʔϏε෦෼ΛϝΠϯγεςϜͱ͸ผͷαʔόʔɾϚγϯʹσϓϩ
    Π͢Δ

    View full-size slide

  21. ࡞ΓํͲͷ෦෼ΛϚΠΫϩαʔϏεԽ͢Δ͔
    w ͢΂ͯΛϚΠΫϩαʔϏεԽ͢΂͖ͱ͍͏Θ͚Ͱ͸ͳ͍
    w ͨͱ͑͹ɺϢʔβʔ΁ͷը໘্ͰͷϦΞϧλΠϜԠ౴͕ඞཁͳॲཧ͸͋·Γ
    ޲͔ͳ͍
    w σʔλϕʔεͷσʔλΛ୯७ʹߋ৽͢Δ͚ͩͷॲཧ΋޲͔ͳ͍

    View full-size slide

  22. ࡞ΓํͲͷ෦෼ΛϚΠΫϩαʔϏεԽ͢Δ͔
    ΧʔτJO
    ߪೖϘλϯ
    Άͪͬͱ
    ஫จσʔλ
    ొ࿥
    ܾࡁ
    ஫จ͓ྱ
    ϝʔϧૹ৴
    ड஫
    ϝʔϧૹ৴
    ࡏݿσʔλ
    ߋ৽

    View full-size slide

  23. ࡞ΓํϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ίʔυʹ͢Δ
    w EPNBJO͚ͩͰͳ࣮͘ࡍʹಈ͘TFSWJDF෦෼΋ϑϨʔϜϫʔΫ͔Βಠཱͤ͞Δ
    w ϝΠϯγεςϜͷ֎ʹஔ͍ͯFYFDͰಈ͘ͷ͕Ұ୴ͷΰʔϧ

    View full-size slide

  24. ࡞ΓํϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ίʔυʹ͢Δ
    TSD%PNBJO1BZNFOU.BLF$SFEJU$BSE1BZNFOUQIQ
    TSD%PNBJO4FOE5IBOL:PV.BJM4FOE5IBOL:PV.BJMQIQ
    TSD%PNBJO4FOE4IJQQJOH.BJM4FOE4IJQQJOH.BJMQIQ

    View full-size slide

  25. ࡞ΓํϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ίʔυʹ͢Δ
    TSD%PNBJO1BZNFOU.BLF$SFEJU$BSE1BZNFOUQIQ
    TSD%PNBJO4FOE5IBOL:PV.BJM4FOE5IBOL:PV.BJMQIQ
    TSD%PNBJO4FOE4IJQQJOH.BJM4FOE4IJQQJOH.BJMQIQ

    View full-size slide

  26. ࡞ΓํϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ίʔυʹ͢Δ
    QBUIUPTIPQQJOH@BQQTSD%PNBJO1BZNFOU.BLF$SFEJU$BSE1BZNFOUQIQ
    QIQ
    OBNFTQBDF"QQa%PNBJOa1BZNFOU
    ʜ
    DMBTT.BLF$SFEJU$BSE1BZNFOU
    \
    QVCMJDGVODUJPO@@DPOTUSVDU QSJWBUF$IBSHF4FSWJDFDIBSHF4FSWJDF
    \^
    QVCMJDGVODUJPONBLF1BZNFOU TUSJOHDBSE5PLFO JOUBNPVOU TUSJOHEFUBJMb`
    WPJE
    \
    ʜ
    ^
    ^

    View full-size slide

  27. ࡞ΓํϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ίʔυʹ͢Δ
    QBUIUPNJSDSPTFSWJDF@UNQNBLF@DSFEJU@DBSE@QBZNFOUTSD%PNBJO1BZNFOU.BLF$SFEJU$BSE1BZNFOUQIQ
    QIQ
    OBNFTQBDF"QQa%PNBJOa1BZNFOU
    ʜ
    DMBTT.BLF$SFEJU$BSE1BZNFOU
    \

    QVCMJDGVODUJPO@@DPOTUSVDU QSJWBUF$IBSHF4FSWJDFDIBSHF4FSWJDF
    \^
    QVCMJDGVODUJPONBLF1BZNFOU TUSJOHDBSE5PLFO JOUBNPVOU TUSJOHEFUBJMb`
    WPJE
    \
    ʜ
    ^
    ^

    View full-size slide

  28. ࡞ΓํϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ίʔυʹ͢Δ
    QBUIUPNJDSPTFSWJDF@UNQNBLF@DSFEJU@DBSE@QBZNFOUSVOQIQ
    QIQ
    ʜ
    SFRVJSF@@%*3@@`WFOEPSBVUPMPBEQIQ`
    TFSWJDFOFX.BLF$SFEJU$BSE1BZNFOU OFX$IBSHF4FSWJDF OFX4USJQF$MJFOU @&/7



    DBSE5PLFOBSHW<>
    BNPVOUBSHW<>
    EFUBJMBSHW<>
    TFSWJDFNBLF1BZNFOU DBSE5PLFO BNPVOU EFUBJM

    View full-size slide

  29. ࡞ΓํϑϨʔϜϫʔΫͷ֎Ͱ΋ಈ͘ίʔυʹ͢Δ
    QBUIUPTIPQQJOH@BQQTSD%PNBJO1BZNFOU.BLF$SFEJU$BSE1BZNFOUQIQ
    QIQ
    OBNFTQBDF"QQa%PNBJOa1BZNFOU
    ʜ
    DMBTT.BLF$SFEJU$BSE1BZNFOU
    \
    QVCMJDGVODUJPONBLF1BZNFOU TUSJOHDBSE5PLFO JOUBNPVOU TUSJOHEFUBJMb`
    WPJE
    \
    FYFD TQSJOUG bQBUIUPNJDSPTFSWJDF@UNQNBLF@DSFEJU@DBSE@QBZNFOUSVOQIQTET` DBSE5PLFO BNPVOU EFUBJM


    ^
    ^

    View full-size slide

  30. ࡞Γํ઀ଓ෦෼Λߟ͑Δ
    w )551"1*͔Ωϡʔ͔ΠϕϯταϒεΫϥΠϒ͔

    View full-size slide

  31. ࡞Γํ઀ଓ෦෼Λߟ͑Δ
    w )551"1*
    w ϝΠϯγεςϜଆʹଈ࣌ϨεϙϯεΛฦͤΔ
    w ॏ͍ॲཧʹ͸޲͔ͳ͍

    View full-size slide

  32. ࡞Γํ઀ଓ෦෼Λߟ͑Δ
    w Ωϡʔ
    w ॏ͍ॲཧ΋Ͱ͖Δ
    w ϝΠϯγεςϜଆ͔Βݪଇͱͯͭ͠ͷϚΠΫϩαʔϏε͔͠ىಈͰ͖ͳ͍
    ʢෳ਺ͷ࢓ࣄ͕ϚΠΫϩαʔϏεͱ࣮ͯ͠૷͞Ε͍ͯΔ৔߹ɺॲཧ͕ऴΘͬ
    ͨΒ࣍ΛΩϡʔ͢ΔܗͰ௚ྻʹॲཧ͢Δ͜ͱʹͳΔʣ
    w ϨεϙϯεΛฦ͢͜ͱ͸Ͱ͖ͳ͍ͷͰɺϝΠϯγεςϜଆͰԿΒ͔ͷ݁Ռ͕
    ඞཁͳͱ͖͸݁Ռ௨஌ͷ࢓૊Έ͕ඞཁ

    View full-size slide

  33. ࡞Γํ઀ଓ෦෼Λߟ͑Δ
    w ΠϕϯταϒεΫϥΠϒ
    w ॏ͍ॲཧ΋Ͱ͖Δ
    w ϝΠϯγεςϜଆ͔Βෳ਺ͷϚΠΫϩαʔϏεΛฒྻͯ͠ىಈͤ͞Δ͜ͱ͕
    Ͱ͖Δ
    w ϨεϙϯεΛฦ͢͜ͱ͸Ͱ͖ͳ͍ͷͰɺϝΠϯγεςϜଆͰԿΒ͔ͷ݁Ռ͕
    ඞཁͳͱ͖͸݁Ռ௨஌ͷ࢓૊Έ͕ඞཁ

    View full-size slide

  34. ࡞Γํ઀ଓ෦෼Λߟ͑Δ
    w ͜ͷαʔϏε͸ԿΛ஌͍ͬͯͯԿΛ஌Βͳ͍ͷ͔ʁ
    w Ͳ͜·Ͱந৅౓Λ্͛Δ͔ʁ
    w ϝΠϯγεςϜ಺ͷଞͷػೳ͔Β΋࢖ΘΕΔ͜ͱ͕͋Δ͔ʁ
    w ଞͷγεςϜ͔Β΋࢖ΘΕΔ͜ͱ͕͋Δ͔ʁ

    View full-size slide

  35. ࡞Γํಠཱͤ͞Δ
    w ϚΠΫϩαʔϏε෦෼Λଞͷαʔόʔʹ࣮ࡍʹσϓϩΠͯ͠ɺͰߟ͑ͨ઀ଓ
    ෦෼Λ௨ͯ͠ݺͼग़ͯ͠ΈΔ

    View full-size slide

  36. 1)1ͰϚΠΫϩαʔϏεͷ࡞Γํ
    w ʮಠཱͤ͞Δʯ·ͰདྷΕͨΒɺʮผʹ1)1͡Όͳͯ͘΋ྑ͍ʯ͜ͱ͕Θ͔
    Δʢ΋ͪΖΜ1)1ͷ··Ͱ΋͔·Θͳ͍ʣ
    w ͚ͩ͜͜ϝΠϯγεςϜͱ1)1ͷόʔδϣϯ͕ҧͬͯ΋ྑ͍
    w ࣋ଓՄೳɾϏδωεͷมԽʹԠͯ͡վमΛকདྷʹ౉ͬͯଓ͚ΒΕΔ͜ͱ͕
    େࣄ

    View full-size slide

  37. 1)1ͰϚΠΫϩαʔϏεͷ࡞Γํ
    w ϝΠϯγεςϜͱϚΠΫϩαʔϏεͷ઀ଓʹ)551"1*Λ࢖͏৔߹ɺϚΠΫϩ
    αʔϏεଆʹ)551Λड͚Δ࣮૷͕ඞཁͳͷͰɺͦͷ৔߹͸ϚΠΫϩαʔϏε
    ଆ͕ϑϨʔϜϫʔΫΛ࢖ͬͯ΋ྑ͍
    w ࠓߋ@(&5ͱ͔@1045͸ͭΒΈ
    w ͨͩ͠ɺ͜͜ͰϑϨʔϜϫʔΫΛ࢖ͬͯ͠·͏ͱϚΠΫϩαʔϏε͕͍ͭͷ
    ؒʹ͔ҭͬͯʮখن໛ʯ͕कΕͳ͘ͳΔ͜ͱ͕͋Γ஫ҙ

    View full-size slide

  38. "84Ͱ1)1ϚΠΫϩαʔϏεΛ
    ಈ͔͢

    View full-size slide

  39. "84Ͱ1)1ͷϚΠΫϩαʔϏεΛಈ͔͢
    w ઀ଓ෦ʹΑͬͯͲ͏͍͏ߏ੒ʹ͢Δ͔ҟͳΔ

    View full-size slide

  40. "84Ͱ1)1ͷϚΠΫϩαʔϏεΛಈ͔͢ɿ)551"1*ܕ
    w "1*(BUF8BZ-BNCEB-BNCEB
    w ຊτʔΫऩ࿥ޙͷʹ-BNCEBʹ௚઀63-Λ΋ͨͤΔ͜ͱ͕Ͱ
    ͖ΔΑ͏ʹͳͬͨ͜ͱ͕"84͔Βൃද͞Ε·ͨ͠🤗
    w "QQ3VOOFS

    View full-size slide

  41. "84Ͱ1)1ͷϚΠΫϩαʔϏεΛಈ͔͢ɿΩϡʔܕ
    w &$4424
    w 'BSHBUFͰͭͷίϚϯυΛ࣮ߦ͢Δ&$4λεΫ͕݅ͷ424ϝοηʔδΛ
    σΩϡʔ࣮ͯ͠ߦ͢Δ
    w &$4λεΫͷىಈʹଟগ͕͔͔࣌ؒΔͷͰɺϥϯφʔεΫϦϓτΛஔ͍ͯσ
    Ωϡʔͤ͞ଓ͚ΔԠ༻ฤ΋͋Γ

    View full-size slide

  42. "84Ͱ1)1ͷϚΠΫϩαʔϏεΛಈ͔͢ɿΠϕϯταϒεΫϥΠϒܕ
    w &$44/4424
    w ͭͷΠϕϯτʹରͯ͠छྨҎ্ͷ࢓ࣄ͕ඞཁͳ৔߹ʹ࢖͏
    w 4/4τϐοΫʹϝοηʔδ͕ૹΒΕͨΒͭҎ্ͷ424͕αϒεΫϦϓγϣ
    ϯͰड͚औΔ
    w 'BSHBUFͰͭͷίϚϯυΛ࣮ߦ͢Δ&$4λεΫ͕݅ͷ424ϝοηʔδΛ
    σΩϡʔ࣮ͯ͠ߦ͢Δʢ&$4424ͱಉ͡ʣ

    View full-size slide

  43. 1)1FSͰ΋Ͱ͖ΔʂϚΠΫϩαʔϏε
    w ڠௐ
    w খن໛
    w ࣗ཯త

    View full-size slide

  44. 1)1FSͰ΋Ͱ͖ΔʂϚΠΫϩαʔϏε
    w ͸΍ΓͷΠέͯΔΞʔΩςΫνϟΛ΍ΔͨΊʹ͋ΔͷͰ͸ͳ͘ɺΞϓϦέʔ
    γϣϯΛ࣋ଓతʹ։ൃՄೳʹ͢ΔͨΊͷํ๏ͷҰͭ
    w ͢΂ͯͷγεςϜʹ࢖͑ΔΘ͚Ͱ͸ͳ͍͠ɺ͢΂ͯͷػೳʹ࢖͑ΔΘ͚Ͱ΋
    ͳ͍
    w 1)1Ͱ΋े෼Ͱ͖Δ
    w ͔͠͠ϕετϓϥΫςΟε͸͋·Γੈʹग़ͯͳ͍ͷͰ࢓૊ΈΛ࡞Δࢼߦࡨޡ
    ͸ඞཁ

    View full-size slide