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

grpc-swiftの紹介

 grpc-swiftの紹介

E1cf27d9fa565b2867d171f21a39759c?s=128

tikidunpon

May 23, 2018
Tweet

Transcript

  1. gRPC-swiftͷ঺հ Origami ాத @tikidunpon ROPPONGI.swift ୈ3ճʢOrigamiɾVISITSڞ࠵ʣ

  2. gRPCͱ͸ʁ

  3. gRPCͱ͸ʁ • Google͕։ൃͨ͠RPCϑϨʔϜϫʔΫ • σϑΥϧτ͸HTTP/2্Ͱಈ࡞ • σϑΥϧτͷIDLʢΠϯλʔϑΣʔεهड़ݴޠʣʹProtocol BuffersΛ࠾༻ • ෳ਺ݴޠʹରԠ

  4. ͦ΋ͦ΋RPCͱ͸ʁ

  5. RPCͱ͸ʁ • Remote Procedure Callͷུ • ϓϩάϥϜ͔ΒผͷΞυϨεۭؒʹ͋ΔϓϩάϥϜΛ࣮ߦՄೳ ʹ͢Δٕज़

  6. RPCͱ͸ʁ • ͬ͘͟Γͱͨ͠ྺ࢙

  7. Protocol Buffersͱ͸ʁ • Google͕։ൃͨ͠IDLʢΠϯλʔϑΣʔεهड़ݴޠʣ • ௨৴΍ӬଓԽͰͷར༻Λ໨తͱͨ͠γϦΞϥΠζϑΥʔϚοτ • .protoϑΝΠϧʹαʔϏεͱϝοηʔδΛܕ෇͖Ͱఆٛ • proto2Ͱ͸optional/requiredͳͲ͕දݱͰ͖ͨ

    • ࠷৽ͷproto3ͰAPIઃܭͷ୯७Խɺ҆ఆԽɺύϑΥʔϚϯε޲্ ͷͨΊ࡟আ͞Εͨ
  8. Protocol Buffersͱ͸ʁ syntax = "proto3"; // όʔδϣϯఆٛ service Greeter {

    // αʔϏεఆٛ rpc SayHello (HelloRequest) returns (HelloReply) {}ɹ // RPCϝιουఆٛ } message HelloRequest {ɹ// ϦΫΤετϝοηʔδఆٛ string name = 1; // ϑΟʔϧυܕ ϑΟʔϧυ໊ ϑΟʔϧυ൪߸ } message HelloReply { // Ϩεϙϯεϝοηʔδ string message = 1; }
  9. gRPC͸ଞͷRPCͱԿ͕ҧ͏ͷ͔ʁ

  10. gRPCͷઃܭݪଇͱཁ݅

  11. gRPCͷઃܭݪଇͱཁ݅ • αʔϏε͸ΦϒδΣΫτͰ͸ͳ͘ɺϝοηʔδ͸ࢀর͞Εͳ͍ ϚΠΫϩαʔϏεͱ෼ࢄΦϒδΣΫτͷୈ1๏ଇͱ෼ࢄΦϒδΣ Ϋτͷམͱ݀͠Λආ͚ɺγεςϜؒͰૈཻʹϝοηʔδΛަ׵ ͢ΔϚΠΫϩαʔϏεઃܭ఩ֶΛଅਐ͢Δɻ

  12. gRPCͷઃܭݪଇͱཁ݅ • ΧόϨοδ&γϯϓϧ͞ ීٴ͍ͯ͠ΔશϓϥοτϑΥʔϜͰ؆୯ʹ։ൃͰ͖ΔɺCPUͱ ϝϞϦ͕ݶΒΕͨσόΠεͰ࣮ߦͰ͖Δɻ • ϑϦʔ&Φʔϓϯ جຊతͳػೳΛ͢΂ͯແྉͰ࢖༻Ͱ͖ɺ͢΂ͯͷ੒Ռ෺ΛOSS ͷऔΓ૊Έͱͯ͠ɺٕज़ͷ࠾༻Λ๦͛ͳ͍Α͏ͳϥΠηϯεͰ ղ์͢Δ͜ͱɻ

  13. gRPCͷઃܭݪଇͱཁ݅ • ૬ޓӡ༻ੑ&Ϧʔν ϫΠϠϓϩτίϧ͸ɺҰൠతͳΠϯλʔωοτΠϯϑϥετϥ ΫνϟΛӽ͑ͯଘଓ͢Δ͜ͱ͕Ͱ͖Δ͜ͱɻ • ൚༻ੑ&ύϑΥʔϚϯε ٕज़ελοΫ͸ɺϢʔεέʔεݻ༗ͷRPCελοΫͱൺֱͯ͠ ύϑΥʔϚϯεΛ΄ͱΜͲଛͳ͏͜ͱͳ͘ɺ෯޿͍Ϣʔεέʔ εʹద༻ՄೳͰ͋Δ͜ͱɻ

  14. gRPCͷઃܭݪଇͱཁ݅ • ϨΠϠԽ ελοΫͷॏཁͳ෦෼͸ɺಠཱͯ͠ਐԽͰ͖Δ͜ͱɻϫΠϠϑ ΥʔϚοτͷվగ͸ɺΞϓϦέʔγϣϯϨΠϠͷόΠϯυΛ๦ ͛ͯ͸͍͚ͳ͍ɻ

  15. gRPCͷઃܭݪଇͱཁ݅ • ϖΠϩʔυඇґଘ ༷ʑͳαʔϏεͰ͸Protocol BuffersɺJSONɺXMLɺThriftͳͲ ҟͳΔϝοηʔδλΠϓͱΤϯίʔσΟϯά͕ඞཁͱͳΔɻϓ ϩτίϧͱ࣮૷͸͜ΕΛՄೳʹ͢Δ͜ͱɺಉ༷ʹࠩ͠ସ͑Մೳ ͳϖΠϩʔυͷѹॖϝΧχζϜΛఏڙ ͢Δ͜ͱɻ

  16. gRPCͷઃܭݪଇͱཁ݅ • ετϦʔϛϯά ετϨʔδγεςϜ͸ετϦʔϛϯάͱϑϩʔ੍ޚʹґଘͯ͠ େن໛ͳσʔληοτΛදݱ͢Δɻvoice-to-text΍גՁࢦ਺ͳͲ ͷଞͷαʔϏε͸ɺ࣌ؒతʹؔ࿈͢Δϝοηʔδɾγʔέϯε Λද͢ετϦʔϛϯάʹґଘ͢Δɻ

  17. ετϦʔϛϯάͷछྨ RPC ϦΫΤετ਺ Ϩεϙϯε਺ ར༻ྫ Unary 1 1 ୯७ͳrpc Server

    streaming 1 n ϑΟʔυ഑৴ɺα ʔόʔPush Client streaming n 1 ෳ਺σʔλૹ৴ Bidirectional streaming n n speech-to-textɺν ϟοτ
  18. gRPCͷઃܭݪଇͱཁ݅ • ϒϩοΩϯά&ඇϒϩοΩϯά ΫϥΠΞϯτͱαʔόʔͰަ׵͞ΕΔҰ࿈ͷϝοηʔδͷඇಉ ظ/ಉظॲཧͷ྆ํΛαϙʔτ͢Δɻ͜Ε͸ɺಛఆͷϓϥοτϑ ΥʔϜͰͷετϦʔϜͷεέʔϦϯάͱॲཧʹ͸ෆՄܽͱͳ Δɻ

  19. gRPCͷઃܭݪଇͱཁ݅ • Ωϟϯηϧ&λΠϜΞ΢τ ΦϖϨʔγϣϯ͸ίετ͕ߴ͘ɺ௕͔͔࣌ؒΔՄೳੑ͕͋Δɻ Ωϟϯηϧ͸ΫϥΠΞϯτ͕ਖ਼͘͠ৼΔ෣͑͹αʔόʔ͕Ϧι ʔεΛ࠶ར༻Ͱ͖ΔΑ͏ʹ͢Δɻ ຢɺҼՌؔ܎ͷ࿈࠯͕௥੻͞ ΕΕ͹ɺΩϟϯηϧ͸ΧεέʔυͰ͖ΔɻΫϥΠΞϯτ͸ɺα ʔϏε͕ΫϥΠΞϯτͷχʔζʹ߹Θͤͯಈ࡞Λௐ੔Ͱ͖ΔΑ ͏ɺgRPCݺͼग़͠ͷλΠϜΞ΢τΛࢦࣔ͢Δ͜ͱ͕͋Δɻ

  20. gRPCͷઃܭݪଇͱཁ݅ • ϨʔϜμοΫԽʢແޮԽʣ ৽͍͠ϦΫΤετΛڋ൱͠ɺਐߦதͷॲཧΛܧଓ͠ͳ͕Βαʔ όΛਖ਼ৗʹγϟοτμ΢ϯ͢Δඞཁ͕͋Δɻ ʢ͍ΘΏΔGraceful Shutdownʣ

  21. gRPCͷઃܭݪଇͱཁ݅ • ϑϩʔ੍ޚʢྲྀྔ੍ޚʣ ίϯϐϡʔςΟϯάͷύϫʔͱωοτϫʔΫ༰ྔ͸ɺΫϥΠΞ ϯτͱαʔόʔͷؒͰෆۉߧʹͳΓ͕ͪɺϑϩʔ੍ޚʹΑΓɺ όοϑΝ؅ཧ͕޲্͠ɺա౓ʹΞΫςΟϒͳϐΞʹΑΔDOS͔ Βͷอޢ͕ՄೳͱͳΔɻ

  22. gRPCͷઃܭݪଇͱཁ݅ • ϓϥΨϒϧ ϫΠϠϓϩτίϧ͸ɺػೳ͢ΔAPIΠϯϑϥετϥΫνϟͷҰ෦ ʹ͗͢ͳ͍ɻ େن໛ͳ෼ࢄγεςϜʹ͸ɺηΩϡϦςΟɺϔϧ ενΣοΫɺϩʔυόϥϯγϯάͱϑΣʔϧΦʔόʔɺ؂ࢹɺ τϨʔεɺϩάͳͲ͕ඞཁͱͳΔɻ ࣮૷Ͱ͸ɺ͜ΕΒͷػೳͷϓϥάΠϯΛՄೳʹ͢Δ֦ுϙΠϯ τͱɺ༗༻ͳ৔߹͸σϑΥϧτͷ࣮૷Λఏڙ͢Δඞཁ͕͋Δɻ

  23. gRPCͷઃܭݪଇͱཁ݅ • APIͱͯ͠ͷ֦ு αʔϏεؒͷ࿈ܞ͕ඞཁͳ֦ுػೳ͸ɺՄೳͰ͋Ε͹ϓϩτί ϧ֦ுͰ͸ͳ͘APIΛ࢖༻ͨ͠ํ͕๬·͍͠ ͜ͷλΠϓͷ֦ு ʹ͸ɺϔϧενΣοΫɺαʔϏεΠϯτϩεϖΫγϣϯɺෛՙ ؂ࢹɺ͓ΑͼϩʔυόϥϯγϯάͷׂΓ౰ؚ͕ͯ·ΕΔɻ

  24. gRPCͷઃܭݪଇͱཁ݅ • ϝλσʔλަ׵ ೝূ΍τϨʔεͷΑ͏ͳɺྑ͋͘Δԣஅతؔ৺ࣄ͸ɺαʔϏε ͷએݴ͞ΕͨΠϯλϑΣʔεͷҰ෦Ͱ͸ͳ͍σʔλͷަ׵ʹґ ଘ͍ͯ͠·͢ɻ σϓϩΠϝϯτ͸ɺαʔϏεʹΑͬͯެ։͞Ε ΔݸʑͷAPIͱ͸ҟͳΔ଎౓Ͱ͜ΕΒͷػೳΛσϓϩΠ͢Δೳ ྗʹґଘ͍ͯ͠Δɻ

  25. gRPCͷઃܭݪଇͱཁ݅ • ඪ४Խ͞Εͨεςʔλείʔυ ΫϥΠΞϯτ͸ɺ௨ৗɺݶΒΕͨ਺ͷํ๏ͰAPIݺͼग़͠ʹΑͬ ͯฦ͞ΕΔΤϥʔʹԠ౴͢Δɻ εςʔλείʔυͷ໊લۭؒ ͸ɺ͜ΕΒͷΤϥʔॲཧͷܾఆΛΑΓ໌֬ʹ͢ΔΑ͏ʹ੍໿͞ ΕΔ΂͖Ͱ͢ɻ ΑΓ๛෋ͳυϝΠϯݻ༗ͷεςʔλε͕ඞཁͳ ৔߹͸ɺϝλσʔλަ׵ϝΧχζϜΛ࢖༻ͯͦ͠ΕΛఏڙͰ͖

    Δɻ
  26. iOSͰ࢖͏ʹ͸ʁ

  27. SwiftͱObjective-Cͷ࣮૷͕͋Δ

  28. Objective-Cͷ࣮૷͕࢖ΘΕ͍ͯΔͷ͸ʁ ԼهGoogleͷSDKͰར༻࣮੷͕͋Δɻ • FirebaseFirestore • CloudSpeech • ͪͳΈʹFirebaseDatabaseͰ͸gRPC͸࢖ΘΕ͍ͯͳ͍

  29. gRPC-Swift

  30. gRPC-Swiftͷঢ়گ • gRPC-C-CoreΛSwiftͰϥοϓ࣮ͨ͠૷ • AppleͷSwift ProtobufΛར༻ • Ґஔ͚ͮ͸ϕʔλͰݱࡏͷόʔδϣϯ͸0.4.3

  31. gRPC-SwiftΛ࢖͏ࣄલ४උ • protocʢϓϩτίϧίϯύΠϥʣͷ४උ • brew install protobuf • SwiftϓϥάΠϯͷ४උ •

    protoc-gen-swift, protoc-gen-swiftgrpc • ಋೖهࣄ
  32. gRPC-SwiftΛϓϩδΣΫτʹಋೖ͢Δ 0.4.2͔ΒCoocoaPodsͰ؆୯ʹಋೖͰ͖ΔΑ͏ʹͳΓ·ͨ͠ɻ target 'gRPC-Client-Sample' do use_frameworks! pod 'SwiftGRPC' target 'gRPC-Client-SampleTests'

    do inherit! :search_paths end end
  33. gRPCʹΑΔ։ൃͷྲྀΕ • protoϑΝΠϧΛఆٛʢvscode-proto3͕ศརʣ • protocͰίʔυΛੜ੒ • ੜ੒͞ΕͨΫϥΠΞϯτ/αʔόʔίʔυΛ࣮૷

  34. protocͰίʔυΛੜ੒ ࣍ͷίϚϯυͰhelloworld.protoΛίϯύΠϧͯ͠ɺ ϓϥάΠϯʹ͸swift_outͱswiftgrpc_outΛ࢖͍ɺ ͦΕͧΕΧϨϯτσΟϨΫτϦʹίʔυΛग़ྗɻͱ͍͏ҙຯɻ $ protoc helloworld.proto --swift_out=. --swiftgrpc_out=.

  35. protocͰίʔυΛੜ੒ 0.4.3Ͱswiftgrpc_out΁ͷOptionࢦఆ͕௥Ճ͞Ε·ͨ͠ɻ protoc helloworld.proto \ --swift_out=GeneratedForClient \ --swiftgrpc_out=Client=true,Server=false,TestStubs=true:GeneratedForClient ͜ΕͰɺΫϥΠΞϯτίʔυͱςετ༻ͷελϒ͚ͩੜ੒͢Δ͜ ͱ͕ग़དྷ·͢ɻ

  36. ੜ੒ίʔυʢൈਮʣhelloworld.pb.swift /// The request message containing the user's name. struct

    Greeter_HelloRequest { var name: String = String() var unknownFields = SwiftProtobuf.UnknownStorage() init() {} }
  37. ੜ੒ίʔυʢൈਮʣhelloworld.grpc.swift internal protocol Greeter_GreeterSayHelloCall: ClientCallUnary {} fileprivate final class Greeter_GreeterSayHelloCallBase:

    ClientCallUnaryBase<Greeter_HelloRequest, Greeter_HelloReply>, Greeter_GreeterSayHelloCall { override class var method: String { return "/greeter.Greeter/SayHello" } } internal protocol Greeter_GreeterService: ServiceClient { /// Synchronous. Unary. func sayHello(_ request: Greeter_HelloRequest) throws -> Greeter_HelloReply /// Asynchronous. Unary. func sayHello(_ request: Greeter_HelloRequest, completion: @escaping (Greeter_HelloReply?, CallResult) -> Void) throws -> Greeter_GreeterSayHelloCall } internal final class Greeter_GreeterServiceClient: ServiceClientBase, Greeter_GreeterService { //লུ }
  38. gRPCαʔόʔ //ϓϩτίϧΛ࣮૷͠·͢ final class GreeterProvider: Greeter_GreeterProvider { func sayHello( request:

    Greeter_HelloRequest, session: Greeter_GreeterSayHelloSession) throws -> Greeter_HelloReply{ var g = Greeter_HelloReply() g.message = request.name + " world" return g } }
  39. gRPCΫϥΠΞϯτ class ViewController: UIViewController { let client = Greeter_GreeterServiceClient.init(address: "127.0.0.1:50051",

    secure: false) override func viewDidLoad() { super.viewDidLoad() var request = Greeter_HelloRequest() request.name = "hello" let ret = try? client.sayHello(request) //ಉظॲཧ print("gRPC Server returns " + ret!.message) } }
  40. gRPCΫϥΠΞϯτ // ඇಉظॲཧ var request = Greeter_HelloRequest() request.name = "hello"

    let _ = try? client.sayHello(request) { (reply, result) in if result.success, result.statusCode == .ok { print("gRPC Server returns " + reply!.message) } else { print("result \(result)") } }
  41. ͪͳΈʹଞͷετϦʔϛϯάํࣜΛ࢖͏ʹ͸ service Echo { //Unary rpc Get(EchoRequest) returns (EchoResponse) {}

    //Server streaming rpc Expand(EchoRequest) returns (stream EchoResponse) {} //Client streaming rpc Collect(stream EchoRequest) returns (EchoResponse) {} //Bidirectional streaming rpc Update(stream EchoRequest) returns (stream EchoResponse) {} }
  42. ·ͱΊ gRPCͱProtocol BuffersΛ࢖͏ͱ • ܕ෇͖ͷAPIఆ͕ٛprotoϑΝΠϧҰՕॴʹ·ͱ·Δ • ෳ਺ݴޠͷϘΠϥʔϓϨʔτΛࣗಈੜ੒Ͱ͖Δ • ෳ਺ͷετϦʔϛϯάํࣜʹॊೈʹରԠՄೳ •

    Nullable͕ѻ͍ͮΒ͍͕՝୊ͱ͍͏ఔͰ΋ͳ͍ • Contributer͕গͳ͍ͷͰߩݙΛ΍͍͖͍ͬͯͨͰ͢Ͷʂ
  43. ࢀߟ৘ใ • https://grpc.io/ • https://grpc.io/blog/principles • https://developers.google.com/protocol-buffers/docs/proto3 • https://github.com/grpc/grpc-swift •

    https://github.com/tikidunpon/gRPC-Swift-Sample