GRPC Spring Boot Tutorial
Google Remote Procedure Call
Google Remote Procedure Call, is an open-source RPC (Remote Procedure Call) framework that uses RPC to communicate. gRPC uses protobuf(default format) instead of JSON&XML messaging formatting and it is built on HTTP 2 to overcome HTTP/1.1 limitations. gRPC services and messages between clients and servers are defined in proto files. Proto files are not owned by any projects, they are separate files, but projects use them as a contract between them. In a way similar to the interfaces that we use in RMI or EJB.
This results in slower implementation than rest but increases message transmission speed. When we look at the microservice architecture the difference is in terms of milliseconds.
But when we talk about hundreds of requests, it will be more meaningful, as it will increase the performance of our microservices.
Apart from pros, it has a non-human readable format and limited browser support. It is not a replacement of rest API but it is another alternative way, for large-scale microservice connections and communications.
For Protobuf scalar types:
For our project first, we will be creating a spring boot application nameserver. Our application will run on port 9435. We will start coding our application with the proto file, which is the backbone of our server and client applications.
According to the proto file, we declare that we will be using the protobuf version3. We write our package names, to avoid any confusion. To create the java files, we use java_package keyword. The keyword message is just like the classes in java.
In pom.xml we will use and declare protobuf-maven-plugin. This plugin is responsible for generating java classes from the proto file. “maven-assembly-plugin” is used to generate a single distributable java archive file. “os-maven-plugin” generates platform-based properties.
Our main class will be com.example.gserver.GServerApplication as declared in pom.xml file.
First, we start with our service implementations. Create, List, Update and Delete. We have already defined them in our proto file as rpc’s. Now we will use these methods and provide some data for them, according to the generated classes.
The GserverApplication class is our main classs.It is used for building and attaching services.
Now we are ready to build and run our gserver application. Please don’t forget to use maven clean build. If you face problems, you may delete the generated classes manually and then try to rebuild your application again.
The next step will be creating our client application. With the client application, we are going to use create, update, list, and delete methods and get the responses from our server application.
When we run our application we will get the following output.
For the complete code samples, please visit Github