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
250
イメージで理解するgRPC
Daiki Nojiri
May 23, 2020
Tweet
Share
More Decks by Daiki Nojiri
See All by Daiki Nojiri
今日から書けるリーダブルコード
nojiri1098
0
330
GitのCLI操作を楽にするためにエイリアスを作った話
nojiri1098
0
230
Other Decks in Programming
See All in Programming
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
320
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
みんなでプロポーザルを書いてみた
yuriko1211
0
260
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Remix on Hono on Cloudflare Workers
yusukebe
1
280
Click-free releases & the making of a CLI app
oheyadam
2
110
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
Ethereum_.pdf
nekomatu
0
460
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
BBQ
matthewcrist
85
9.3k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
How to Ace a Technical Interview
jacobian
276
23k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Writing Fast Ruby
sferik
627
61k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Visualization
eitanlees
145
15k
Typedesign – Prime Four
hannesfritz
40
2.4k
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ϑΝΠϧΛॻ͍ͯΠϯλʔϑΣʔεΛ ɹੜ͢Δ ˠੜ͞ΕͨΠϯλʔϑΣʔεΛ࣮ͬͯ͢Δ