Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
70 slides
-
Outline • RPC ͱ • gRPC ͱ • Protocol Buffers
ͱ • gRPC + Protocol Buffers ͷಋೖ • ։ൃ্ͷ • protoϑΝΠϧͷڞ༗ํ๏ͷྫ -
RPC > Remote procedure call is the synchronous language-level transfer
of control between programs in disjoint address spaces whose primary communication medium is a narrow channel. ଞͷΞυϨεۭؒʢe.g. ωοτϫʔΫ্ͷαʔόʣʹ͋ΔॲཧΛ ࣮ߦ͢Δ͜ͱɻ Bruce Jay Nelson. 1981. Remote Procedure Call. Ph.D. Dissertation. Carnegie Mellon Univ., Pittsburgh, PA, USA. AAI8204168. https://dl.acm.org/citation.cfm?id=910306 -
RPC as HTTP API σʔλͷදݱܗࣜ JSON, XML, Protocol Buffers, …
REST, RPC, … APIͷઃܭํ -
REST vs RPC as HTTP API • ҧ͍ͷ1ͭ:ʮAPIઃܭ࣌ʹͲ͜ʹண͢Δ͔ʯ • Ϧιʔεʢૢ࡞ରʣͱΞΫγϣϯʢૢ࡞ํ
๏ʣΛͲ͏ͬͯදݱ͢Δ͔ • REST: ϦιʔεΛத৺ʹߟ͑Δ • RPC: ΞΫγϣϯΛத৺ʹߟ͑Δ -
RESTͷํ͕͍͍ͯΔέʔε • e.g. ϒϩάαʔϏεͷAPI • APIͷओͳׂϒϩάهࣄʹରͯ͠CRUD ૢ࡞Λ͢Δ͜ͱ • هࣄͱ͍͏ϦιʔεΛத৺ʹߟ͑ͨํ͕ྑ͍ •
ΞΫγϣϯHTTPϝιουͰදݱ https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ -
ϒϩάAPI: RESTͷ߹ • Create: • POST https://example.com/api/articles • body: {“title”:
“New post”, “author”: “John”, “description”: “…”} • Read: • GET https://example.com/api/articles/123 • Update: • PATCH https://example.com/api/articles/123 • body: {“title”: “Revised”} • Delete: • DELETE https://example.com/api/articles/123 https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ -
ϒϩάAPI: RPCͷ߹ • Create: • https://example.com/api/createArticle • body: {“title”: “New
post”, “author”: “John”, “description”: “…”} • Read: • https://example.com/api/getArticle • Update: • https://example.com/api/updateArticle • body: {“article_id”: 123, “title”: “Revised”} • Delete: • https://example.com/api/deleteArticle https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ -
RPCͷํ͕͍͍ͯΔέʔε • e.g. SlackͷΑ͏ͳνϟοταʔϏεͷAPI • APIΛ௨ͯ͡ҎԼͷػೳΛఏڙ͍ͨ͠ͱ͢Δ • ͋Δνϟϯωϧʹ͍ΔϢʔβ͕νϟϯωϧ͔Βୀग़ ͢Δ (leave)
• ͋Δνϟϯωϧʹ͍ΔϢʔβΛνϟϯωϧ͔Β͍ ग़͠ɺ࠶ࢀՃΛې͡Δʢkickʣ https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ -
νϟοτAPI: RESTͷ߹ • leaveͱkickͷҧ͍Λ៉ྷͳRESTͰදݱ͢Δͷ͕͍͠ • e.g. bodyʹԿ͔͠ΒͷใΛՃ͢Δͷ͕ඞཁ • leave: •
PATCH https://example.com/api/channels/123/users/456 • body: {“status”: “left”} • kick: • PATCH https://example.com/api/channels/123/users/456 • body: {“status”: “kicked”} https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ -
νϟοτAPI: RESTͷ߹ • ҎԼͷΑ͏ʹ͢ΔͱRESTͱRPCͷதؒͷΑ͏ͳͷʹͳͬͯ͠·͏ • leave: • POST https://example.com/api/channels/123/users/456/leave •
kick: • POST https://example.com/api/channels/123/users/456/kick https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ -
νϟοτAPI: RPCͷ߹ • leave: • https://example.com/api/leaveFromChannel • body: {“channel_id”: 123,
“user_id”: 456} • kick: • https://example.com/api/kickFromChannel • body: {“channel_id”: 123, “user_id”: 456} https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/ -
Slackͷweb APIRPC > The Web API is a collection of
HTTP RPC-style methods, all with URLs in the form `https://slack.com/api/METHOD_FAMILY.method`. https://api.slack.com/web Web APIHTTP RPCελΠϧͷϝιουͷू·ΓͰ͋ΓɺશͯҎ ԼͷϑΥʔϜͰද͞Ε·͢ɻ `https://slack.com/api/METHOD_FAMILY.method`. -
ͦͷଞͷAPI • Dropbox API v2ͷେRPC • AWS APIͷҰ෦RPC • Discord
APIREST https://www.dropbox.com/developers/documentation/http/documentation https://docs.aws.amazon.com/Route53/latest/APIReference/requests-rpc.html https://github.com/discordapp/discord-api-docs/blob/master/docs/Reference.md -
ͦͷଞͷREST, RPCͷൺֱݕ౼ʹ ؔ͢Δࢀߟ • https://speakerdeck.com/petitviolet/web-api- design • https://www.slideshare.net/disc99_/apirestgrpc • https://apihandyman.io/do-you-really-know-why-
you-prefer-rest-over-rpc/ • https://philsturgeon.uk/api/2017/12/18/rest- confusion-explained/ -
gRPC • Google͕։ൃΛ࢝ΊͨΦʔϓϯιʔεͷRPCϑϨʔϜϫʔΫ • ݩGoogleࣾͷϚΠΫϩαʔϏεΛͭͳ͛ΔͨΊʹ10 Ҏ্ΘΕ͍ͯͨRPCϑϨʔϜϫʔΫ”Stubby” • HTTP/2 • ํετϦʔϛϯά
• ެࣜʹAndroid JavaؚΊ10ݴޠʹରԠ https://grpc.io/blog/principles -
gRPCͷσʔλදݱܗࣜ • σϑΥϧτͰProtocol Buffers (Protobuf) • C++ͱGoͰFlatBuffersαϙʔτ͞Εͯ ͍Δͱͷ͜ͱ • ଞͷϑΥʔϚοτٕज़తʹରԠՄೳͱ
ॻ͍ͯ͋Δ͕ɺݱ࣌ͰରԠࡁΈ͔ผ https://grpc.io/blog/flatbuffers https://grpc.io/faq/ -
͍ͪAndroid։ൃऀ͔Βݟͨ gRPCͷσϝϦοτ • ੈͷதͷใ͕·ͩेͰͳ͍ • e.g. TLS 1.2͕ຊʹͪΌΜͱΘΕ͍ͯΔͷ͔ΛௐΔͨΊʹ grpc-javaͷιʔείʔυΛಡΜͩΓ •
https://github.com/grpc/grpc-java/blob/master/ SECURITY.md ͳͲެࣜͷࢿྉ͋Γ·͢ • e.g. AndroidΫϥΠΞϯτJava, GoͰॻ͔Εͨαʔόͱ௨৴Ͱ ͖ΔͷʹNode.js, RubyͰॻ͔Εͨαʔόͱ௨৴Ͱ͖ͳ͍ࣄʹ ૺ۰ͨ͠Γʢ2017ͷʣ -
Twitchͷݴ͏gRPCͷσϝϦοτ • ෦࣮ͷෳࡶ͞ʹىҼ͢Δ • HTTP/1.1Λ͑ͳ͍͜ͱʹΑΔޓੑͷ • ProtobufͷΤϯίʔυܗ͕ࣜόΠφϦͰѻ͍ ͮΒ͍ • etc
https://blog.twitch.tv/twirp-a-sweet-new-rpc-framework-for-go-5f2febbf35f -
gRPCͷର߅അ”Twirp” • 20181݄ެ։ͷTwitchRPCϑϨʔϜϫʔΫ • γϯϓϧ͔ͭHTTP/1.1Λ͑ΔʢHTTP/2Մʣ • ࣮ʹProtobufΛ͏͕ɺbodyͷΤϯίʔυܗࣜProtobufʢόΠφϦʣ͚ͩͰͳ͘ JSONՄ • ࣮Protobufࣗମ͕JSONΤϯίʔσΟϯάͰ͖ΔʢJSON
Mappingʣ • gRPCͰඇαϙʔτͷ༷ • ํετϦʔϛϯάඇαϙʔτ • TwitchGoɺαʔυύʔςΟJava, Lua, JS, RubyͳͲ https://blog.twitch.tv/twirp-a-sweet-new-rpc-framework-for-go-5f2febbf35f https://groups.google.com/forum/#!topic/grpc-io/ncVgOlp16F8 -
Protocol Buffers (Protobuf) • XMLͷΑ͏ʹߏԽ͞Εͨσʔλදݱܗࣜɺ ͳ͍͠IDL (Interface Description Language) •
ݱࡏͷ࠷৽ver.3 (proto3) > think XML, but smaller, faster, and simpler. https://developers.google.com/protocol-buffers/docs/overview -
.protoϑΝΠϧ syntax = "proto3"; message Bank { int32 code =
1; string name = 2; string name_kana = 3; } https://developers.google.com/protocol-buffers/docs/overview -
Protobufͱ • .protoϑΝΠϧΛαʔόɾΫϥΠΞϯτؒͰڞ༗ • Protobuf compilerʹΑͬͯ.protoϑΝΠϧ͔ΒγϦΞϥΠζɾσγϦΞϥΠ ζ༻ͷΫϥε͕ੜ͞ΕΔ • όΠφϦʹΤϯίʔυͯ͠ૹ৴ •
JSONΤϯίʔυ͢Δ͜ͱՄ • ͨͩ͠ઌड़ͷ௨ΓgRPCͰະαϙʔτɺTwirpͰαϙʔτ͍ͯ͠Δ༷ • ެࣜɾαʔυύʔςΟ߹Θͤͯ10छྨҎ্ͷݴޠʹରԠ https://developers.google.com/protocol-buffers/docs/overview -
γϦΞϥΠζ (Java) Bank bank = Bank.newBuilder() .setCode(123) .setName("υϩΠυۜߦ") .setNameKana("υϩΠυΪϯί") .build();
output = new FileOutputStream(path); bank.writeTo(output); https://developers.google.com/protocol-buffers/docs/overview -
σγϦΞϥΠζ (C++) Bank bank; fstream input(path, ios::in | ios::binary); bank.ParseFromIstream(&input);
int code = bank.code(); string name = bank.name(); string nameKana = bank.nameKana(); https://developers.google.com/protocol-buffers/docs/overview -
ྻͱmessageͷೖΕࢠ message GetBanksResponse { repeated Bank bank = 1; }
message Bank { int32 code = 1; string name = 2; string name_kana = 3; } https://developers.google.com/protocol-buffers/docs/overview -
Enum enum BankAccountType { SAVING = 0; // ී௨ CHECKING
= 1; // ࠲ } https://developers.google.com/protocol-buffers/docs/overview -
Enum enum BankAccountType { option allow_alias = true; SAVING =
0; // ී௨ CHECKING = 1; // ࠲ CURRENT = 1; // ࠲ʢผ໊ʣ } https://developers.google.com/protocol-buffers/docs/overview -
@Deprecated message Bank { int32 code = 1; string name
= 2; string name_kana = 3 [deprecated=true]; string name_katakana = 4; } https://developers.google.com/protocol-buffers/docs/overview -
RPC service BankListService { rpc GetBanks (GetBanksRequest) returns (GetBanksResponse); }
message GetBanksRequest {} message GetBanksResponse { repeated Bank bank = 1; } https://developers.google.com/protocol-buffers/docs/overview -
͍ͪAndroid։ൃऀ͔Βݟͨ ProtobufͷϝϦοτ • ܕ͕͋Δɹ • messageʹ૬͢ΔΫϥε͕ࣗಈੜ͞ΕΔͷͰࣗͰॻ͘ඞཁ͕ͳ͍ • αʔόαΠυͷ։ൃऀͱίϛϡχέʔγϣϯ͍͢͠ • protoϑΝΠϧ͕ͦͷ··༷ʹͳΔ
• protoϑΝΠϧ͕؆қͳͷͰཉ͍͠ΫϥεAPI͕͋ͬͨΒprotoϑΝ ΠϧΛॻ͍ͯPRΛૹΕྑ͍ • ͪΖΜαʔόαΠυͷ࣮͕ඞཁʹͳΔ -
͍ͪAndroid։ൃऀ͔Βݟͨ ProtobufͷσϝϦοτ • όΠφϦͰ௨৴͞ΕΔͷͰhuman readableͰͳ͍ • ※ JSON Mappingػೳ͋Δ͕ɺgRPCͰඇαϙʔτͷ༷ •
curlcharlesͳͲͰσόοάͰ͖ͳ͍ɺϒϥβͰΞΫηεͯ͠Ϩεϙϯ εΛݟΔ͜ͱ͕Ͱ͖ͳ͍ • ͪΌΜͱ௨৴Ͱ͖͍ͯΔ͔Ͳ͏͔Λ֬ೝ͢ΔͨΊʹ؆୯ͳΫϥΠΞϯτΛ ॻ͍͓ͯ͘ඞཁ͕͋Δ • αʔόɾΫϥΠΞϯτؒͷprotoϑΝΠϧΛྑ͍ײ͡ʹಉظ͢ΔΈ͕ඞཁ • ҰྫΛޙड़͠·͢ -
͜ͷ߲ͷ·ͱΊ • Protocol BuffersߏԽ͞Εͨσʔλදݱܗࣜͳ͍͠ IDL (Interface Description Language) • protoϑΝΠϧΛॻ͍ͯαʔόɾΫϥΠΞϯτؒͰڞ༗
• Ϧονͳදݱྗ • ProtobufίϯύΠϥͰmessageʹ૬͢ΔΫϥεͷࣗ ಈੜ -
: ඞͣެࣜͷREADMEΛ֬ೝ • gRPC-Java: • https://github.com/grpc/grpc-java • Android͚ͱͦͷଞͷJavaڥ͚Ͱ༰͕ҟͳΔͷͰࠞಉ͠ͳ͍ཁ ҙ •
Protobuf: • https://github.com/google/protobuf-gradle-plugin • αϯϓϧ: • https://github.com/grpc/grpc-java/tree/master/examples/android -
// build.gradle buildscript { ... dependencies { ... classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
} } // app/build.gradle apply plugin: ‘com.android.application’ apply plugin: 'com.google.protobuf' dependencies { ... compile 'io.grpc:grpc-okhttp:1.9.0' compile 'io.grpc:grpc-protobuf-lite:1.9.0' compile 'io.grpc:grpc-stub:1.9.0' } -
// app/build.gradle protobuf { protoc { artifact = ‘com.google.protobuf:protoc:3.5.1-1’ }
plugins { javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = “io.grpc:protoc-gen-grpc-java:1.9.0” } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } } -
3. protoϑΝΠϧΛॻ͘ • src/main/proto src/test/proto ҎԼʹproto ϑΝΠϧΛஔ͘ • ҎԼͷίϚϯυͰprotoϑΝΠϧ͕ίϯύΠ
ϧՄೳ͔֬ೝͰ͖Δ • $ protoc —java_out=<PATH> foo.proto -
service BankListService { rpc GetBanks (GetBanksRequest) returns (GetBanksResponse); } message
GetBanksRequest {} message GetBanksResponse { repeated Bank bank = 1; } -
ManagedChannel channel = OkHttpChannelBuilder .forAddress("example.com", "443") .build(); BankListServiceStub stub =
BankListServiceGrpc.newStub(channel); GetBanksRquest request = GetBanksRequest .newBuilder() .build(); stub.getBanks(request, new StreamObserver<GetBanksResponse>() { @Override public void onNext(GetBanksResponse response) { List<Bank> banks = response.getBankList() ... } @Override public void onError(Throwable throwable) { ... } @Override public void onCompleted() { ... } ); -
͜ͷ߲ͷ·ͱΊ • AndroidͰgRPC-JavaͱProtocol Buffers Gradle Plugin͕ඞཁ • gRPC + Protocol
Buffersͷಋೖ࣌ඞͣެࣜ ϦϙδτϦͷREADMEΛ֬ೝʂʂ -
֤repoͷprotoϑΝΠϧͷಉظ proto files repo iOS app repo Android app repo
API server repo proto files proto files proto files ྑ͍ײ͡ʹprofo files repoΛίϐʔ or ࢀর͍ͨ͠ -
proto files repo Digdag GCP Cloud Functions iOS app repo
Android app repo API server repo 1. PRϚʔδͰwebhookૹ৴ 2. DigdagϫʔΫϑϩʔIDΛ ɹࢦఆͯ͠ϫʔΫϑϩʔىಈ 3. ֤repoͷprofo filesΛ ɹߋ৽͢ΔPRΛ࡞ 4. PRͷࠩΛݟͯϚʔδʢखಈʣ -
proto files repo Digdag GCP Cloud Functions iOS app repo
Android app repo API server repo 1. PRϚʔδͰwebhookૹ৴ 2. DigdagϫʔΫϑϩʔIDΛ ɹࢦఆͯ͠ϫʔΫϑϩʔىಈ 3. ֤repoͷprofo filesΛ ɹߋ৽͢ΔPRΛ࡞ 4. PRͷࠩΛݟͯϚʔδʢखಈʣ -
proto files repo Digdag GCP Cloud Functions iOS app repo
Android app repo API server repo 1. PRϚʔδͰwebhookૹ৴ 2. DigdagϫʔΫϑϩʔIDΛ ɹࢦఆͯ͠ϫʔΫϑϩʔىಈ 3. ֤repoͷprofo filesΛ ɹߋ৽͢ΔPRΛ࡞ 4. PRͷࠩΛݟͯϚʔδʢखಈʣ -
exports.syncTheoProto = function syncTheoProto (req, res) { ... postData =
{ "workflowId": settings.workflowId, ... } req.write(JSON.stringify(postData)); req.end(); ... }; POSTϦΫΤετʹ༧Ί࡞͓͍ͬͯͨ DigdagϫʔΫϑϩʔͷIDΛՃ͢Δ -
proto files repo Digdag GCP Cloud Functions iOS app repo
Android app repo API server repo 1. PRϚʔδͰwebhookૹ৴ 2. DigdagϫʔΫϑϩʔIDΛ ɹࢦఆͯ͠ϫʔΫϑϩʔىಈ 3. ֤repoͷprofo filesΛ ɹߋ৽͢ΔPRΛ࡞ 4. PRͷࠩΛݟͯϚʔδʢखಈʣ -
proto files repo Digdag GCP Cloud Functions iOS app repo
Android app repo API server repo 1. PRϚʔδͰwebhookૹ৴ 2. DigdagϫʔΫϑϩʔIDΛ ɹࢦఆͯ͠ϫʔΫϑϩʔىಈ 3. ֤repoͷprofo filesΛ ɹߋ৽͢ΔPRΛ࡞ 4. PRͷࠩΛݟͯϚʔδʢखಈʣ -
࠷ޙʹ… Swagger or GraphQL or gRPC??? • গͳ͘ͱݱ࣌ͰͲΕ͔͕ઈରతʹྑ͍ͱ͍͏͜ͱແͦ͞͏ • ൺֱهࣄ͕ࢁ͋Δ
• e.g. https://brandur.org/api-paradigms • ͜ͷهࣄʹର͢Δٞ: https://news.ycombinator.com/item?id=14003134 • DroidKaigi 2018Ͱൃද͞Εͨʂ • Swagger: https://speakerdeck.com/magiepooh/madaapiding-yi-guan-li- dexiao-hao-siterufalse-swaggerwoyong-itada-gui-mo-apurishi-dai-falseapiding- yi-guan-li-tokodozienereto • GraphQL: https://speakerdeck.com/gfx/subarasikigraphqlfalsesekaiheyoukoso -
ൃදͷ·ͱΊ • RESTͱRPCదࡐదॴ • gRPCGoogleൃͷRPCϑϨʔϜϫʔΫ • ProtobufߏԽ͞Εͨσʔλදݱܗࣜͳ͍͠IDL • gRPC +
ProtobufͰίʔυΛࣗಈੜ • ಋೖ࣌ඞͣ࠷৽ͷެࣜREADMEΛ֬ೝ • ProtobufΛαʔόɾΫϥΠΞϯτؒͰಉظ͢ΔΈΛ࡞Ζ͏ • SwaggerGraphQLʹ͍ͭͯௐͯΈΑ͏