
An introduction to gRPC and protocol buffers.
What is GRPC
gRPC is an open source remote procedure call (RPC) system initially developed at Google in 2015 as the next generation of the RPC infrastructure. It uses HTTP/2 for transport and provides features such as authentication, etc.
Why GRPC
It is a modern open source high performance Remote Procedure Call framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, etc.
Service Definition
Like many RPC systems, it is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. By default, it uses protocol buffers.
Protocol buffers : Protocol buffers provide a language-neutral extensible mechanism for serializing structured data in a forward-compatible and backward-compatible way. It’s like JSON, except it’s smaller and faster.
Some of the advantages of using protocol buffers include:
- Compact data storage
- Fast parsing
- Availability in many programming languages
- Optimized functionality through automatically-generated classes.
Example
This guide gets you started with this in Java with a simple working example.
The example code is part of the grpc-java repo.
- Download the repo as a zip file and unzip it, or clone the repo:
$ git clone -b v1.45.1 --depth 1 https://github.com/grpc/grpc-java
2. Change to the examples directory:
$ cd grpc-java/examples
Run the example
From the examples
directory:
- Compile the client and server:
$ ./gradlew installDist
2. Run the server:
$ ./build/install/examples/bin/hello-world-server
INFO: Server started, listening on 50051
3. Run the client:
$ ./build/install/examples/bin/hello-world-client
INFO: Will try to greet world ...
INFO: Greeting: Hello world
You’ve just run a client-server application with gRPC.