Implementing gRPC with Java project
What is RPC?
Remote Procedure Call (RPC) is a protocol that one program can use to request a service from a program located in another computer on a network without having to understand the network’s details. A procedure call is also known as a function call or a subroutine call.

gRPC is a modern open source high performance RPC framework developed by Google. The gRPC provides simple service definition, works across different languages and platforms. Similar to many RPC service, gRPC revolves around the idea of specifying and defining the service that can called remotely with the parameters and return types. Internally it uses HTTP/2 for transport and protocol buffers as the interface description language. To know more about HTTP/2.
Protocol buffer
Protocol buffer is google’s open source mechanism which is language neutral, platform neutral, extensible way of serialising the structured data for communication like XML, but smaller, faster, and simpler.
Advantages over XML
Protocol Buffers
- are simpler
- are 3 to 10 times smaller
- are 20 to 100 times faster
- are less ambiguous
- generate data access classes that are easier to use programmatically
Implementing in Java
For Implementing this in Java, we have to
- Define a service in .proto file
- Generate server and client code using the protocol buffer compiler.
- Use the Java gRPC API to write a simple client and server for your service
Initially create a maven project in Eclipse.
Open Eclipse →File →new →other →Maven Project
Once the Project is Created, create a new package and add proto (products.proto) file in it.
The Dependencies required for the project are protobuf-java, grpc-netty-shaded, grpc-protobuf, grpc-stub.
MySQL database is used for this project, so adding that dependency too.
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>3.6.1</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId><version>1.15.1</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.15.1</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>1.15.1</version></dependency>
To generate the protobuf based code we have to use protobuf-maven-plugin for the Maven build system:
<plugin><groupId>com.github.os72</groupId><artifactId>protoc-jar-maven-plugin</artifactId><version>3.6.0.1</version><executions><execution><phase>generate-sources</phase><goals><goal>run</goal></goals><configuration><includeMavenTypes>direct</includeMavenTypes><inputDirectories><include>src/main/resources</include></inputDirectories><outputTargets><outputTarget><type>java</type><outputDirectory>src/main/java</outputDirectory></outputTarget><outputTarget><type>grpc-java</type><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.15.0</pluginArtifact><outputDirectory>src/main/java</outputDirectory></outputTarget></outputTargets></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin>
Now we have completed the dependencies part. After adding these, right click pom.xml →run →maven build..

Once the build is successful, we will get the stub file for the proto we have defined. After finishing these we have to create the Service class (ProductService.java) , which extends the abstract class (productsImplBase). Then we should implement all methods defined in the abstract class and write the logic in each method as per our use case.
In my case, database operations like creating a MySQL Database, and adding one table with ID as primary key.
Run these queries sequentially in your MySQL terminal.

After finishing these, create gRPC server which hosts these in a particular port in your local machine.
After doing this compile this code and check whether this process is running in the port number which you have configured. If the process is running in the particular port then proceed in creating gRPC client specifying the address and port number that is mentioned in the gRPC server. Then write the required logic to call the method. Compile and run the program you’ll get desired output.
For the whole github repo :
https://github.com/deepakchandh/gRPC-java-implementation
I would love to hear the feedback on this post, please let me know in comments.
References :
