Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

ࣗݾ঺հ w ࠤ໺ਖ਼ߒ !LB[FHVTVSJ w 43&!.FSDBSJ *OD w #BDLFOE4ZTUFNXJUI(P w 044ͷ׆ಈ w --7.ؔ࿈ w ,VCFSOFUFT w HSQDHBUFXBZ

Slide 3

Slide 3 text

Mercari Your Friendly Mobile Marketplace

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

OSSͰͷ࠾༻ࣄྫ w 5FOTPS'MPX (PPHMF w DPOUBJOFSE %PDLFS w FUDE $PSF04 w HJUIVCDPNHPLJULJU w ࠃ಺اۀͰ΋࠾༻ྫ͸ଟ͍

Slide 8

Slide 8 text

GRPCͷ࢖͍ํ w εςοϓ QSPUPఆٛΛॻ͘ QSPUP͔ΒHPΛੜ੒͢Δ ΠϯλϑΣΠεΛ࣮૷͢Δ (31$αʔόΛىಈ͢Δ

Slide 9

Slide 9 text

protoఆٛΛॻ͘ w TFSWJDFʹNFUIPEΛ࣋ͨͤΔ w ೖग़ྗͷNFTTBHFΛࢦఆ syntax = “proto3"; package echo; message EchoMessage { string msg = 1; } service EchoService { rpc Echo(EchoMessage) returns (EchoMessage) {} }

Slide 10

Slide 10 text

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) }

Slide 11

Slide 11 text

ΠϯλϑΣΠεΛ࣮૷͢Δ 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 }

Slide 12

Slide 12 text

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) }

Slide 13

Slide 13 text

ຊ൪ಋೖ·Ͱʹߦͬͨ͜ͱ • ॲཧͷڞ௨Խ • Τϥʔॲཧ • ੑೳݕূ • ϦΫΤετ෼ࢄ • grpc-gateway

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Logging: ΞΫηεϩά w ֤छ஋Λϩάʹอଘ w TFSWJDF໊΍NFUIPE໊ w ϦΫΤετ࣌ؒ w εςʔλείʔυͱΤϥʔϝοηʔδ w ϦϞʔτΞυϨε w MPHHFS͸ࣗલͰ࣮૷

Slide 17

Slide 17 text

Logging: ΞϓϦέʔγϣϯϩά w ΞϓϦέʔγϣϯϩάʹίϯςΩετΛ௥Ճ w TFSWJDFNFUIPE໊ɺ6TFS*%ͳͲ w HJUIVCDPNHPLJULJUMPH w HPMBOHPSHYOFUDPOUFYUʹMPHHFSΛηοτ

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Metadata w )FBEFSͱ5SBJMFS w ϦΫΤετ࣌͸)FBEFSͷΈ w Ϩεϙϯε࣌͸)FBEFSͱ5SBJMFS͕ར༻Մೳ w ৄࡉͳΤϥʔ৘ใ͸5SBJMFSΛར༻ md := metadata.Pairs( “error”, "ValidationError", “message", "message is empty") grpc.SetTrailer(md)

Slide 23

Slide 23 text

ύϑΥʔϚϯε w FDIPαʔόͰ,SFRT w MPHHJOHJOTUSVNFOUBUJPOؚΉ w ೝূ ॺ໊ݕূ Λ͍Εͯ΋,SFRT w NT͔͔Βͳ͍ w %#ΞΫηεΛؚΉ࣮ࡍͷॲཧͰ΋,SFRT

Slide 24

Slide 24 text

ϩʔυόϥϯε w ෳ਺αʔόʹϦΫΤετΛ෼ࢄ w )551ͷ··(31$αʔόʹ௨৴ w ϦΫΤετݩͷ*1ΞυϨεΛ౉͍ͨ͠

Slide 25

Slide 25 text

nginx TCP Load Balancing w OHJOY͔ΒTUSFBNNPEVMFΛαϙʔτ w VQTUSFBNʹ*1ΞυϨεΛ౉͢ͷ͕՝୊ w 9'PXBSEFE'PS w 51SPYZ 5SBOTQBSFOU1SPYZ w 1309:QSPUPDPM

Slide 26

Slide 26 text

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()

Slide 27

Slide 27 text

grpc-gateway w +40/͔Β(31$΁ม׵͢ΔϦόʔεϓϩΩγ w QSPUP͔Βࣗಈੜ੒ w 3&45"1*Λఏڙ͍ͨ͠৔߹ʹศར w ಛʹσόοάʹศར

Slide 28

Slide 28 text

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: "*" }; } }

Slide 29

Slide 29 text

grpc-gateway w Կ΋ߟ͑ͳ͍ઃఆ͕͓͢͢Ί w 63-͸TFSWJDF໊NFUIPE໊ w NFTTBHF͸શ෦1045Ͱ౉͢ w ਏ͍ͱ͜Ζ΋͋Δ w ϔομʔͱNFUBEBUBͷѻ͍ w QSPUPͰఆٛ͞ΕΔKTPO͕શͯPNJUFNQUZ

Slide 30

Slide 30 text

GRPCͷܽ఺ w ϛυϧ΢ΣΞ͕ͳ͍ɺͦ΋ͦ΋࡞ΓͮΒ͍ w ΞΫηεϩάͳͲ w DVSM͕࢖͑ͳ͍ w υΩϡϝϯτ͕গͳ͍ w ιʔεΛಡΉͱ࣮͸͜Μͳػೳ͕ʜ w ݴޠຖͷ׬੒౓΍ػೳ͕ҧ͏

Slide 31

Slide 31 text

GRPCͷར఺ w ࣮૷ʹूதͰ͖Δ w ϧʔςΟϯάɺFODEFD͕ࣗಈ w ΫϥΠΞϯτ͕࡞Γ΍͍͢ w ςετ΍ଟݴޠͷରԠ w ܽ఺ͷ෦෼΋ͳΜͱ͔ͳΔ

Slide 32

Slide 32 text

GRPC͸݁ہ͓͢͢Ίͳͷ͔ʁ w ͔ͳΓ͓͢͢Ί w JOUFSDFQUPS͕ೖΕ͹΄΅՝୊͸ͳ͘ͳΔ w ϚΠΫϩαʔϏεͷ͓ڙʹ w ΋͏ී௨ͷ3&45"1*ʹ͸໭Γͨ͘ͳ͍ w +40/ʹରԠ͢ΔܕΛ౎౓ॻ͘ख͕ؒݮΔ w (31$͸͍͍ͧ

Slide 33

Slide 33 text

GoΤϯδχΞืूʂ Backend System Engineer https://www.mercari.com/jp/jobs/backend/