gRPC with Spring Boot
In this tutorial, we are going to discuss gRPC. gRPC is a framework which is used to work with RPC. RPC stands for Remote Procedural Calls. It is an alternative to HTTP Protocol. gRPC is developed by Google. It is very fast compared to HTTP. Because of that, It is becoming very popular. If we use Go Lang gRPC will be the default Protocol.
In gRPC (g) has a meaning which changes with the release of every version. Here g does not stand for google.
In this article, we will be understanding about-
- Internal working of gRPC.
- How to use gRPC with Spring Boot.
What is RPC:
RPC is a way of communication between client and server, In HTTP we make an HTTP call to a different method, but in RPC we just use the function. call.
It uses IDL (Interface Definition Language) as a form of contract on functions to be called and on the data type.
Similar to request and response objects in HTTP, here we use call Packet (It has data related to method call) and Result Packet. (It has info about the return type of the result).
It is just an improved version of the Old technique where we used request and response objects in HTTP Protocol.
Why gRPC is Popular:
gRPC is popular because of Microservices, In Microservices there is a lot of communication between different services which are developed using different programming Languages.
So for service-to-service communication gRPC is easy to use.
Performance wise gRPC is very much improved. why?
Because as we know it is just an improved version of HTTP, So HTTP is used in every application and it was developed long back, But in 2015 latest HTTP/2 came, which was developed by Google and the same is used by gRPC.
How gRPC is faster:
- In HTTP/1 multiple calls were not possible within one connection. Like sending multiple requests and getting multiple responses. but in HTTP/2 a new layer was introduced called Binary Framing.
It uses two frames Header frame and a data frame, In both frames we just put the header and data respectively for multiple requests.
2. HTTP/2 uses HPack for Header Compression, sometimes headers become larger than data.
ProtoBuf:
ProtoBuf which are similar JSON Schemas. Protobuf is the most commonly used IDL (Interface Definition Language) for gRPC. It’s where you basically store your data and function contracts in the form of a proto file.
This file has a method declared and it should be similar in both client and server, It works as a bridge between client and server.
void printhello();
Apart from that gRPC offers:
- Load Balancing
- Streaming
- Interceptors
For using gRPC with Spring Boot.
- Create a project from the spring starter
- Add gRPC starter dependency from here.
- After that create projects for storing interfaces and services and use them.
Next, we can discuss how we can use gRPC with microservices and further development work with Spring boot.
Thanks for reading!!