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

php grpc-client in phpcon2018

633ac716e2e09b8710621c881508cac2?s=47 shmurakami
December 15, 2018

php grpc-client in phpcon2018

About grpc-client and use case of that for PHP

633ac716e2e09b8710621c881508cac2?s=128

shmurakami

December 15, 2018
Tweet

More Decks by shmurakami

Other Decks in Technology

Transcript

  1. 1)1ͰͷH31$ΫϥΠΞϯτ࡞੒ͱ Ϣʔεέʔε 1)1$POGFSFODF

  2. © Chatwork ࣗݾ঺հ ˔ଜ্ढ़հ ˔$IBUXPSLגࣜձࣾ ˔ೖࣾ ˔1)1ΤϯδχΞ ˔*46$0/ͷຊઓʹ1)1Ͱग़৔ͨ͠Αʂ

  3. © Chatwork $IBUXPSLʹ͍ͭͯ ˔ϏδωενϟοταʔϏε ˓άϧʔϓνϟοτ λεΫ؅ཧ ϑΝΠϧڞ༗ ϏσΦԻ੠௨࿩ ˔ಋೖاۀ ࣾҎ্

    ݱࡏ
  4. © Chatwork 5PQJDT ˔ಋೖ ˔H31$ʹ͍ͭͯ ˔1)1ͰͷH31$ΫϥΠΞϯτ࣮૷ ˔$IBUXPSLͷH31$Ϣʔεέʔε

  5. © Chatwork

  6. © Chatwork (3085) ˔αʔϏε͕(SPXUIͨ͠ͱ͖ʹͲ͏͢Δ͔ʁ ˔ڊେͳϞϊϦεͷγεςϜ͸େମπϥ͍ ˓ಡΈղ͚ͳ͍εύήοςΟίʔυ ˓ྺ࢙తܦҢɺ҉໧஌ ˓ؔ܎ͳ͍͸ͣͷมߋ͕΋ͨΒ͢όά

  7. © Chatwork (3085) ˔ҙຯͷ͋Δ୯ҐͰ෼ׂ͍ͨ͠ ˓ίϯϙʔωϯτΛ"1*Ͱ΍ΓऔΓ͢Δέʔε͸Α͋͘Δ ˙มߋͷӨڹΛখ͘͢͞Δ ˙վળΛ༰қʹ ˔㱠.JDSPTFSWJDFT

  8. © Chatwork (3085) ˔+40/Ͱ௨৴͢Δ)551ϕʔεͷ"1* ˓"1*ͷϑΥʔϚοτΛڞ༗͢Δͷ͸େม ˓௨৴ʹΑΔύϑΥʔϚϯε΁ͷӨڹ͕খ͘͞ͳ͍ ˔ϋΠύϑΥʔϚϯεɺ։ൃ༰қੑ ˓H31$Λ࢖ͬͯղܾͰ͖Δ͔΋͠Εͳ͍

  9. © Chatwork 1)1ΞϓϦέʔγϣϯͷจ຺Ͱߟ͑Δͱ ˔1)1ϞϊϦε͔ΒίϯϙʔωϯτΛ੾Γग़͢λΠϛϯάͰ ผݴޠΛબ୒͢Δέʔε ˓ΞʔΩςΫνϟɺύϑΥʔϚϯε্ͷཧ༝ͱ͔ ˓ 1)1πϥ͍ͱ͔υϠΕΔݴޠΛ࢖͍͍ͨͱ͔  ˔H31$ͷ࢓্༷ύϑΥʔϚϯε͕༏Ε͍ͯΔ

    ˓1)1ΛH31$ΫϥΠΞϯτͱͯ͠Քಇ͍ͤͨ͞
  10. © Chatwork gRPCʹ͍ͭͯ

  11. © Chatwork H31$ ˔(PPHMF͕։ൃͨ͠ϓϩτίϧ ˔)UUQϕʔεͰߴ଎ͳ௨৴͕Մೳ ˓4USFBNJOH ˔3FRVFTU3FTQPOTF4FSJBMJ[BUJPO ˔ৄࡉͳυΩϡϝϯτ ˓IUUQTHSQDJP ˓IUUQTHJUIVCDPNHSQDHSQDUSFFNBTUFSEPD

    ˙HSQDJP͸ݹ͍ͱ͜Ζ͕͋ΔͷͰ(JU)VC΋ݟͨํ͕͍͍
  12. © Chatwork 1SPUPDPM#VGGFST ˔ߏ଄σʔλͷγϦΞϥΠζϝΧχζϜ ˓3FRVFTU3FTQPOTFͷ4FSJBMJ[FSͱͯ͠H31$ͷσϑΥϧ τʹͳ͍ͬͯΔ ˔*%- *OUFSGBDF%FGJOJUJPO-BOHVBHF  ˓3FRVFTU3FTQPOTFΛఆٛ͢Δ%4-

    QSPUP  ˓αʔόʔΫϥΠΞϯτ͕ڞ௨Ͱ࢖༻͢Δ"1*ϑΥʔϚοτ Λ౷ҰͰ͖Δ
  13. © Chatwork 1SPUPDPM#VGGFSTTBNQMF syntax = “proto3"; message Person { string

    name = 1; string email = 2; enum PhoneType { MOBILE = 0; HOME = 1; } message PhoneNumber { string number = 1; PhoneType type = 2; } } service GreetingService { rpc greet (Person) returns (stream PersonResponse); }
  14. © Chatwork QSPUPD ˔1SPUPDPM#VGGFST$PNQJMFS ˔1SPUPDPM#VGGFSTͷҰ෦ ˓ϏϧυࡁΈͷ࣮ߦϑΝΠϧ͕(JU)VCʹ্͕͍ͬͯΔ ˓CSFXͳͲͷύοέʔδϚωʔδϟʔͰ΋औಘՄೳ ˔QSPUPϑΝΠϧΛίϯύΠϧͯ͠ίʔυΛੜ੒

  15. © Chatwork QSPUPDίϚϯυαϯϓϧ $ protoc \ --proto_path=./proto \ --php_out=./dest \

    --grpc_out=./dest \ --plugin=protoc-gen-grpc=./grpc/bins/opt/grpc_php_plugin \ ./proto/sample.proto
  16. © Chatwork ੜ੒ϑΝΠϧ $ tree dest ├── GPBMetadata │ └──

    Sample.php ├── GreetingServiceClient.php ├── Person │ ├── PhoneNumber.php │ └── PhoneType.php ├── Person.php proto └── sample.proto syntax = “proto3"; message Person { string name = 1; string email = 2; enum PhoneType { MOBILE = 0; HOME = 1; } message PhoneNumber { string number = 1; PhoneType type = 2; } } service GreetingService { rpc greet (Person) returns (stream PersonResponse); }
  17. © Chatwork ੜ੒ϑΝΠϧ <?php class Person extends \Google\Protobuf\Internal\Message { private

    $name = ''; private $email = ''; public function __construct($data = NULL) { /*...*/ } public function getName() { return $this->name; } public function setName($var) { /*...*/ } public function getEmail() { return $this->email; } public function setEmail($var) { /*...*/ } }
  18. © Chatwork ੜ੒ϑΝΠϧ <?php // GENERATED CODE -- DO NOT

    EDIT! namespace ; class GreetingServiceClient extends \Grpc\BaseStub { public function __construct($hostname, $opts, $channel = null) { parent::__construct($hostname, $opts, $channel); } public function greet(\Person $argument, $metadata = [], $options = []) { return $this->_serverStreamRequest('/GreetingService/greet', $argument, ['\PersonResponse', 'decode'], $metadata, $options); } }
  19. © Chatwork H31$1SPUPDPM#VGGFST ˔1)1Ҏ্ɹͦ΋ͦ΋1)1͸೥಺Ͱ&0-Ͱ͢Αʂ ˔$ Ҏ্ʹରԠͨ͠HDD ˔$FOU04ͩͱHDDͷόʔδϣϯ͕௿͗ͯ͢Ϗϧυ͕େม ˓ηοτΞοϓ͕஍ຯʹΊΜͲ͍͘͞ ˓ίϯύΠϧ͢ΔEPDLFSΠϝʔδΛ࡞ͬͯ͠·͏ͱָ ˔1)1͕ରԠ͍ͯ͠Δͷ͸QSPUP͔ΒͳͷͰɺQSPUPΛ࢖ͬͯ

    ͍Δ৔߹͸ʹ্͛Δඞཁ͕͋Δ
  20. © Chatwork %PDLFSGJMFྫ FROM amazonlinux:2018.03 RUN yum update -y &&

    \ yum install -y \ # grpc dependencies git gcc gcc48-c++ autoconf automake libtool unzip which wget # download protoc RUN mkdir /opt/protoc && \ wget -O /opt/protoc/protoc.zip https://github.com/protocolbuffers/protobuf/ releases/download/v3.5.1/protoc-3.5.1-linux-x86_64.zip \ && unzip -d /opt/protoc /opt/protoc/protoc.zip \ && rm /opt/protoc/protoc.zip # build grpc_php_plugin RUN git clone https://github.com/grpc/grpc /opt/grpc \ && cd /opt/grpc \ && git submodule update --init \ && cd /opt/grpc/third_party/protobuf \ && sh autogen.sh \ && cd /opt/grpc \ && make grpc_php_plugin
  21. © Chatwork ࢒೦ͳ͓஌Βͤ ˔1)1ͰͷH31$αʔόʔ͸Ͱ͖ͳ͍ ˔H31$αʔόʔΛ࡞Δͱ͖͸ผݴޠΛ࢖͏ ˔$IBUXPSLͰ͸4DBMBΛ࠾༻͍ͯ͠·͢ ˓4DBMBΛ࠾༻ͨ͠ܦҢ͸ͪ͜Β IUUQDOPUFDIBUXPSLDPNQPTUEFWDBNQ

  22. © Chatwork 1)1ͰͷH31$αʔόʔ͸ͳͥ೉͍͔͠ ˔NPE@QIQ΍QIQGQNͳͲΛհ͢ͱHSQD௨৴ͷ؊Ͱ͋ΔίωΫγϣ ϯͷ4USFBN௨৴͕࢖͑ͳ͘ͳΔ ˔"QBDIFOHJOYΛ࢖Θͳ͍ͱ1)1୯ମͰ͸)551ͷϋϯυϦϯά͕Ͱ ͖ͳ͍ ˓1)1͸୯ମͰEBFNPOJ[FͰ͖ͳ͍ ˓NBY@FYFDVUJPO@UJNFͱ͔͋Δ͠ ˓.JDSPTFSWJDFTͰ1)1͕͋·Γ࢖ΘΕͳ͍͜ͱͱࣅͯΔ

    ˙1)1Λ࢖͓͏ͱ͢Δͱϑϩϯτ8FCαʔό͕ඞਢʹͳΔ
  23. © Chatwork 1)1ͰͷH31$αʔόʔ͸ͳͥ೉͍͔͠ ˔3PBE3VOOFSͱ͍͏1)1"QQMJDBUJPO4FSWFS (P੡ Λ࢖ ͏ͱ(P͕)UUQΛϋϯυϦϯά͢ΔͷͰ࢖͑ΔΒ͍͠ ˓IUUQTHJUIVCDPNTQJSBMSPBESVOOFS ˓΋͸΍ී௨ͷ1)1Ͱ͸ͳ͍͕ʜʜ

  24. © Chatwork PHPͰͷgRPCΫϥΠΞϯτ࣮૷

  25. © Chatwork PHPͰͷgRPCΫϥΠΞϯτ࣮૷ ˔ඞཁʹͳΔίϯϙʔωϯτ ˓H31$௨৴ ˓3FRVFTU3FTQPOTF4FSJBMJ[FS

  26. © Chatwork H31$௨৴ ˔H31$௨৴ॲཧͷ࣮ମ ˓H31$FYUFOTJPO$ Ͱ࡞ΒΕ͓ͯΓ1)1֦ுΛΠϯε τʔϧ͢Δ ˓1&$-Ͱ഑෍͞Ε͍ͯΔ HSQD 

    ˔HSQDϥΠϒϥϦ ˓HSQD֦ுΛૢ࡞͢ΔͨΊͷϥΠϒϥϦ ˓$PNQPTFSͰ഑෍͞Ε͍ͯΔ HSQDHSQD
  27. © Chatwork 3FRVFTU3FTQPOTF4FSJBMJ[FS ˔1SPUPDPM#VGGFSTͷ1)1֦ு QSPUPCVG ͕ͦΕ ˓1&$- QSPUPCVG ΋͘͠͸ ˓$PNQPTFS

    HPPHMFQSPUPCVG Ͱఏڙ͞Ε͍ͯΔ ˓֦ுͷํ͕$PNQPTFSΑΓύϑΥʔϚϯε͕༏Ε͍ͯΔ ˙֦ுΛ࢖͏Α͏ʹ͢Δͱ1IQ4UPSNͰίʔυิ׬͕ ޮ͔ͳ͍ͷͰSFRVJSFEFWͰೖΕ͍ͯΔ
  28. © Chatwork QSPUPCVG ˔֦ுͱ$PNQPTFSͷ଎౓ൺֱ 1)1  ˓͋ΔσʔληοτΛQSPUPCVGͷΫϥεʹηοτͯ͠γ ϦΞϥΠζ͢Δ 1&$- $PNQPTFS

    ॴཁ࣌ؒ NT NT ˔ഒҎ্଎͍
  29. © Chatwork ϦϙδτϦઃܭ ˔QSPUP͔Βੜ੒͞ΕΔίʔυΛઐ༻ͷϦϙδτϦͰ؅ཧ ˔$PNQPTFSܦ༝Ͱࢀর ˓QSPUP͔Βͷੜ੒෺͸ΞϓϦέʔγϣϯͷຊ࣭ͱ͸ҟͳ Δ΋ͷ ˓ґଘΛڧͨ͘͘͠ͳ͍ͷͰผϦϙδτϦʹ੾Γ཭͍ͨ͠

  30. © Chatwork ϋϚΓͲ͜Ζ ˔1&$-ͰͷΠϯετʔϧ ˔ґଘίϯϙʔωϯτͷόʔδϣϯ

  31. © Chatwork 1&$-ͰͷΠϯετʔϧ ˔1&$-ͰͷΠϯετʔϧ࣌ʹ͸ίϯύΠϧ͕࣮ߦ͞ΕΔ ˓$FOU04ͷΑ͏ͳHDDͷόʔδϣϯ͕ݹ͍؀ڥͩͱί ϯύΠϧ͕Ͱ͖ͳ͍ ˓EFWUPPMTFUͱ͔࢖ͬͯແཧ΍ΓೖΕΔ͔͠ͳ͍

  32. © Chatwork ґଘίϯϙʔωϯτͷόʔδϣϯ ˔HSQD QSPUPCVG QSPUPDͷόʔδϣϯޓ׵ੑΛอͭඞཁ͕͋Δ ˓ͦΕͧΕͷೖखܦ࿏͕ҧ͏ ˓1&$- $PNQPTFS (JU)VCSFMFBTFQBHF

    ˓1)1࣮ߦ࣌ͰͷΤϥʔ·Ͱ໰୊ʹؾͮ͘λΠϛϯά͕ແ͍ ˙Τϥʔʹͳͬͯ΋ϥΠϒϥϦͷத·ͰίʔυΛݟʹߦ ͔ͳ͍ͱԿ͕͓͔͍͔͠෼͔Βͳ͍
  33. © Chatwork ґଘίϯϙʔωϯτͷόʔδϣϯ ˔ޓ׵ੑ͕෼͔ΓͮΒ͍ ˓ެࣜυΩϡϝϯτ͕ߋ৽͞Εͳ͍ ˔࠷৽൛ ࣌఺ ͸ ˓HSQD ˓QSPUPCVG

    ˔3FMFBTFT/PUFTΛݟͯؤுΔʜ grpc.io github
  34. © Chatwork 1)1H31$DMJFOU·ͱΊ ˔H31$ͷ$ ֦ு$PNQPTFSίʔυ͕ඞཁ ˔QSPUPCVGͷ1&$-൛cc$PNQPTFS൛͕ඞཁ ˓ݹ͍αʔόʔͩͱେม ˔ίϯϙʔωϯτͷόʔδϣϯޓ׵ੑ͕ແ͍৔߹ͷ࣮ߦ࣌Τ ϥʔ͸σόοά͕େม͔ͩΒؾΛ͚ͭΔ

  35. © Chatwork ChatworkͰͷgRPCϢʔεέʔε

  36. © Chatwork Ϣʔεέʔε ˔$IBUXPSLͷςΩετͷ࢓༷͸ύʔε͕πϥ͍ [info][title]PHPCon2018:)[/title]PHPͰgRPCಈ͔͍ͨ͠Ͱ͢Ͷ(bow)[/info] ˔ΫϥΠΞϯταΠυͰύʔε͍͍ͯ͠ײ͡ͷݟͨ໨ʹ͢Δ ˓πϥ͍

  37. © Chatwork Ϣʔεέʔε [info][title]PHPCon2018:)[/title]PHPͰgRPCಈ͔͍ͨ͠Ͱ͢Ͷ (bow)[/info] {“tokens":[ {“info”:{"title":[{"text":"PHPCon2018"},{"emoticonSmile":{}}], “message":[ {“text":"PHPͰgRPCಈ͔͍ͨ͠Ͱ͢Ͷ"},{"emoticonBow":{}} ]}}

    ]} ASTԽ ˔ৄࡉ͸ϒϩά͕ޙ೔ॻ͔Ε·͢ʂ ˓IUUQTDSFBUPSTOPUFDIBUXPSLDPN
  38. © Chatwork Ϣʔεέʔε ˔νϟοτ ˔෦԰֓ཁ ˔λεΫ ộ

  39. © Chatwork Ϣʔεέʔε ˔ద༻ൣғ͕޿͍ ˔$IBUXPSLͷϦΫΤετྔͰ͸ͪΐͬͱͨ͠ϨεϙϯελΠ Ϝͷ૿Ճ΋க໋త ˓͔ͳΓͷߴεϧʔϓοτ͕ٻΊΒΕΔ

  40. © Chatwork Ϣʔεέʔε

  41. © Chatwork ύϑΥʔϚϯε ˔H31$ͷॲཧΛೖΕΔલޙͰͷฏۉϨεϙϯελΠϜͷൺֱ ˓"84ಉҰ71$಺ͷ&$ؒ௨৴ LTܦ༝  ˙ҎԼ͸ࢀߟ஋ͱͯ͠ϩʔΧϧ؀ڥͷൺֱ ಋೖલ H31$͋Γ

    ฏۉ NT   ˔ϩʔΧϧͰ͸େମNTఔ౓ͷ૿Ճ ˓+40/4USJOHΛฦ͚ͩ͢ͷ"1*͕OHJOYܦ༝ͰNT
  42. © Chatwork ύϑΥʔϚϯε ϦϦʔε͕ؒʹ߹Θͣ ϓϩμΫγϣϯ؀ڥͰͷݕূ݁Ռ͕ग़ͤ·ͤΜͰͨ͠ ϦϦʔεޙʹϓϩμΫγϣϯ؀ڥͰൺֱͯ͠ߋ৽͢Δ༧ఆͰ͢

  43. © Chatwork ·ͱΊ ˔1)1ͰͷΫϥΠΞϯτ࣮૷͸ϋϚΓͲ͜Ζ΋গͳ͘؆୯ ˓ࣗಈੜ੒͞Εͨίʔυ͕࢖͑ΔͷͰैདྷΑΓ΋ָ͔΋ ˔ߴεϧʔϓοτͷ؀ڥͰ΋࢖͑ͦ͏ ˓αʔϏε͕(SPXUIͨ͠ͱ͖ͷࢪࡦͷͭͱͯ͠༗ྗ ˓ଞݴޠͰH31$αʔόʔΛ࣮૷Ͱ͖Δਓ͕͍Ε͹ɾɾɾ ˔1)1ͰH31$αʔόʔΛ࣮ݱ͢Δະདྷ͕དྷΔͱ͍͍Ͱ͢Ͷ

  44. © Chatwork 8F$IBUXPSLBSFIJSJOH IUUQTDPSQDIBUXPSLDPNKBSFDSVJUFOHJOFFS

  45. © Chatwork ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠