Introduction
gRPC (Google Remote Procedure Calls) is an open source remote procedure call.
Why
MSA를 하다 보면 서비스 간 통신이 자주 일어 나게 된다.
서비스 간 통신은 아래와 같은 구조를 같는다.
- 통신 명세가 존재 하고
- 해당 명세를 각 언어에 맞는 serialize/ deserialize 코드를 작성한다.
- network 를 사용 해야 하는 경우 특정 프로토콜로 구현한다.
자동화 해보자.
- 통신 명세 = protobuf.proto
- 각언어 코드 = protoc
- network 간 호출 = http2
Grpc 란
위의 기능을 자동화 한 구현체이다.
Example
Components
- Uses HTTP/2 for transport.
- Protocol Buffers as the interface description language.
Overview
Define
// The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloResponse {
string message = 1;
}
Methods
Unary RPC
rpc SayHello(HelloRequest) return (HelloResponse) {}
sequenceDiagram
client ->>+ stub : sayHello("hi")
stub ->> server : metadata
opt if need
server -->> stub : metadata
end
stub ->>+ server : request
server -->>- stub : respond, status
stub -->>- client : return respond
Server streaming RPC
rpc LotsOfReplies(HelloRequest) return (stream HelloResponse) {}
sequenceDiagram
client ->>+ server : request
server -->> client : respond 1
server -->> client : respond * n
server -->>- client : status
Client streaming RPC
rpc LotsOfRequest(stream HelloRequest) return (HelloResponse) {}
- The server sends back a single response, typically but not necessarily after it has received all the client’s requests
sequenceDiagram
client ->>+ server : request 1
client ->> server : request * n
server -->>- client : respond
Bidirectional streaming RPC
rpc BidHello(steam HelloRequest) return (stream HelloResponse) {}
- The client and server can read and write any order
Features
- Authentication
- Bidirectional streaming and flow control
- Blocking or nonblocking bindings
- Cancellation and timeout