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

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

59c0cc69a8ad4ca8d26d752b3b795b55?s=128

kazegusuri

April 22, 2016
Tweet

Transcript

  1. 2.

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

    w 044ͷ׆ಈ w --7.ؔ࿈ w ,VCFSOFUFT w HSQDHBUFXBZ
  2. 7.

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

    FUDE $PSF04  w HJUIVCDPNHPLJULJU w ࠃ಺اۀͰ΋࠾༻ྫ͸ଟ͍
  3. 9.

    protoఆٛΛॻ͘ w TFSWJDFʹNFUIPEΛ࣋ͨͤΔ w ೖग़ྗͷNFTTBHFΛࢦఆ syntax = “proto3"; package echo;

    message EchoMessage { string msg = 1; } service EchoService { rpc Echo(EchoMessage) returns (EchoMessage) {} }
  4. 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) }
  5. 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 }
  6. 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) }
  7. 22.

    Metadata w )FBEFSͱ5SBJMFS w ϦΫΤετ࣌͸)FBEFSͷΈ w Ϩεϙϯε࣌͸)FBEFSͱ5SBJMFS͕ར༻Մೳ w ৄࡉͳΤϥʔ৘ใ͸5SBJMFSΛར༻ md

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