Sejarah gRPC
● Tahun 2015 Google mempublikasikan gRPC,
yang sekarang digunakan di banyak organisasi di
luar Google untuk memberdayakan kasus
penggunaan dari microservice. gRPC
menggunakan HTTP/2 untuk transport, Protocol
Buffers untuk interface description language,and
memiliki fitur authentication, bidirectional
streaming and flow control, blocking atau
nonblocking bindings, dan cancellation dan
timeouts. gRPC juga generate cross-platform
client dan server bindings untuk banyak bahasa
pemrograman
● gRPC kepanjangan dari Google Remote
Procedure call.
● gRPC adalah teknik remote procedure call (RPC)
yang membawa fitur-fitur modern ke aplikasi client
- server.
● gRPC dapat memanggil fungsi langsung di
komputer yang lain walaupun menggunakan
bahasa pemrograman yang berbeda
Source https://grpc.io/docs/what-is-grpc/introduction/
Bahasa yang official support gRPC
● C# / .NET
● C++
● Dart
● Go
● Java
● Kotlin
● Node
● Objective-C
● PHP
● Python
● Ruby
https://grpc.io/docs/languages/
gRPC API Service Method
1. A unary service method takes one input and
returns one output.
2. A server streaming service method receives
one input from the client and sends a stream
of outputs. It can also send back multiple
outputs as data becomes available.
3. Client streaming service methods open a
connection to a server, and then when the
server acknowledges the stream can begin,
the client side can begin sending data until it
terminates the stream.
4. Bidirectional streaming service methods
simultaneously send and receive data
streams in both directions.
Source: https://blog.knoldus.com/unary-streaming-via-grpc/
gRPC Flow
pic source:https://blog.bytebytego.com/p/ep32-how-does-grpc-work
Step 1: A REST call is made from the client. The request
body is usually in JSON format.
Steps 2 - 4: The order service (gRPC client) receives the
REST call, transforms it, and makes an RPC call to the
payment service. gPRC encodes the client stub into a
binary format and sends it to the low-level transport layer.
Step 5: gRPC sends the packets over the network via
HTTP2. Because of binary encoding and network
optimizations, gRPC is said to be 5X faster than JSON.
Steps 6 - 8: The payment service (gRPC server) receives
the packets from the network, decodes them, and invokes
the server application.
Steps 9 - 11: The result is returned from the server
application, and gets encoded and sent to the transport
layer.
Steps 12 - 14: The order service receives the packets,
decodes them, and sends the result to the client application.
Instalasi
1. Golang versi terakhir
https://go.dev/doc/devel/release
2. Protocol Buffer compiler
https://github.com/protocolbuffers/protobuf/releases
1. Go plugins for the protocol compiler
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
Instalasi protocol buffer (4)
$ git clone -b v1.50.0 --depth 1 https://github.com/grpc/grpc-go
$ go run greeter_server/main.go
download contoh project
run server grpc
run server client
$ go run greeter_client/main.go
Generate proto
$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-
grpc_opt=paths=source_relative helloworld/helloworld.proto