๐๐ปโ๏ธ
์๋
ํ์ธ์! ํ
์ผ๋ฌ์
๋๋ค. ์ค๋์ gPRC
์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
gRPC๋ ๋ค์ํ Language/Framework๋ก ๊ตฌ์ฑ๋ ๋ถ์ฐ application, service ๋ค์ ํจ์จ์ ์ผ๋ก ์ฐ๊ฒฐํ๊ธฐ ์ํด ๊ฐ๋ฐ๋ ์คํ์์ค์ ๋๋ค.
gRPC์ ๋ํด ์์๋ณด๊ธฐ ์ ์ RPC
(Remote Procedure Call)์ ๋ํด ๋จผ์ ์์๋ณด๊ณ ,
gRPC์ ๋ํ ์๊ฐ, gRPC์์ ์ฌ์ฉํ๊ณ ์๋ protocol buffer ์๊ฐ, ์๋์๋ฆฌ๋ฅผ ์์ฐจ์ ์ผ๋ก ์์๋ณด๊ฒ ์ต๋๋ค.
์ด ๋ฌธ์๋ gRPC ๊ณต์๋ฌธ์ ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋์์ต๋๋ค.
Background: RPC
RPC
(Remote Procedure Call): ์๊ฒฉ ํ๋ก์์ ธ ํธ์ถ์๊ฒฉ ํ๋ก์์ ํธ์ถ(์์ด: remote procedure call, ๋ฆฌ๋ชจํธ ํ๋ก์์ ์ฝ, RPC)์ ๋ณ๋์ ์๊ฒฉ ์ ์ด๋ฅผ ์ํ ์ฝ๋ฉ ์์ด ๋ค๋ฅธ ์ฃผ์ ๊ณต๊ฐ์์ ํจ์๋ ํ๋ก์์ ๋ฅผ ์คํํ ์ ์๊ฒํ๋ ํ๋ก์ธ์ค ๊ฐ ํต์ ๊ธฐ์ ์ด๋ค.
์ํคํผ๋์ https://ko.wikipedia.org/wiki/์๊ฒฉ_ํ๋ก์์ _ํธ์ถ
์์ฆ ๋ง์ ์๋น์ค๋ค์ด MSA ๊ตฌ์กฐ๋ก ๊ฐ๋ฐ๋๋ฉด์ ๋ค์ํ ์ธ์ด, ํ๋ ์์ํฌ๊ฐ ์ฌ์ฉ๋๋ ํ๊ฒฝ์์ ํ๋กํ ์ฝ์ ๋ง์ถฐ์ ํต์ ํด์ผํ๋ ๋น์ฉ์ด ๋ฐ์๋์๊ณ , ์ด ๋ RPC
๋ฅผ ์ด์ฉํด ๊ฐ๋ฐํ๊ฒฝ์ ๊ตฌ์ ๋ฐ์ง ์๊ณ ๊ฐ ํ๋ก์์ ๋ฅผ ํธ์ถํด์ ์ฌ์ฉํ ์ ์๊ฒ ๋์์ต๋๋ค.
Introduction to gRPC
gRPC
A high performance, open source, general-purpose RPC framework
์ด ํ์ด์ง์์๋ gRPC
์ protocol buffer
์ ๋ํด ์๊ฐํฉ๋๋ค.
gRPC
๋ protocol buffer
๋ฅผ ์ฌ์ฉํ๊ณ ์๊ณ , ์๋ ๋๊ฐ์ง ์ฉ๋๋ก ์ฌ์ฉํฉ๋๋ค.
- Interface Defintion Language(IDL)
- Message Interchange Format
Overview
gRPC์์๋, ํด๋ผ์ด์ธํธ application์ ๋ค๋ฅธ ๋จธ์ ์ ์์นํ ์๋ฒ application์ ๋ง์น local object์ ์๋ ๊ฒ์ฒ๋ผ ์ง์ ํธ์ถํด์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด distributed application, service๋ฅผ ๋์ฑ ์ฝ๊ฒ ๋ง๋ค ์ ์๊ฒ ๋ฉ๋๋ค.
๋ค๋ฅธ ๋ง์ RPC ์์คํ ๋ค์ฒ๋ผ, gRPC๋
- ์๋น์ค๋ฅผ ์ ์ํ๊ณ ,
- parameters, return types๋ก ์๊ฒฉํธ์ถ์ด ๋ ์ ์๋ ๋ฉ์๋๋ฅผ ์ง์
ํ๋ ๊ฒ์ ๊ธฐ๋ฐ์ผ๋ก ์๋ํฉ๋๋ค.
- Server side Interface๋ฅผ ๊ฐ๋ฐํ๊ณ , ํด๋ผ์ด์ธํธ ํธ์ถ์ ํธ๋ค๋งํ ์ ์๋
gRPC Server
๋ฅผ ์คํํฉ๋๋ค.
- Client side ์๋ฒ์ ๋์ผ ๋ฉ์๋๋ฅผ ๊ฐ๋ฆฌํค๋
gRPC Stub
์ ๊ฐ์ง๋๋ค. *gRPC Stub: ํน์ ์ธ์ด์์๋ ๋จ์ํClient
๋ผ๊ณ ์ง์นญ
gRPC ํด๋ผ์ด์ธํธ์ ์๋ฒ๋ ๋ค์ํ ํ๊ฒฝ์์ ์ํ๋๋ฉฐ ํต์ ํ ์ ์๊ณ , gRPC์์ ์ ๊ณตํ๋ ๋ค์ํ ์ธ์ด๋ก ์์ฑ๋ ์ ์์ต๋๋ค.
์๋ฅผ๋ค์ด gRPC ์๋ฒ๋ Java๋ก ๋ง๋ค๊ณ , client๋ Go, Python ํน์ Ruby๋ก ๋ง๋ค์ ์์ต๋๋ค.
์ต์ Google API์์ ์ธํฐํ์ด์ค์ gRPC ๋ฅผ ์ง์ํ๋ฏ๋ก Google์ ๊ธฐ๋ฅ๋ค์ ์ฝ๊ฒ ๋น์ ์ application์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Working with Protocol Buffers
Protocol Buffer ์ด๋?
๊ธฐ๋ณธ์ ์ผ๋ก gRPC๋ Protocol Buffer๋ฅผ ์ฌ์ฉํฉ๋๋ค.
structured data(๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ)๋ฅผ serializing(์ง๋ ฌํ)ํ๊ธฐ ์ํด Google์์ ๊ฐ๋ฐํ ์ค๋๋ ์คํ์์ค ๋ฉ์นด๋์ฆ์ผ๋ก JSON๊ณผ ๊ฐ์ ๋ค๋ฅธ ๋ฐ์ดํฐ ํฌ๋งท๋ค๊ณผ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค.
Protocol Buffer ์๋ ์๋ฆฌ
proto
ํ์ผ์ ์์ฑํจ์ผ๋ก์จ protocol buffer message๋ฅผ ๊ตฌ์ฒดํํ๊ณ ์ด๋ฅผ method parameter, return type์ผ๋ก ๊ฐ์ง๋ gRPC ์๋น์ค๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
1. proto file๋ก ์๋ฆฌ์ผ๋ผ์ด์ฆ ํ๊ณ ์ํ๋ ๋ฐ์ดํฐ structure ์ ์
- proto file:
.proto
ํ ์คํธํ์ผ
- Protocol Buffer data๋
message
๋ก ๊ตฌ์กฐํ ๋ฉ๋๋ค. ๊ฐ ๋ฉ์์ง๋ ์์ ๋ ผ๋ฆฌ์ ์ ๋ณด ๋ ์ฝ๋๋กfield
๋ผ๊ณ ์ง์นญ๋๋ name-value ์กฐํฉ์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.message Person { string name = 1; int32 id = 2; bool has_ponycopter = 3; }
2. protoc
์ ์ฌ์ฉํ์ฌ data access class๋ฅผ ์ํ๋ language๋ก ์์ฑ
protoc
: protocol buffer compiler- ๊ฐ field ์ ๊ทผ์๋ฅผ ์ ๊ณต. ex.
name()
,set_name()
- ๊ฐ ๊ตฌ์กฐ์ฒด๋ฅผ raw bytes๋ก serialize/parse ํ๊ธฐ ์ํ ๋ฉ์๋
- ๋ง์ฝ ๋น์ ์ด ์ ํํ ์ธ์ด๊ฐ C++์ด๋ผ๋ฉด, ์ปดํ์ผ๋ฌ๋ฅผ ํตํด ์
Person
์ด๋ผ๋ ๊ตฌ์กฐ์ฒด๋ฅผ C++ class๋ก ์์ฑํ ๊ฒ์ ๋๋ค. ์์ฑ๋ ํด๋์ค๋ฅผ ๋น์ ์ application์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- ๊ฐ field ์ ๊ทผ์๋ฅผ ์ ๊ณต. ex.
.proto
Example
// gRPC ์๋น์ค์ ์
// The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// protocol buffer message ์ ์
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
gRPC์์๋ ํน๋ณํ gRPC ํ๋ฌ๊ทธ์ธ๊ณผ ํจ๊ป protoc
์ ์ฌ์ฉํ์ฌ proto file ๋ก๋ถํฐ ์ฝ๋๋ฅผ ์์ฑํ๊ณ , ์ด๋ฅผ ํตํด gRPC client, server ์ฝ๋๋ฅผ ๋ง๋ค๊ฒ๋ฉ๋๋ค.
protocol buffer์ ๋ํ ์์ธํ ์ค๋ช
๊ณผ, ๋น์ ์ด ์ ํํ ์ธ์ด๋ฅผ ์ง์ํ๋ gRPC ํ๋ฌ๊ทธ์ธ๊ณผ ํจ๊ป protoc
์ค์น๋ฐฉ๋ฒ์ด ๊ถ๊ธํ๋ค๋ฉด protocol buffers documentaion์ ์ฐธ์กฐํ์ธ์.
Protocol buffer versions
protocol buffer๋ ์คํ์์ค๋ก ๊ณต๊ฐ๋์ด์์ด ํด๋น ์ฌ์ดํธ์ ๋๋ถ๋ถ์ ์์๋ค์ Proto3(protocol buffer version 3)๋ฅผ ์ฌ์ฉํฉ๋๋ค. Proto3๋ ๊ฐ๋จํ ๋ฌธ๋ฒ, ์ฌ๋ฌ ์ ์ฉํ ํผ์ฒ๋ค, ๊ทธ๋ฆฌ๊ณ ๋ ๋ค์ํ ์ธ์ด๋ค์ ์ง์ํฉ๋๋ค.
์ง์ ์ธ์ด๋ณ GitHub Repo
- protocol buffers GitHub repo
Java, C++, Dart, Python, Objective-C, C#, a lite-runtime (Android Java), Ruby, and JavaScript
์ธ์ด๋ณ ์์ธํ ๊ฐ์ด๋
- reference documentation :
.proto
ํ์ผ ํฌ๋งท ๋ช ์ธ๋ ์ด๊ณณ์์ ํ์ธ๊ฐ๋ฅํฉ๋๋ค.
Proto2(ํ์ฌ์ ๊ธฐ๋ณธ protocol buffer version)๋ ์ฌ์ฉ๊ฐ๋ฅํ์ง๋ง gRPC์์ ์ ๊ณตํ๋ ๋ค์ํ ์ธ์ด๋ค์ ๋ชจ๋ ์ฌ์ฉํ๋ ค๋ฉด Proto3๋ฅผ ์ฌ์ฉํ๋๊ฒ์ ์ถ์ฒํฉ๋๋ค. proto2-proto3 ๊ฐ ํธํ์ฑ ์ด์๋ ํผํ ์ ์์ต๋๋ค.
Link
https://media.vlpt.us/images/jakeseo_me/post/cbee941a-abc3-40a0-bab3-69c03bb25cba/image.png
Uploaded by Notion2Tistory v1.1.0