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
Search
Daiki Nojiri
May 23, 2020
Programming
0
260
イメージで理解するgRPC
Daiki Nojiri
May 23, 2020
Tweet
Share
More Decks by Daiki Nojiri
See All by Daiki Nojiri
今日から書けるリーダブルコード
nojiri1098
0
340
GitのCLI操作を楽にするためにエイリアスを作った話
nojiri1098
0
230
Other Decks in Programming
See All in Programming
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
270
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
120
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
170
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
340
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.9k
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
6
1.2k
KubeCon NA 2024の全DB関連セッションを紹介
nnaka2992
0
100
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
180
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
140
Оптимизируем производительность блока Казначейство
lamodatech
0
790
情報漏洩させないための設計
kubotak
4
1.1k
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
840
Featured
See All Featured
Building Adaptive Systems
keathley
38
2.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
171
50k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
18
2.3k
YesSQL, Process and Tooling at Scale
rocio
170
14k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Building an army of robots
kneath
302
44k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
470
A Philosophy of Restraint
colly
203
16k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Transcript
ΠϝʔδͰཧղ͢Δ
ࠓͷΰʔϧ ɾH31$ͬͯͳʹʁ ɾ31$ͬͯͳʹʁ ɾͲ͏࣮ͬͯ͢Δͷʁ
ͬͯͳʹʁ
H31$ (PPHMF͕࡞ͬͨ31$ϑϨʔϜϫʔΫ
31$ͬͯͳʹʁ
31$ 3FNPUF1SPDFEVSF$BMM
31$ DMJFOU͕ࣗͷϝιουΛ࣮ߦ͢Δײ֮Ͱ TFSWFS্ͷϝιουΛ࣮ߦͰ͖Δ͘͠Έ
ͭ·ΓɺͲ͏͍͏͜ͱʁ
DMJFOUͱTFSWFSʹಉ໊ϝιου͕͋ͬͯ IUUQTJDPOTDPNJDPOTTFUTFSWFS DMJFOU TFSWFS Find() Find()
DMJFOU͕ϝιουΛ࣮ߦ͢Δͱ DMJFOU TFSWFS Find() Find() IUUQTJDPOTDPNJDPOTTFUTFSWFS
TFSWFSଆͷಉ໊ϝιου͕࣮ߦ͞Ε DMJFOU TFSWFS Find() Find() SFRVFTU IUUQTJDPOTDPNJDPOTTFUTFSWFS
ͦͷฦΓ͕DMJFOUଆͷฦΓʹͳΔ DMJFOU TFSWFS Find() Find() SFRVFTU SFTQPOTF IUUQTJDPOTDPNJDPOTTFUTFSWFS
H31$͜ͷؒΛ͏·ͬͯ͘͘ΕΔ DMJFOU TFSWFS Find() Find() SFRVFTU SFTQPOTF IUUQTJDPOTDPNJDPOTTFUTFSWFS
const res = axios.get("/api/v1/users/1") js req := FindRequest({ID: 1}) res,
err = user.Find(req) go 3&45 31$ ͜Μͳ͍ํ͕Ͱ͖·͢
Ͳ͏࣮ͬͯ͢Δͷʁ
31$Λ࣮ݱ͢Δʹ SFRVFTU DMJFOU TFSWFS SFTQPOTF IUUQTJDPOTDPNJDPOTTFUTFSWFS
ڞ௨ೝ͕ࣝඞཁ SFRVFTU DMJFOU TFSWFS SFTQPOTF ϝιουͷ໊લ ϦΫΤετͷܗࣜ Ϩεϙϯεͷܗࣜ IUUQTJDPOTDPNJDPOTTFUTFSWFS
ͦΕ͕ɺQSPUPϑΝΠϧ SFRVFTU DMJFOU TFSWFS SFTQPOTF QSPUP IUUQTJDPOTDPNJDPOTTFUTFSWFS
QSPUPϑΝΠϧͷྫ service User { rpc Find(FindRequest) returns (FindResponse) {} }
message FindRequest { int32 id = 1; } message FindResponse { string name = 1; } user_service.proto
QSPUPϑΝΠϧͷྫ user_service.proto ϦΫΤετͷܗࣜ Ϩεϙϯεͷܗࣜ service User { rpc Find(FindRequest) returns
(FindResponse) {} } message FindRequest { int32 id = 1; } message FindResponse { string name = 1; }
service User { rpc Find(FindRequest) returns (FindResponse) {} } message
FindRequest { int32 id = 1; } message FindResponse { string name = 1; } QSPUPϑΝΠϧͷྫ user_service.proto ˢϝιουͷఆٛ
QSPUP QCHP ࣍ʹɺΠϯλʔϑΣʔεΛੜ͢Δ QSPUPD QSPUPDΠϯλʔϑΣʔεΛੜ͢ΔͨΊͷίϚϯυ
QSPUP QCHP ࣍ʹɺΠϯλʔϑΣʔεΛੜ͢Δ QSPUPD ͍ΖΜͳJOUFSGBDFGVOD͕ࣗಈੜ ͞ΕΔɽਓҰ͍͡Βͳ͍
ॏཁͳ෦͚ͩϐοΫΞοϓ type UserServer interface { Find( context.Context, *FindRequest ) (*FindResponse,
error) } user_service.pb.go αʔόଆͷΠϯλʔϑΣʔε αʔόଆ͜Εʹ߹ΘͤͯϝιουΛ࣮͢Δ
ॏཁͳ෦͚ͩϐοΫΞοϓ type UserClient interface { Find( ctx context.Context, in *FindRequest,
opts ...grpc.CallOption ) (*FindResponse, error) } ΫϥΠΞϯτଆͷΠϯλʔϑΣʔε ΫϥΠΞϯτଆ͜Εʹ߹ΘͤͯϝιουΛ͏ user_service.pb.go
ॏཁͳ෦͚ͩϐοΫΞοϓ func (c *userClient) Find( ctx context.Context, in *FindRequest, opts
...grpc.CallOption ) (*FindResponse, error) { out := new(FindResponse) err := c.cc.Invoke( ctx, "/user_service.User/Find", in, out, opts... ) if err != nil { return nil, err } return out, nil } ΫϥΠΞϯτଆͷϝιου ΫϥΠΞϯτଆ͕ݺͼग़͢ϝιου࣮ࡁΈ user_service.pb.go
QSPUP QCHP ͪͳΈʹɺෳݴޠʹରԠ͍ͯ͠Δ SC KBWB
αʔόଆͷ͍ํ ؆ུ൛ import pb "path/to/pb.go" type server struct{} func (s
*server) Find( ctx context.Context, in *pb.FindRequest ) (*pb.FindResponse, error) { user := db.Find(&user) out := &pb.FindResponse{name: user.name} return out, nil } s := grpc.NewServer() pb.RegisterUserServer(s, &server{}) s.Serve(lis) server/main.go
import pb "path/to/pb.go" type server struct{} func (s *server) Find(
ctx context.Context, in *pb.FindRequest ) (*pb.FindResponse, error) { user := db.Find(&user) out := &pb.FindResponse{name: user.name} return out, nil } s := grpc.NewServer() pb.RegisterUserServer(s, &server{}) s.Serve(lis) αʔόଆͷ͍ํ ؆ུ൛ server/main.go %#͔ΒΛ औಘͯ͠ฦ͢ αʔόͷ্ཱͪ͛
ΫϥΠΞϯτଆͷ͍ํ ؆ུ൛ import pb "path/to/pb.go" c := pb.NewUserClient(conn) req :=
&pb.FindRequest{ id: 1, } resp, err := c.Find(context.Background(), req) if err != nil { log.Fatal(err) } log.Println("Name: ", resp.name) // #=> Name: alice client/main.go
ΫϥΠΞϯτଆͷ͍ํ ؆ུ൛ import pb "path/to/pb.go" c := pb.NewUserClient(conn) req :=
&pb.FindRequest{ Name: "alice", } resp, err := c.Find(context.Background(), req) if err != nil { log.Fatal(err) } log.Println("Name: ", resp.name) // #=> Name: alice client/main.go αʔόଆͷFind()ͷฦΓ
ࠓͷ·ͱΊ H31$ͬͯͳʹʁ ˠ(PPHMF͕࡞ͬͨ31$ϑϨʔϜϫʔΫ 31$ͬͯͳʹʁ ˠΫϥΠΞϯτ͔ΒαʔόͷϝιουΛݺͿ H31$Λ͏ʹʁ ˠQSPUPϑΝΠϧΛॻ͍ͯΠϯλʔϑΣʔεΛ ɹੜ͢Δ ˠੜ͞ΕͨΠϯλʔϑΣʔεΛ࣮ͬͯ͢Δ