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

マイクロサービスバックエンドAPIのためのRESTとgRPC

disc99
March 27, 2017

 マイクロサービスバックエンドAPIのためのRESTとgRPC

disc99

March 27, 2017
Tweet

More Decks by disc99

Other Decks in Technology

Transcript

  1. ϚΠΫϩαʔϏεͷ࿈ܞ > In short, the microservice architectural style is an

    approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. => HTTPϦιʔεΛ࢖༻ͨܰ͠ྔAPIͰ௨৴ Microservices @Martin Fowler, @James Lewis https://martinfowler.com/articles/microservices.html
  2. RESTful API > RESTful APIͱ͸ɺWebγεςϜΛ֎෦͔Βར༻͢ΔͨΊͷϓϩάϥϜͷ ݺͼग़͠ن໿(API)ͷछྨͷҰͭͰɺRESTͱݺ͹ΕΔઃܭݪଇʹैͬͯࡦఆ ͞Εͨ΋ͷɻRESTͦͷ΋ͷ͸ద༻ൣғͷ޿͍ந৅తͳϞσϧ͕ͩɺҰൠత ʹ͸RESTͷߟ͑ํΛWeb APIʹద༻ͨ͠΋ͷΛRESTful APIͱݺΜͰ͍Δ

    
 > URL/URIͰ͢΂ͯͷϦιʔεΛҰҙʹࣝผ͠ɺηογϣϯ؅ཧ΍ঢ়ଶ؅ཧ ͳͲΛߦΘͳ͍(εςʔτϨε)ɻ 
 > Ϧιʔεͷૢ࡞͸HTTPϝιουʹΑͬͯࢦఆ͞Εɺ݁Ռ͸XML΍HTMLɺ JSONͳͲͰฦ͞ΕΔɻ·ͨɺॲཧ݁Ռ͸HTTPεςʔλείʔυͰ௨஌͢ Δͱ͍͏ݪଇؚ͕·ΕΔ͜ͱ΋͋Δɻ e-Words http://e-words.jp/w/RESTful_API.html * Ҏ߱͸লུͷͨΊRESTful APIΛREST APIͱهࡌ͠·͢
  3. ϙΠϯτ • Web APIͱͯ͠Α͘࢖ΘΕΔ • ϦιʔεΛURL/URIͰҰҙࣝผ • ૢ࡞: HTTPϝιου •

    ݁Ռ: XMLɺHTMLɺJSON + HTTPεςʔλείʔυ • ઃܭݪଇ:̋ ࢓༷:✕
  4. ࠷௿ݶͷ஌ࣝᶃ • جຊ஌ࣝɺΨΠυϥΠϯ • WebΛࢧ͑Δٕज़(ٕज़ධ࿦ࣾ) • RESTful WebαʔϏε(O'Reilly) • Web

    API: The Good Parts(O'Reilly) • Microsoft REST API Guidelines • https://github.com/Microsoft/api-guidelines/blob/master/ Guidelines.md • Google API Design Guide • https://cloud.google.com/apis/design/
  5. ઃܭͷϙΠϯτ • URLɺύϥϝʔλɺΤϥʔͳͲͷઃܭ • جຊతͳઃܭཧ࿦ɺ࣌୅ͷಈ޲ɺٕज़ελοΫɺ૊৫ؒͷؔ܎ੑ • ֤ݴޠ͝ͱͷม׵ϥΠϒϥϦɺαʔόɺΫϥΠΞϯτͷબఆɺઃܭɺӡ༻ݕ౼ • όϦσʔγϣϯ •

    ͢Δ or ͠ͳ͍͔: ͢ΔͳΒ֤ݴޠͷػೳൺֱɺϥΠϒϥϦɺӡ༻ݕ౼ • υΩϡϝϯτɺπʔϧ܈ • SwaggerɺAPI BlueprintɺRAMLɺJSON (Hyper)SchemeɺಠࣗܗࣜͳͲͷػೳൺֱɺ ϥΠϒϥϦɺӡ༻ݕ౼ • ೝূɺೝՄ • ࠷ۙͷಈ޲ɺ֤ݴޠͷػೳൺֱɺϥΠϒϥϦɺӡ༻ݕ౼ • ςετ • ϢχοτςετɺΠϯςάϨʔγϣϯςετɺE2EɺϞοΫɺConsumer-Driven Contracts • ΠϯϑϥɺωοτϫʔΫɺAPIήʔτ΢ΣΠɺ؂ࢹɺϩΪϯάɺτϨʔγϯά…
  6. Web APIσβΠϯͷమଇ >Web APIͷΞʔΩςΫνϟελΠϧʹ ͸RESTɺRPCɺSOAPͳͲ͕͋Δ > RPCελΠϧͷಛ௃: 
 Remote Procedure

    Callͷ໊લͷ௨Γɺ ϦϞʔτϗετͷؔ਺Λݺͼग़͢Α͏ͳ ελΠϧɻҰൠతͳϓϩάϥϛϯάͷߟ ͑ํΛͦͷ··Ԡ༻͠΍͍͢ɻ > RPC͕޲͍͍ͯΔ৔߹: 
 ΫϥΠΞϯτ͕ࣾ಺ʹݶఆ͞Ε͍ͯͨΓɺ SDKͷΑ͏ͳܗͰඞͣϥοϓ͞Ε͍ͯΔ ৔߹
  7. Web APIσβΠϯͷమଇ >Web APIͷΞʔΩςΫνϟελΠϧʹ ͸RESTɺRPCɺSOAPͳͲ͕͋Δ > RPCελΠϧͷಛ௃: 
 Remote Procedure

    Callͷ໊લͷ௨Γɺ ϦϞʔτϗετͷؔ਺Λݺͼग़͢Α͏ͳ ελΠϧɻҰൠతͳϓϩάϥϛϯάͷߟ ͑ํΛͦͷ··Ԡ༻͠΍͍͢ɻ > RPC͕޲͍͍ͯΔ৔߹: 
 ΫϥΠΞϯτ͕ࣾ಺ʹݶఆ͞Ε͍ͯͨ ΓɺSDKͷΑ͏ͳܗͰඞͣϥοϓ͞Εͯ ͍Δ৔߹ ຊ౰ʹඞཁͩͬͨ΋ͷ ϚΠΫϩαʔϏεͷΑ͏ͳ؀ڥ
  8. Web APIσβΠϯͷమଇ >Web APIͷΞʔΩςΫνϟελΠϧʹ ͸RESTɺRPCɺSOAPͳͲ͕͋Δ > RPCελΠϧͷಛ௃: 
 Remote Procedure

    Callͷ໊લͷ௨Γɺ ϦϞʔτϗετͷؔ਺Λݺͼग़͢Α͏ͳ ελΠϧɻҰൠతͳϓϩάϥϛϯάͷߟ ͑ํΛͦͷ··Ԡ༻͠΍͍͢ɻ > RPC͕޲͍͍ͯΔ৔߹: 
 ΫϥΠΞϯτ͕ࣾ಺ʹݶఆ͞Ε͍ͯͨΓɺ SDKͷΑ͏ͳܗͰඞͣϥοϓ͞Ε͍ͯΔ ৔߹ gRPC (ޙड़)
  9. gRPCͱ͸ • Google͕ެ։ͨ͠RPCϑϨʔϜϫʔΫ • Google͕ࣾ಺Ͱ࢖༻͍ͯͨ͠RPCϑϨʔϜϫʔΫͷOSS൛ • Protocol Buffers *1ʹΑΓαʔϏεͷΠϯλʔϑΣΠεఆٛ •

    αʔόɺΫϥΠΞϯτࣗಈੜ੒ • ௨৴ϓϩτίϧ͕HTTP/2 • ߴ଎ • ैདྷܕͷϦΫΤετ/ϨεϙϯεܗࣜҎ֎ʹ୯ํ޲ɺ૒ํ޲ Streaming΋Մೳ *1. gRPCͷγϦΞϥΠβ͸ଞͷϑΥʔϚοτ΋αϙʔτ͍ͯ͠·͕͢ɺ͜ͷࢿྉͰ͸Protocol BuffersΛ࢖༻͢Δલఏ
  10. Protocol Buffers? • ߏ଄Խ͞ΕͨσʔλΛγϦΞϥΠζ ͢ΔͨΊͷݴޠඇґଘͳϓϥοτ ϑΥʔϜ • IDL(ΠϯλʔϑΣΠεఆٛݴޠ) ʹΑΓهड़ •

    ओཁͳݴޠ΁ͷιʔείʔυੜ੒ • ߴ଎ɺܰྔ • ޙํޓ׵ੑͷ͋ΔγϦΞϥΠζϑΥʔ Ϛοτ • IDLʹΑΔߏ଄ఆٛ • γϦΞϥΠζ • σγϦΞϥΠζ
  11. gRPC࠾༻ࣄྫ • ւ֎ • Google • Square • Netflix •

    CoreOS • Docker • Cisco • ࠃ಺ • ϝϧΧϦ • GRPCͷ࣮ફͱݱঢ়Ͱͷར఺ܽ఺ *1 • AbeamTV • AbeamTVʹ͓͚ΔMicroservices *2 • υϫϯΰ • Protocol Buffers *3 *1. https://speakerdeck.com/kazegusuri/go-conference-2016-spring *2. http://www.slideshare.net/RyotaNishio/abematvmicroservices-architecture *3. http://tayama0324.github.io/slides/20161127-kbkz-tech.html#/
  12. URLɾύϥϝʔλɾΤϥʔઃܭ • IDLΛ࢖༻͠ɺҰൠతͳϓϩάϥϛϯάͷΑ͏ʹ هड़ • ఆٛࡁΈߏ଄ • جຊܕ • repeatɺmapɺenum

    • messageɺstreamɺserviceɺnestͳͲ • Τϥʔ • ࢓༷ͱͯ͠ఆٛ *1 • ϥΠϒϥϦ૊ΈࠐΈ *2 *1. http://www.grpc.io/docs/guides/error.html *2. ex:Java) http://www.grpc.io/grpc-java/javadoc/io/grpc/Status.html
  13. υΩϡϝϯτ؅ཧɺπʔϧ • IDL͕ͦͷ··ΠϯλʔϑΣΠευΩϡϝϯτ • ؔ࿈ΤίγεςϜ • protoc-gen-doc: IDL͔ΒMarkdown, HTMLͳͲͷυΩϡϝϯτੜ੒*1 •

    grpc-gateway: gRPCαʔό͔ΒJSONϨεϙϯεΛฦͨ͢ΊͷϦόʔεϓϩ Ωγ*2 • polyglot: grpcͷCLIΫϥΠΞϯτ*3 => IDL͔Β։ൃ͢ΔελΠϧ͕֬ఆ͍ͯ͠ΔͨΊɺͦΕΛϕʔεͱͯ͠Τίγες Ϝ͕ߏங͠΍͍͢ => ۀ຿ݻ༗ͷπʔϧͳͲͰ΋ੜ੒͢ΔίʔυͷݴޠΛબ୒Ͱ͖ΔͨΊѻ͍΍͍͢ • *1. https://github.com/estan/protoc-gen-doc • *2. https://github.com/grpc-ecosystem/grpc-gateway • *3. https://github.com/grpc-ecosystem
  14. όϦσʔγϣϯ • ެࣜʹ͸ͳ͠(Protocol Buffers) • IDLͰఆٛͰ͖ΔόϦσʔγϣϯʹ͸ݶք͕͋Δ • ૬ؔόϦσʔγϣϯͳͲͷϩδοΫΛؚΉόϦσʔγϣϯʹ͸ධՁ͢Δ ͨΊͷॲཧܥ͕ඞཁ •

    ೖྗνΣοΫ/ϏδωεϩδοΫͷڥք൑அ͕೉͍͠ • ֤ݴޠɺαʔϏεؒΛލ͍ͩҰݩ؅ཧ͸࣮૷ϨϕϧͰ୼ͼ͕ੜ·Ε΍͢ ͍ • ϑϩϯτΤϯυͱҧͬͯɺ1จࣈೖྗ୯Ґͷଈ࣌௨஌ͳͲ͸ͳͯ͘΋Α͍ => ݱ࣌఺Ͱ͸IDLʹίϝϯτϕʔεͰهड़͢Δͷ͕ݱ࣮త?
  15. ςετ • ੜ੒͞ΕΔίʔυΛ׆༻ʢ࣮૷ݴޠґଘ͋Γʣ • ΫϥΠΞϯτ • αʔό • ΠϯλʔϑΣΠε •

    ϞοΫ => ιʔείʔυੜ੒Λߦ͏ͨΊɺ࣮ࡍʹ࢖༻͢Δίʔυϕʔεͷςετ͕Մೳ • ςετʹϞοΫ͸ඞཁ͔ʁ • https://github.com/grpc/grpc-java/issues/ 1469#issuecomment-230852433
  16. Other Pros • ܰྔɺߴ଎ͳγϦΞϥΠζ௨৴ • σʔλߏ଄΍ݴޠɺ؀ڥʹ΋ґଘ͢Δ΋ͷͷɺଟ͘ͷ৔߹XML΍JSONΑ Γߴ଎ • ετϦʔϛϯάαϙʔτ •

    αʔόϓογϡ΍େ༰ྔͷ௨৴ͳͲ΋Մೳ • Protocol BuffersҎ֎ͷγϦΞϥΠζϑΥʔϚοτʹ΋ରԠ(JSONɺXMLͳͲ) • ΫϥΠΞϯτϩʔυόϥϯγϯά • ϔϧενΣοΫ
  17. Cons • ֶशίετ • RESTful APIઃܭɺӡ༻Λ໢ཏతʹཧղ͢Δͷʹൺ΂গͳ͍͕ɺ࢓༷ʹͦΕͳΓͷϘ ϦϡʔϜ => ॳظ౤ࢿ͸͋΋ͷͷɺͦΕΛ௒͑Ε͹RESTΑΓΑ͍ (ϝϧΧϦɺAbemaTV)

    • γϦΞϥΠζ͞Ε͍ͯΔͨΊJSONͷΑ͏ʹؾܰʹݺͼग़͠ɺσόοάͰ͖ͳ͍ => grpc-gatewayɺpolyglotͳͲͷ׆༻ => ֤ΞϓϦέʔγϣϯ಺ͰΠϯλʔηϓλʔΛ࢖͏ • ϒϥ΢β͔Β࢖͑ͳ͍ => ϚΠΫϩαʔϏεόοΫΤϯυͰ͸໰୊ͳ͍ => ࠓ͙͢ඞཁͰ͋Ε͹grpc-gatewayɺকདྷతʹ͸ϒϥ΢βରԠ*1ʹظ଴ • HTTP/2पΓͷΠϯϑϥߏங => Ϋϥ΢υ΍KubernetesͳͲϚωʔδυͳػೳͷ؀ڥ׆༻ *1. https://github.com/grpc/grpc-experiments/issues/159
  18. REST APIઃܭͱͷҧ͍ • جຊతʹαʔϏεؒͷΠϯλʔϑΣΠεఆٛ(IDL)Λ࠷ॳʹ࣮ࢪ
 => ݴޠʹඇґଘͰαʔϏεؒͷΠϯλʔϑΣΠε߹ҙΛߦ͏ελΠϧ
 => RESTͷݪଇɺΠϯϐʔμϯεϛεϚονղফͷͨΊͷཧ࿦ͳͲ͸ඞཁͳ͘ɺ௨ৗͷϓϩάϥϛϯά Ϟσϧʹ͍ۙ
 =>

    ܗࣜͷ౷Ұ͞ΕͨυΩϡϝϯτͱͯ͠΋׆༻Ͱ͖Δ • αʔόɺΫϥΠΞϯτࣗಈੜ੒͕جຊ
 => ݴޠ͝ͱͷϥΠϒϥϦબఆͳͲ͕ෆཁ
 => ௨৴෦෼ͷ࣮૷΋ෆཁ • Google͕։ൃͨ͠ͷ΋͋ΓɺϚΠΫϩαʔϏεʹඞཁͳػೳ͕ఆٛɺ૊ΈࠐΈͰ༻ҙ͞Ε͍ͯΔ • ΤϥʔɺೝূɺೝՄɺετϦʔϛϯάɺϩʔυόϥϯγϯάɺϔϧενΣοΫ… => αʔϏεؒ࿈ܞʹඞཁͳ࢓૊Έɺػೳͷଟ͕͘ఏڙ͞ΕΔɹɹ ɹ=> ػೳ࣮૷ʹूத͠΍͍͢
  19. ·ͱΊ • ϚΠΫϩαʔϏεͰ͸ैདྷͷγεςϜ಺ͷؔ਺ݺͼग़ͩͬͨ͠ॲཧ͕ɺωο τϫʔΫΛܦ༝ • ௨৴ʹ͸HTTPΛ༻͍ͨAPI͕Α͘࢖ΘΕΔ • RESTful API •

    े෼ͳφϨοδɺΤίγεςϜ • ϑϩϯτΤϯυͱͷ਌࿨ੑ • Ұఆͷݪଇͷఏڙ • gRPC • ϚΠΫϩαʔϏεʹٻΊΒΕΔ௨৴࢓༷ͷఏڙ • αʔϏεؒ࿈ܞʹඞཁͳ࣮૷ͷੜ੒ • େखاۀʹࢧ͑ΒΕͨ࠾༻࣮੷