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

Rustとtonicで始める gRPC再入門

Rustとtonicで始める gRPC再入門

gunmaweb #50

Tsubasa SEKIGUCHI

September 10, 2023
Tweet

More Decks by Tsubasa SEKIGUCHI

Other Decks in Programming

Transcript

  1. Rustͱ tonicͰ࢝ΊΔ gRPC࠶ೖ໳ Advanced guide for development gRPC service with

    Rust and tonic. Gunma.web #50 ςʔϚϑϦʔ 2023.09.10 @ Takasaki Met., Empire of Gunma Tsubasa SEKIGUCHI
  2. ࠓճͷLTͰ࿩͢ൣғ  લճ(Gunma.web #49) ͷଓ͖ 
 લճݟ͍ͯͳ͍ํ΋Θ͔ΔΑ͏ʹਐΊ·͢ 
 ۩ମతʹ͸RustΛ࢖͍͍ͬͨͩͨ۩ମతͳ࣮૷ͱ͔ɻ 


    લճͷηογϣϯ͸Speaker Deckʹ্͕ͬͯ·͢ɻ ˞͜ͷ23ίʔυ͸ಈతʹ63-ΛมߋͰ͖ΔͨΊɺࠓޙϦϯΫ͕ػೳ͠ͳ͘ͳΔՄೳੑ͕͋Γ·͢
  3. ࠓճͷLTͰ࿩͞ͳ͍ൣғ  ϋϯζΦϯͰ͖ΔϨϕϧͷ࣮૷ͷ࿩ 
 ͔Μ΂Μ͍ͯͩ͘͠͞  README.mdʹॻ͍ͯ͋Δఔ౓ͷ஌ࣝ 
 10෼Ͱ͸͕࣌ؒ଍Γ·ͤΜ 

    JSͷόϯυϧαΠζͰ͸ͳ͘ମॏΛݮΒ͢ํ๏ 
 ૫ͤΔͨΊͷ͕࣌ؒͳ͍Ͱ͢  ਅ໷தʹ΍Δؾ͕ग़Δݱ৅ʹ͍ͭͯ 
 ͜ͷεϥΠυ͸େମ࢓ࣄ͕ऴΘͬͯՋʹͳͬͨ໷தʹ࡞ͬͯ·͢
  4. લճͷ͓͞Β͍  GoogleࣾͷߟҊͨ͠RPCϓϩτίϧ 
 RPC͸Remote Procedure Call(ԕִखଓ͖ݺͼग़͠ ͷུͰ͢ 
 gRPC͸ࣗࣾϚΠΫϩαʔϏεؒͰͷ಺੡γεςϜʮStubbyʯ͕લ਎

      IDL͔Β༷ʑͳݴޠͷίʔυ͕ੜ੒Ͱ͖Δ 
 ΋ͪΖΜJavaScript΋ՄೳͰ͢  gRPC-webͰWebϒϥ΢β͔ΒgRPCαʔόʔΛୟ͚Δ 
 8FCϒϥ΢βͰͷ࢖༻Ͱ͸੍ݶ͕͋Δ 
 αʔόʔͱͷʮ௚઀ʯ௨৴͸Ͱ͖ͳ͍ʢجຊతʹ&OWPZ౳ͷϓϩΩγͷ༻ҙ͕ඞཁʣ 
  Bi-directional streaming(C/SؒͰ೚ҙͷλΠϛϯάͰ௨৴͕Ͱ͖Δ)ʹରԠ͍ͯ͠ͳ͍
  5. Rustͱ͸  ݴΘͣͱ஌Εͨϓϩάϥϛϯάݴޠ 
 CݴޠɺC++ʹ୅ΘΔγεςϜϓϩάϥϛϯάݴޠΛ໨ࢦ͍ͯ͠Δ  GCͳΜͯ΋ͷ͸ͳ͍ 
 GCͳ͠ͰͷϝϞϦ҆શੑΛอূ͍ͯ͠Δ 


    ($ͷΑ͏ͳػߏ͕ඞཁͳ৔໘Ͱ͸εϚʔτϙΠϯλΛ࢖༻Մೳ 
 AtomicͳArcɺͦ͏Ͱ͸ͳ͍Rc͕͋Δ 
 WebAssemblyʹͩͬͯ࢖͑·͢  ֶशίετߴΊ 
 आ༻ɺϥΠϑλΠϜͱࠓ·Ͱͳ͔ͬͨ֓೦͕͋Δ͕ɺ 
 ͦΕΒ͕ϝϞϦ҆શΛୡ੒͍ͤͯ͞Δ
  6. tonicͱ͸  RustͷgRPC࣮૷ 
 3VTUͰ࢖͑ΔϋΠύϑΥʔϚϯεͰOSSɺ൚༻తͳgRPCϑϨʔϜϫʔΫ  ϋΠύϑΥʔϚϯεʹϑΥʔΧε 
 ଞʹ΋૬ޓӡ༻ੑ͕͋ΓॊೈͳϥΠϒϥϦͱᨳ͍ͬͯΔ -

    ࢖͍ํʹΑͬͯ͸֎෦ϓϩΩγෆཁ 
 grpc-webͷαʔόʔͱͯ͠࢖͍͍ͨ৔߹͸ɺtonic-web crate͕͋Γ·͢ 
 tonic-web crateΛ࢖͏͜ͱͰɺϑϨʔϜϫʔΫଆͰHTTP/1.1Λ஻ΕΔΑ͏ʹͳΓ·͢ 
 ͭ·ΓɺEnvoy౳ͷ֎෦ϓϩΩγ͕ෆཁʹͳΓ·͢
  7. StationAPIϑϧεΫϥονͷϞνϕʔγϣϯ(1/3)  NestJSΛ࢖͍ͬͯ·ͨ͠ 
 NestJSͰGraphQLϞδϡʔϧΛ࢖ͬͯGraphQLͷAPIΛ࣮૷͍ͯ͠·ͨ͠ɻ 
 NestJSࣗମ͸ѱ͘͸ͳ͔ͬͨͰ͢ɻΉ͠Ζਖ਼ࣾһ࣌୅ʹ࢖͍ͬͯͨ 
 Angularͱߏ଄͕ࣅ͍ͯͯɺϊελϧδʔʹਁΕ·ͨ͠ શવؔ܎ͳ͍

    ɻ  N+1໰୊ʹͿͪ౰ͨΔ 
 ΋ͪΖΜɺNestJSͰղܾෆՄೳͳ໰୊Ͱ͸ͳ͍ͷͰ͕͢ɻ 
 ۙࠒTrainLCDΞϓϦͷϢʔβʔ΋૿͖͑ͯͯɺͲ͏ͤൈຊతʹॻ͖׵͑ΔͳΒ 
 NestJSʹ᩾ር͢ΔΑΓ΋͏ͪΐͬͱݎ࿚Ͱߴ଎ͳ"1*Λ࡞Γ͔ͨͬͨͷͰ͢ɻ
  8.  ͜͜ͰRustʹനӋͷ໼ཱ͕ͭ 
 աڈʹͪΐͬͱ͚ͩRustͷษڧΛ͍ͯͯ͠ɺݴޠͷઃܭ͕RustͷॳֶϓϩάϥϚʹݫ͍͕͠ɺ 
 ҆৺ͯ͠ಈ͔ͤΔ্ʹNode.jsΑΓߴ଎ɾ௿ϝϞϦϑοτϓϦϯτͳAPIΛ࡞ΕΔͷͰɺ 
 ࠓճͷϑϧεΫϥον͸RustͰߦ͏લఏͱͳΓ·ͨ͠ɻ  Ͳ͏ͤͳΒgRPC΋ࢼͯ͠ΈΑ͏

    
 gRPCࣗମ͸ҎલΑΓ஌͍ͬͯͯ࠾༻͢Δ͔ܰ͘໎͍·͕ͨ͠ɺैདྷͷGraphQL APIͷ՝୊ͱͯ͠ 
 ʮΞϓϦͷૢ࡞ճͰඞཁͳσʔλ͕ଟͯ͘ΫΤϦ͕ංେԽɺϨεϙϯεσʔλ͕͑͛ͭͳ͍αΠζʹͳΔʯ 
 ͱ͍͏ͷ΋͋Γͦ΋ͦ΋ΞϓϦͷઃܭࣗମ͕ؒҧ͍ͬͯΔؾ΋͠ͳ͘͸ͳ͍Ͱ͕͢ɺ 
 εϚϗ޲͚ΞϓϦͰͷ௨৴༰ྔ͕ଟ͍ͷ͸େ͖ͳ՝୊Ͱͨ͠ɻ StationAPIϑϧεΫϥονͷϞνϕʔγϣϯ(2/3)
  9.  gRPCͷϖΠϩʔυ͸όΠφϦ 
 όΠφϦͰ΍ΓऔΓ͢ΔͷͰɺઌ΄Ͳઆ໌ͨ͠ʮϨεϙϯεσʔλͷංେԽʯʹ͸ޮՌతͦ͏Ͱ͢ΑͶɻ 
 ChromeͷDevToolsͰ͸όΠφϦͷͨΊ֬ೝ͕͠ΜͲ͔ͬͨΓ͠·͢ɻ  Protocol Bu ff

    ersͰλΠϓηʔϑ 
 ͪ͜Β͸REST APIͱͷൺֱͰ͕͢ɺ 
 gRPC APIͱ௨৴͢Δ࣌ʹඞͣProtocol Bu ff ersͷεΩʔϚఆٛΛ࢖༻͢ΔͷͰ 
 ௨৴͢Δલ͔ΒϦΫΤετͱϨεϙϯεͷܕ͕Θ͔Βͳ͍ɺͱ͍͏͜ͱ͸ͳ͘ͳΔ͸ͣͰ͢ɻ 
 ཁ͢ΔʹREST APIͰ͍͏Swagger͕࠷ॳ͔Β࢖͑Δͱ͍ͬͨΠϝʔδͰ͢ɻ StationAPIϑϧεΫϥονͷϞνϕʔγϣϯ(3/3)
  10.  Protocol Bu ff ersͰεΩʔϚఆٛΛߦ͏  RustͰ࣮૷  GCPʹσϓϩΠ 

    PostmanͰୟ͍ͯΈΔ  ΞϓϦʹͭͳ͗͜Ή લճઆ໌ͨ͠Օॴͱ࿩͕௕͘ͳΔ෦෼͸ඈ͹͠·͢ ͬ͟ͱ΍ͬͨ͜ͱ
  11.  Protocol Bu ff ersͰεΩʔϚఆٛΛߦ͏  RustͰ࣮૷  GCPʹσϓϩΠ 

    PostmanͰୟ͍ͯΈΔ  ΞϓϦʹͭͳ͗͜Ή લճઆ໌ͨ͠Օॴͱ࿩͕௕͘ͳΔ෦෼͸ඈ͹͠·͢ ͬ͟ͱ΍ͬͨ͜ͱ
  12. RustΛ࢖ͬͨ։ൃͷ஌ݟύϑΥʔϚϯεฤ  ύϑΥʔϚϯεΛߴΊΔͨΊͷ޻෉͸ʁ 
 ͍͢͝ॳาతͰ͕͢ɺͳΔ΂͘஋ͷΫϩʔϯΛߦΘͳ͍ͱ͔ɺ 
 mokaͱ͍͏crateͰӬଓԽͳ͠ͷ 
 ΠϯϝϞϦΩϟογϡΛ࣮૷͍ͯͨ͠Γ͠·͢ɻ 


    ༨ஊͰ͕͢ɺmokaͰ͸஋ͷऔಘ࣌ʹclone()͕࣮ߦ͞ΕΔͷͰ 
 NPLBͰσʔλΛૠೖ͢Δ࣌Arc(ΞτϛοΫࢀরΧ΢ϯτ)Ͱ 
 ϥοϓ͠ͳ͍ͱߴՁͳdeep copy͕ൃੜ͢ΔՄೳੑ͕͋Γ·͢ 
 େ͖ͳσʔλΛૠೖɾऔಘ͢Δ৔߹͸େมͳ͜ͱʹͳΓ·͢ɻ 
 ૠೖ͢Δࡍ͸ඞͣArcͰϥοϓ͠·͠ΐ͏ɻ
  13. gRPC APIΛσϓϩΠ  ελοΫʹ͍ͭͯ 
 StationAPI͸GCPʹશ෦৐͔ͬͬͯ·͢ɻ࢖͍ͬͯΔαʔϏε͸ 
 
 - Cloud

    Build
 - Cloud Run
 - Cloud SQL
 - Text-to-Speech AI(5SBJO-$%ΞϓϦͷҡ࣋අͷ಺Ͱ͸Ұ൪ߴֹ) 
 Firebase
 - Firestore/Storage/Messaging/Authentication/Functions/Remote Con fi g 
 Կʹ࢖͍ͬͯΔ͔͸͜ͷεϥΠυͷൣғ֎ͳͷͰݸผͰฉ͍͍ͯͩ͘͞ 
 
 ईͷؔ܎Ͱ΄ͱΜͲ঺հͰ͖ͳ͍Ͱ͢🥺
  14. Cloud Buildͷ࢖͍ಓ  ͦ΋ͦ΋Cloud Buildͱ͸ʁ 
 ໊લͷ௨ΓCI/CDͷCDΛ୲͏αʔϏεɻ 
 GitHub΁ͷPush౳(PubSubϝοηʔδɺWebhookΠϕϯτ΋Մೳ)Λ 


    τϦΨʔʹͯ͠ࢦఆͨ͠ίϚϯυΛ૸Βͤͯ͘Ε·͢ɻ 
 Docker fi le͔Βͦͷ··σϓϩΠ΋ՄೳͰɺ 
 ΄͔ʹ͸ઐ༻ͷߏ੒ϑΝΠϧ(cloudbuild.yaml)΍ 
 ΠϯϥΠϯͰͷߏ੒͕ՄೳͰ͢ɻ
  15. Cloud Buildͷ஌ݟRustͷϏϧυΛߴ଎Խ͢Δ  KanikoΛ׆༻ͯ͠Ϗϧυ࣌ؒΛ࡟ݮ͢Δ 
 جຊతʹRustͷϏϧυ͸஗͍͜ͱΛઆ໌͠·ͨ͠ 
 ͦ͜ͰɺKanikoΛCloud Buildʹ࢖ͬͯΈ·͠ΐ͏ 


    Kaniko͸Docker DaemonΛ࢖Θͣʹ 
 k8sΫϥελ্ͰϏϧυΛߦ͏͜ͱ͕Ͱ͖·͢ 
 Cloud Buildͱ૊Έ߹ΘͤΔͱͳΜ΍͔Μ΍ͰΩϟογϡ͞Εͯ 
 2ճ໨͔ΒϏϧυ͕ര଎ʹͳΓ·͢ 
 StationAPIͰ͸Ϗϧυ͕࣌ؒ20෼͔Β10෼ͱ൒෼ʹͳΓ·ͨ͠
  16. Cloud Runͷ࢖͍ಓ  ͦ΋ͦ΋Cloud Runͱ͸ʁ 
 ϑϧϚωʔδυͷαʔόʔϨεϓϥοτϑΥʔϜ 
 ϏϧυࡁΈͷDockerίϯςφΛσϓϩΠग़དྷΔ 


    ·ͨઃఆΛ׶͑ͯม͑ͳ͚Ε͹ 
 ίʔυ͕࣮ߦ͞Ε͍ͯΔͱ͖ͷྉ͕ۚൃੜ͢Δ 
 
 ࣗಈεέʔϧ͸ߴ଎ͰτϥϑΟοΫʹ߹Θͤͯ 
 θϩ͔Β೚ҙͷن໛ͰࣗಈతʹεέʔϦϯάՄೳ 
 ߋʹΠϯελϯε͋ͨΓ࠷େ ݅ͷಉ࣌ϦΫΤετΛ 
 ॲཧͰ͖ΔͨΊͱͯ΋༨༟͕͋Γ·͢ 
 
 ϑϧϚωʔδυͷͨΊΠϯϑϥͷ؅ཧ͸ෆཁͰɺ 
 σϓϩΠͨ͠ޙ͸Cloud Runʹؙ౤͛Ͱ͖·͢
  17. ·ͱΊ  Rust͸͍͍ͧ 
 Rust͸҆શͰߴ଎ͳγεςϜϓϩάϥϛϯάݴޠͰ͢ 
 ࠷ॳͷֶशίετ͸ߴ͍Ͱ͕͢ɺҰ౓Ͱ΋৮ͬͯΈΔՁ஋͸ 
 զʑWebۀքͷਓؒͩͱͯ͠΋͋Γ·͢ 

    GCP΋͍͍ͧ 
 AWSΑΓγϯϓϧͳ؅ཧUIΛ͍ͯͯ͠ຊ࣭తʹඞཁͳ࡞ۀʹ஫ྗͰ͖·͢ɻ 
 AWSͱ؅ཧUI͚ͩͰ༏ྼΛ͚ͭΔ͜ͱ͸Ͱ͖ͳ͍ͱࢥ͍·͕͢ɺ 
 ݸਓతʹ͸GCPਪ͠Ͱ͢  TrainLCDΞϓϦΛ͝᩾ርʹ 
 ൱ఆతͳઌೖ؍Λ͍࣋ͬͯͨਓͰ΋ɺ࢖ͬͯΈΕ͹ 
 ʮੜ׆ʹ͔ܽͤͳ͍ʯ໘ന͍ΞϓϦͱධՁͯ͠΋Β͍͑ͯ·͢