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

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

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

kazegusuri

April 22, 2016
Tweet

More Decks by kazegusuri

Other Decks in Technology

Transcript

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

    w 044ͷ׆ಈ w --7.ؔ࿈ w ,VCFSOFUFT w HSQDHBUFXBZ
  2. OSSͰͷ࠾༻ࣄྫ w 5FOTPS'MPX (PPHMF  w DPOUBJOFSE %PDLFS  w

    FUDE $PSF04  w HJUIVCDPNHPLJULJU w ࠃ಺اۀͰ΋࠾༻ྫ͸ଟ͍
  3. protoఆٛΛॻ͘ w TFSWJDFʹNFUIPEΛ࣋ͨͤΔ w ೖग़ྗͷNFTTBHFΛࢦఆ syntax = “proto3"; package echo;

    message EchoMessage { string msg = 1; } service EchoService { rpc Echo(EchoMessage) returns (EchoMessage) {} }
  4. 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) }
  5. ΠϯλϑΣΠεΛ࣮૷͢Δ 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 }
  6. 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) }
  7. Metadata w )FBEFSͱ5SBJMFS w ϦΫΤετ࣌͸)FBEFSͷΈ w Ϩεϙϯε࣌͸)FBEFSͱ5SBJMFS͕ར༻Մೳ w ৄࡉͳΤϥʔ৘ใ͸5SBJMFSΛར༻ md

    := metadata.Pairs( “error”, "ValidationError", “message", "message is empty") grpc.SetTrailer(md)
  8. 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()
  9. 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: "*" }; } }