GRPCの実践と現状での利点欠点 / Go Conference 2016 Spring

GRPCの実践と現状での利点欠点 / Go Conference 2016 Spring

59c0cc69a8ad4ca8d26d752b3b795b55?s=128

kazegusuri

April 22, 2016
Tweet

Transcript

  1. GRPCͷ࣮ફͱ ݱঢ়Ͱͷར఺ɾܽ఺ @kazegusuri / Masahiro Sano Go Conference 2016 Spring

  2. ࣗݾ঺հ w ࠤ໺ਖ਼ߒ !LB[FHVTVSJ  w 43&!.FSDBSJ *OD w #BDLFOE4ZTUFNXJUI(P

    w 044ͷ׆ಈ w --7.ؔ࿈ w ,VCFSOFUFT w HSQDHBUFXBZ
  3. Mercari Your Friendly Mobile Marketplace

  4. ΞδΣϯμ w (31$ͱ͸ w (13$ͷ࢖͍ํ w ຊ൪ಋೖ·Ͱʹߦͬͨ͜ͱ w (31$ͷར఺ͱܽ఺

  5. GRPC w )551Λ࢖ͬͨ൚༻తͳ31$ϑϨʔϜϫʔΫ w ೥݄ʹ(PPHMF͕ެ։ w 1SPUPDPM#V⒎FSͰΠϯλϑΣΠεΛఆٛ

  6. GRPCͷ։ൃಈػ w 4UVCCZͱ͍͏31$Λ࢖͍ͬͯͨ w ࣾ಺γεςϜʹີ݁߹ w ಉ౳Ҏ্ͷػೳ͕ඪ४ن֨Ͱग़͖ͯͨ w ϞόΠϧ΍*P5ɺΫϥ΢υʹద༻ http://www.grpc.io/posts/principles

  7. OSSͰͷ࠾༻ࣄྫ w 5FOTPS'MPX (PPHMF  w DPOUBJOFSE %PDLFS  w

    FUDE $PSF04  w HJUIVCDPNHPLJULJU w ࠃ಺اۀͰ΋࠾༻ྫ͸ଟ͍
  8. GRPCͷ࢖͍ํ w εςοϓ  QSPUPఆٛΛॻ͘  QSPUP͔ΒHPΛੜ੒͢Δ  ΠϯλϑΣΠεΛ࣮૷͢Δ 

    (31$αʔόΛىಈ͢Δ
  9. protoఆٛΛॻ͘ w TFSWJDFʹNFUIPEΛ࣋ͨͤΔ w ೖग़ྗͷNFTTBHFΛࢦఆ syntax = “proto3"; package echo;

    message EchoMessage { string msg = 1; } service EchoService { rpc Echo(EchoMessage) returns (EchoMessage) {} }
  10. proto͔ΒgoΛੜ੒͢Δ w QSPUPDHP@PVUQMVHJOTHSQDFDIPQSPUP w NFTTBHF͸TUSVDUʹɺTFSWJDF͸JOUFSGBDFʹ type EchoMessage struct { Msg

    string `protobuf:"bytes,1,opt,name=msg" json:"msg,omitempty"` } type EchoServiceServer interface { Echo(context.Context, *EchoMessage) (*EchoMessage, error) }
  11. ΠϯλϑΣΠεΛ࣮૷͢Δ w &DIPؔ਺Λ࣋ͭTUSVDUΛ࣮૷ w Τϥʔ͸ઐ༻ͷεςʔλεΛฦ͢ type EchoServer struct{} func (s

    *EchoServer) Echo(ctx context.Context, in *pb.EchoMessage) (*pb.EchoMessage, error) { if in.Msg == "" { return nil, grpc.Errorf(codes.InvalidArgument, "message is empty") } return &pb.EchoMessage{ Msg: in.Msg, }, nil }
  12. GRPCαʔόΛىಈ͢Δ w HSQD4FSWFSʹTFSWJDFΛొ࿥ w OFU-JTUFOFSΛ౉ͯ͠4FSWF func main() { echoServer :=

    &server.EchoServer{} s := grpc.NewServer() pb.RegisterEchoServiceServer(s, echoServer) lis, _ := net.Listen("tcp", ":8080") s.Serve(lis) }
  13. ຊ൪ಋೖ·Ͱʹߦͬͨ͜ͱ • ॲཧͷڞ௨Խ • Τϥʔॲཧ • ੑೳݕূ • ϦΫΤετ෼ࢄ •

    grpc-gateway
  14. ϦΫΤετॲཧͷϨΠϠԽ w શͯͷNFUIPEͰڞ௨ͷॲཧΛߦ͍͍ͨ w MPHHJOHೝূͳͲ w TFSWJDFͷΠϯλϑΣΠεΛ࣋ͭϋϯυϥΛ࡞੒ w IUUQͰͷ4FSWF)551Λఏڙ͢Δϋϯυϥ

  15. ϦΫΤετϋϯυϥ w -PHHJOH)BOEMFS w *OTUSVNFOUBUJPO)BOEMFS w $SBTI)BOEMFS w "VUIFOUJDBUJPO)BOEMFS w

    4FSWJDF)BOEMFS
  16. Logging: ΞΫηεϩά w ֤छ஋Λϩάʹอଘ w TFSWJDF໊΍NFUIPE໊ w ϦΫΤετ࣌ؒ w εςʔλείʔυͱΤϥʔϝοηʔδ

    w ϦϞʔτΞυϨε w MPHHFS͸ࣗલͰ࣮૷
  17. Logging: ΞϓϦέʔγϣϯϩά w ΞϓϦέʔγϣϯϩάʹίϯςΩετΛ௥Ճ w TFSWJDFNFUIPE໊ɺ6TFS*%ͳͲ w HJUIVCDPNHPLJULJUMPH w HPMBOHPSHYOFUDPOUFYUʹMPHHFSΛηοτ

  18. Instrumentation Handler w QSPNFUIFVT w JOTUSVNFOUBUJPOϥΠϒϥϦͱͯ͠ར༻ w ϦΫΤετճ਺ɾ࣌ؒΤϥʔճ਺ w औಘͨ͠ϝτϦΫε͸)551Ͱఏڙ

  19. Crash Handler w ϦΫΤετॲཧதͷQBOJDΛSFDPWFS w *OUFSOBMΤϥʔͱͯ͠ฦ͢ w ΞϓϦέʔγϣϯϩάʹελοΫτϨʔε w *OUFSOBMΤϥʔ͸ϩά؂ࢹͰϞχλϦϯά

  20. Authentication Handler w NFUBEBUBΛ࢖ͬͨೝূ w Ϣʔβ৘ใΛDPOUFYUʹηοτ w DSFEFOUJBMTͷػೳ͸࢖͍ͬͯͳ͍

  21. Τϥʔͷฦ͠ํ • Τϥʔ࣌͸message͕ฦͤͳ͍ • εςʔλείʔυͱΤϥʔϝοηʔδͷΈ return nil, grpc.Errorf(codes.InvalidArgument, "message is

    empty") εςʔλείʔυ Τϥʔϝοηʔδ ແࢹ͞ΕΔ
  22. Metadata w )FBEFSͱ5SBJMFS w ϦΫΤετ࣌͸)FBEFSͷΈ w Ϩεϙϯε࣌͸)FBEFSͱ5SBJMFS͕ར༻Մೳ w ৄࡉͳΤϥʔ৘ใ͸5SBJMFSΛར༻ md

    := metadata.Pairs( “error”, "ValidationError", “message", "message is empty") grpc.SetTrailer(md)
  23. ύϑΥʔϚϯε w FDIPαʔόͰ,SFRT w MPHHJOHJOTUSVNFOUBUJPOؚΉ w ೝূ ॺ໊ݕূ Λ͍Εͯ΋,SFRT w

    NT͔͔Βͳ͍ w %#ΞΫηεΛؚΉ࣮ࡍͷॲཧͰ΋,SFRT
  24. ϩʔυόϥϯε w ෳ਺αʔόʹϦΫΤετΛ෼ࢄ w )551ͷ··(31$αʔόʹ௨৴ w ϦΫΤετݩͷ*1ΞυϨεΛ౉͍ͨ͠

  25. nginx TCP Load Balancing w OHJOY͔ΒTUSFBNNPEVMFΛαϙʔτ w VQTUSFBNʹ*1ΞυϨεΛ౉͢ͷ͕՝୊ w 9'PXBSEFE'PS

    w 51SPYZ 5SBOTQBSFOU1SPYZ  w 1309:QSPUPDPM
  26. PROXY protocol w 5$1ϨϕϧͰ*1ΞυϨεͱϙʔτΛνΣΠϯ w ΫϥΠΞϯτଆͷରԠ͕ඞཁ w HPͰ͸HJUIVCDPNBSNPOHPQSPYZQSPUP w OFU-JTUFOFSͷϥούʔͳͷͰ؆୯

    list, err := net.Listen("tcp", "...") proxyList := &proxyproto.Listener{list} conn, err := proxyList.Accept()
  27. grpc-gateway w +40/͔Β(31$΁ม׵͢ΔϦόʔεϓϩΩγ w QSPUP͔Βࣗಈੜ੒ w 3&45"1*Λఏڙ͍ͨ͠৔߹ʹศར w ಛʹσόοάʹศར

  28. grpc-gatewayͷఆٛํ๏ message SimpleMessage { string id = 1; } service

    EchoService { rpc Echo(SimpleMessage) returns (SimpleMessage) { option (google.api.http) = { post: "/v1/example/echo/{id}" }; } rpc EchoBody(SimpleMessage) returns (SimpleMessage) { option (google.api.http) = { post: "/v1/example/echo_body" body: "*" }; } }
  29. grpc-gateway w Կ΋ߟ͑ͳ͍ઃఆ͕͓͢͢Ί w 63-͸TFSWJDF໊NFUIPE໊ w NFTTBHF͸શ෦1045Ͱ౉͢ w ਏ͍ͱ͜Ζ΋͋Δ w

    ϔομʔͱNFUBEBUBͷѻ͍ w QSPUPͰఆٛ͞ΕΔKTPO͕શͯPNJUFNQUZ
  30. GRPCͷܽ఺ w ϛυϧ΢ΣΞ͕ͳ͍ɺͦ΋ͦ΋࡞ΓͮΒ͍ w ΞΫηεϩάͳͲ w DVSM͕࢖͑ͳ͍ w υΩϡϝϯτ͕গͳ͍ w

    ιʔεΛಡΉͱ࣮͸͜Μͳػೳ͕ʜ w ݴޠຖͷ׬੒౓΍ػೳ͕ҧ͏
  31. GRPCͷར఺ w ࣮૷ʹूதͰ͖Δ w ϧʔςΟϯάɺFODEFD͕ࣗಈ w ΫϥΠΞϯτ͕࡞Γ΍͍͢ w ςετ΍ଟݴޠͷରԠ w

    ܽ఺ͷ෦෼΋ͳΜͱ͔ͳΔ
  32. GRPC͸݁ہ͓͢͢Ίͳͷ͔ʁ w ͔ͳΓ͓͢͢Ί w JOUFSDFQUPS͕ೖΕ͹΄΅՝୊͸ͳ͘ͳΔ w ϚΠΫϩαʔϏεͷ͓ڙʹ w ΋͏ී௨ͷ3&45"1*ʹ͸໭Γͨ͘ͳ͍ w

    +40/ʹରԠ͢ΔܕΛ౎౓ॻ͘ख͕ؒݮΔ w (31$͸͍͍ͧ
  33. GoΤϯδχΞืूʂ Backend System Engineer https://www.mercari.com/jp/jobs/backend/