Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
GRPCの実践と現状での利点欠点 / Go Conference 2016 Spring
Search
kazegusuri
April 22, 2016
Technology
44
32k
GRPCの実践と現状での利点欠点 / Go Conference 2016 Spring
kazegusuri
April 22, 2016
Tweet
Share
More Decks by kazegusuri
See All by kazegusuri
go-sqlite3を使ってCloud Spannerエミュレーターを作ってみた / Cloud Spanner emulator with go-sqlite3
kazegusuri
5
6.4k
handy-spanner GCPUG
kazegusuri
4
1.8k
Open SKT: メルペイ開発の裏側 / builderscon tokyo 2019 Open SKT
kazegusuri
22
26k
Keep watching and extending features of gRPC
kazegusuri
3
2.4k
Testing with microservices in merpay
kazegusuri
10
10k
Real World Mercari API Architecture
kazegusuri
1
6.1k
gRPC and REST with gRPC in practice
kazegusuri
19
7.7k
Fluentdで始めるPrometheus / Prometheus Tokyo Meetup #1
kazegusuri
1
1.7k
OutputとBufferedOutputの間の何か
kazegusuri
2
3.2k
Other Decks in Technology
See All in Technology
Jetpack Composeで始めるServer Cache State
ogaclejapan
2
160
TSKaigi 2024 の登壇から広がったコミュニティ活動について
tsukuha
0
160
10分で学ぶKubernetesコンテナセキュリティ/10min-k8s-container-sec
mochizuki875
3
330
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
2
2.2k
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
24
11k
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
150
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
How to be an AWS Community Builder | 君もAWS Community Builderになろう!〜2024 冬 CB募集直前対策編?!〜
coosuke
PRO
2
2.8k
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
私なりのAIのご紹介 [2024年版]
qt_luigi
1
120
2024年にチャレンジしたことを振り返るぞ
mitchan
0
130
5分でわかるDuckDB
chanyou0311
10
3.2k
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
88
5.7k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
How GitHub (no longer) Works
holman
311
140k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
The Language of Interfaces
destraynor
154
24k
Writing Fast Ruby
sferik
628
61k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Thoughts on Productivity
jonyablonski
67
4.4k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
The Cost Of JavaScript in 2023
addyosmani
45
7k
Transcript
GRPCͷ࣮ફͱ ݱঢ়Ͱͷརɾܽ @kazegusuri / Masahiro Sano Go Conference 2016 Spring
ࣗݾհ w ࠤਖ਼ߒ !LB[FHVTVSJ w 43&!.FSDBSJ *OD w #BDLFOE4ZTUFNXJUI(P
w 044ͷ׆ಈ w --7.ؔ࿈ w ,VCFSOFUFT w HSQDHBUFXBZ
Mercari Your Friendly Mobile Marketplace
ΞδΣϯμ w (31$ͱ w (13$ͷ͍ํ w ຊ൪ಋೖ·Ͱʹߦͬͨ͜ͱ w (31$ͷརͱܽ
GRPC w )551Λͬͨ൚༻తͳ31$ϑϨʔϜϫʔΫ w ݄ʹ(PPHMF͕ެ։ w 1SPUPDPM#V⒎FSͰΠϯλϑΣΠεΛఆٛ
GRPCͷ։ൃಈػ w 4UVCCZͱ͍͏31$Λ͍ͬͯͨ w ࣾγεςϜʹີ݁߹ w ಉҎ্ͷػೳ͕ඪ४ن֨Ͱग़͖ͯͨ w ϞόΠϧ*P5ɺΫϥυʹద༻ http://www.grpc.io/posts/principles
OSSͰͷ࠾༻ࣄྫ w 5FOTPS'MPX (PPHMF w DPOUBJOFSE %PDLFS w
FUDE $PSF04 w HJUIVCDPNHPLJULJU w ࠃاۀͰ࠾༻ྫଟ͍
GRPCͷ͍ํ w εςοϓ QSPUPఆٛΛॻ͘ QSPUP͔ΒHPΛੜ͢Δ ΠϯλϑΣΠεΛ࣮͢Δ
(31$αʔόΛىಈ͢Δ
protoఆٛΛॻ͘ w TFSWJDFʹNFUIPEΛ࣋ͨͤΔ w ೖग़ྗͷNFTTBHFΛࢦఆ syntax = “proto3"; package echo;
message EchoMessage { string msg = 1; } service EchoService { rpc Echo(EchoMessage) returns (EchoMessage) {} }
proto͔ΒgoΛੜ͢Δ w QSPUPDHP@PVUQMVHJOTHSQDFDIPQSPUP w NFTTBHFTUSVDUʹɺTFSWJDFJOUFSGBDFʹ type EchoMessage struct { Msg
string `protobuf:"bytes,1,opt,name=msg" json:"msg,omitempty"` } type EchoServiceServer interface { Echo(context.Context, *EchoMessage) (*EchoMessage, error) }
ΠϯλϑΣΠεΛ࣮͢Δ 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 }
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) }
ຊ൪ಋೖ·Ͱʹߦͬͨ͜ͱ • ॲཧͷڞ௨Խ • Τϥʔॲཧ • ੑೳݕূ • ϦΫΤετࢄ •
grpc-gateway
ϦΫΤετॲཧͷϨΠϠԽ w શͯͷNFUIPEͰڞ௨ͷॲཧΛߦ͍͍ͨ w MPHHJOHೝূͳͲ w TFSWJDFͷΠϯλϑΣΠεΛ࣋ͭϋϯυϥΛ࡞ w IUUQͰͷ4FSWF)551Λఏڙ͢Δϋϯυϥ
ϦΫΤετϋϯυϥ w -PHHJOH)BOEMFS w *OTUSVNFOUBUJPO)BOEMFS w $SBTI)BOEMFS w "VUIFOUJDBUJPO)BOEMFS w
4FSWJDF)BOEMFS
Logging: ΞΫηεϩά w ֤छΛϩάʹอଘ w TFSWJDF໊NFUIPE໊ w ϦΫΤετ࣌ؒ w εςʔλείʔυͱΤϥʔϝοηʔδ
w ϦϞʔτΞυϨε w MPHHFSࣗલͰ࣮
Logging: ΞϓϦέʔγϣϯϩά w ΞϓϦέʔγϣϯϩάʹίϯςΩετΛՃ w TFSWJDFNFUIPE໊ɺ6TFS*%ͳͲ w HJUIVCDPNHPLJULJUMPH w HPMBOHPSHYOFUDPOUFYUʹMPHHFSΛηοτ
Instrumentation Handler w QSPNFUIFVT w JOTUSVNFOUBUJPOϥΠϒϥϦͱͯ͠ར༻ w ϦΫΤετճɾ࣌ؒΤϥʔճ w औಘͨ͠ϝτϦΫε)551Ͱఏڙ
Crash Handler w ϦΫΤετॲཧதͷQBOJDΛSFDPWFS w *OUFSOBMΤϥʔͱͯ͠ฦ͢ w ΞϓϦέʔγϣϯϩάʹελοΫτϨʔε w *OUFSOBMΤϥʔϩάࢹͰϞχλϦϯά
Authentication Handler w NFUBEBUBΛͬͨೝূ w ϢʔβใΛDPOUFYUʹηοτ w DSFEFOUJBMTͷػೳ͍ͬͯͳ͍
Τϥʔͷฦ͠ํ • Τϥʔ࣌message͕ฦͤͳ͍ • εςʔλείʔυͱΤϥʔϝοηʔδͷΈ return nil, grpc.Errorf(codes.InvalidArgument, "message is
empty") εςʔλείʔυ Τϥʔϝοηʔδ ແࢹ͞ΕΔ
Metadata w )FBEFSͱ5SBJMFS w ϦΫΤετ࣌)FBEFSͷΈ w Ϩεϙϯε࣌)FBEFSͱ5SBJMFS͕ར༻Մೳ w ৄࡉͳΤϥʔใ5SBJMFSΛར༻ md
:= metadata.Pairs( “error”, "ValidationError", “message", "message is empty") grpc.SetTrailer(md)
ύϑΥʔϚϯε w FDIPαʔόͰ,SFRT w MPHHJOHJOTUSVNFOUBUJPOؚΉ w ೝূ ॺ໊ݕূ Λ͍Εͯ,SFRT w
NT͔͔Βͳ͍ w %#ΞΫηεΛؚΉ࣮ࡍͷॲཧͰ,SFRT
ϩʔυόϥϯε w ෳαʔόʹϦΫΤετΛࢄ w )551ͷ··(31$αʔόʹ௨৴ w ϦΫΤετݩͷ*1ΞυϨεΛ͍ͨ͠
nginx TCP Load Balancing w OHJOY͔ΒTUSFBNNPEVMFΛαϙʔτ w VQTUSFBNʹ*1ΞυϨεΛ͢ͷ͕՝ w 9'PXBSEFE'PS
w 51SPYZ 5SBOTQBSFOU1SPYZ w 1309:QSPUPDPM
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()
grpc-gateway w +40/͔Β(31$ม͢ΔϦόʔεϓϩΩγ w QSPUP͔Βࣗಈੜ w 3&45"1*Λఏڙ͍ͨ͠߹ʹศར w ಛʹσόοάʹศར
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: "*" }; } }
grpc-gateway w Կߟ͑ͳ͍ઃఆ͕͓͢͢Ί w 63-TFSWJDF໊NFUIPE໊ w NFTTBHFશ෦1045Ͱ͢ w ਏ͍ͱ͜Ζ͋Δ w
ϔομʔͱNFUBEBUBͷѻ͍ w QSPUPͰఆٛ͞ΕΔKTPO͕શͯPNJUFNQUZ
GRPCͷܽ w ϛυϧΣΞ͕ͳ͍ɺͦͦ࡞ΓͮΒ͍ w ΞΫηεϩάͳͲ w DVSM͕͑ͳ͍ w υΩϡϝϯτ͕গͳ͍ w
ιʔεΛಡΉͱ࣮͜Μͳػೳ͕ʜ w ݴޠຖͷػೳ͕ҧ͏
GRPCͷར w ࣮ʹूதͰ͖Δ w ϧʔςΟϯάɺFODEFD͕ࣗಈ w ΫϥΠΞϯτ͕࡞Γ͍͢ w ςετଟݴޠͷରԠ w
ܽͷ෦ͳΜͱ͔ͳΔ
GRPC݁ہ͓͢͢Ίͳͷ͔ʁ w ͔ͳΓ͓͢͢Ί w JOUFSDFQUPS͕ೖΕ΄΅՝ͳ͘ͳΔ w ϚΠΫϩαʔϏεͷ͓ڙʹ w ͏ී௨ͷ3&45"1*ʹΓͨ͘ͳ͍ w
+40/ʹରԠ͢ΔܕΛॻ͘ख͕ؒݮΔ w (31$͍͍ͧ
GoΤϯδχΞืूʂ Backend System Engineer https://www.mercari.com/jp/jobs/backend/