Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Go / Node.js で入門する gRPC
enta0701
December 21, 2018
0
100
Go / Node.js で入門する gRPC
enta0701
December 21, 2018
Tweet
Share
More Decks by enta0701
See All by enta0701
Kubernetes 超入門
endotakuya
0
97
GKE と EKS について 理解した気分になる7分間
endotakuya
0
45
はじめての CircleCI × EKS
endotakuya
1
490
LINE Clova スキルの開発ハンズオンに enebular は最適だった話。
endotakuya
1
710
LINE Clova と自由に会話をしよう
endotakuya
0
140
GoでClova Extension開発ができるようになりました
endotakuya
0
40
シンプルなテンプレートエンジンが欲しい
endotakuya
0
540
Alfred Workflows by Go
endotakuya
4
1.9k
GoでGemを作っています
endotakuya
0
490
Featured
See All Featured
Fireside Chat
paigeccino
12
1.3k
Why Our Code Smells
bkeepers
PRO
324
55k
Practical Orchestrator
shlominoach
178
8.6k
A designer walks into a library…
pauljervisheath
196
16k
BBQ
matthewcrist
74
7.9k
Documentation Writing (for coders)
carmenhchung
48
2.6k
Learning to Love Humans: Emotional Interface Design
aarron
261
37k
Support Driven Design
roundedbygravity
86
8.5k
Become a Pro
speakerdeck
PRO
3
840
A Philosophy of Restraint
colly
192
15k
Infographics Made Easy
chrislema
233
17k
Atom: Resistance is Futile
akmur
255
20k
Transcript
Go / Node.js で入門する gRPC Created by @endotakuya
gRPC 使ってますか?
gRPC Google が2015 年2 月に公開した OSS の RPC フレームワーク Features
IDL による言語に依存しないインターフェースの定義 API 設計におけるサーバ/ クライアントや他言語の考慮不要 ドキュメント管理不要 HTTP/2 による通信
None
そもそも RPC とは 遠隔手続き呼出し(リモートプロシージャコール) Features ローカルなサブルーチン呼び出しと基本的に同じコードでリモート呼び 出しを行える プログラムから別のアドレス空間(通常、共有ネットワーク上の別 のコンピュータ上)にあるサブルーチンや手続きを実行することを 可能にする技術
“ “
How to use
0. 準備 protoc コマンドや、必要なパッケージをインストール $ brew upgrade protobuf $ go
get -u google.golang.org/grpc $ go get -u github.com/golang/protobuf/protoc-gen-go ささっと試したい方はこちら github.com/endotakuya/grpc-example $ go run server.go # Run server $ node /front/app.js # Run Client
1. .proto ファイル の生成 サーバ/ クライアントで呼び出すメソッドを定義(一部省略) service ArticleService { rpc
First (Empty) returns (Article) {} rpc Post (Article) returns (Empty) {} } message Article { int32 id = 1; string title = 2; string content = 3; enum Status { DRAFT = 0; PUBLISH = 1; } Status status = 4; }
2. .proto をコンパイル サーバ/ クライアントのひな形になるコードを生成 今回は Go 用に吐き出し $ protoc
article/article.proto --go_out=plugins=grpc:. article.pb.go が生成される
3. サーバ側の実装 article.proto に定義した、First メソッド、 Post メソッドを実装 article.pb.go の ArticleServiceServer
を確認 # article.pb.go type ArticleServiceServer interface { First(context.Context, *Empty) (*Article, error) Post(context.Context, *Article) (*Empty, error) }
3. サーバ側の実装 First メソッド( server.go#L28 ) type server struct{} func
(s *server) First(ctx context.Context, in *pb.Empty) (*pb.Article, error) db, _ := initDb() defer db.Close() article := pb.Article{} dist := []interface{}{&article.Id, &article.Title, &article.Content, &article.Stat err := db.QueryRow("SELECT * FROM articles ORDER BY id DESC LIMIT 1").Scan(dist... return &article, err }
3. サーバ側の実装 Post メソッド( server.go#L38 ) func (s *server) Post(ctx
context.Context, in *pb.Article) (*pb.Empty, error) { db, _ := initDb() defer db.Close() stmtIns, err := db.Prepare(fmt.Sprintf("INSERT INTO %s (title, content, status) VA defer stmtIns.Close() _, err = stmtIns.Exec(in.Title, in.Content, in.Status) return &pb.Empty{}, err }
4. クライアント側の実装 多言語での動作を確認するため、Nodejs で実装 入れたパッケージはこちら { "dependencies": { "body-parser": "^1.18.3",
"ejs": "^2.6.1", "express": "^4.16.4", "grpc": "^1.16.1", "grpc-caller": "^0.11.0" } }
4. クライアント側の実装 First メソッドを叩いて、記事を1件取得 const PROTO_PATH = __dirname + '/../article/article.proto';
const SERVER_HOST = process.env.SERVER_HOST || 'localhost'; const client = caller(`${SERVER_HOST}:50051`, PROTO_PATH, 'ArticleService'); ... app.get('/', (req, res) => { client.first({}, (err, response) => { res.send(response); }); }); Nodejs だと直接 article.proto を読み込めばOK
5. 動作確認 +----+-----------------+--------------------------------+--------+ | id | title | content |
status | +----+-----------------+--------------------------------+--------+ | 3 | メリークリスマス | テーマ忘れてた | 1 | +----+-----------------+--------------------------------+--------+ Go のサーバが立ち上がっていることを確認して、 express を起動 $ node app.js Listening on port 3000
None
クライアント側の実装 Post メソッドを叩いて、記事を登録 app.post('/new', (req, res) => { let data
= { title: req.body.title, content: req.body.content, status: parseInt(req.body.status, 10) }; client.post(data, (err, response) => { // ... }); });
http://localhost:3000/new にアクセスし、フォームから送信 Insert されれば成功
便利なコマンド
grpc_cli クライアントを作成してデバックするのが面倒… grpc_cli なら CLI 上から gRPC によるインターフェース等を確認できる インストール方法は次を参考に ローカル環境でgRPC
をデバッグする方法 ただ、途中こけるので、以下を実行 $ brew install gflags
call 実際にメソッドを呼ぶ $ grpc_cli call localhost:50051 ArticleService.First '' connecting to
localhost:50051 id: 3 title: " お腹空いた" content: " 肉が食べたい" status: PUBLISH Rpc succeeded with OK status
詳細は Qiita へ Go / Node.js で入門する gRPC
いい gRPC ライフを