gRPC with Java
Would like to share my learning with you all about the gRPC. There are so many articles around the internet about the why gRPC and when to use that. Just want to showcase here how to implement gRPC server using java.
Maven as package manager and find its dependency below.
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
</dependency>
Below is the maven plugin configuration to generate the java class from the proto file. It is similar like SOAP Web service, generating stub classes and use it wherever we need to use either client or server.
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.19.2:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.47.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Referenced the above configuration from the github repo https://github.com/grpc/grpc-java
Please check it out the reference link for the grpc maven plugin https://www.xolstice.org/protobuf-maven-plugin/usage.html
Find the steps below for the gRPC service creation process
Defining the proto
Let us design the user service to add, get and find all user related data. Find the sample proto file below for the same.
https://grpc.io/docs/what-is-grpc/introduction/
syntax = "proto3";
package com.ask.grpc;
option java_package = "com.ask.grpc.proto";
option java_generic_services = true;
option java_outer_classname = "UserDetailProto";
option java_multiple_files = true;
import "google/protobuf/empty.proto";
service UserService {
rpc save(User) returns (User) {}
rpc get(User) returns (User) {}
rpc findAll (google.protobuf.Empty) returns (stream User) {}
}
message User {
string id = 1;
string name = 2;
string email = 3;
optional int32 age = 4;
repeated Address addresses = 5;
}
message Address {
string address1 = 1;
string address2 = 2;
string state = 3;
string city = 4;
string country = 5;
string phoneNumber = 6;
PhoneType phoneType = 7;
}
enum PhoneType {
MOBILE = 0;
HOME = 1;
}
Proto compilation and Stub Generation
As already added the above plugin, run the below maven command to generate the java class for the grpc proto
mvn clean install
Stubs are generated and use the Stub classes and implement the changes
To implement the user service, just used the HashMap to store and retrieve the user details for the simplicity.
Implement gRPC Server
Creating the gRPC server with UserService on the port 8080.
package com.ask.grpc;
import com.ask.grpc.service.UserServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;
public class Application {
public static void main(String[] args) throws InterruptedException, IOException {
final Server server = ServerBuilder.forPort(8080).addService(new UserServiceImpl()).build();
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
server.shutdown();
System.out.println("Successfully stopped the server");
}));
server.start();
System.out.println("Server started");
server.awaitTermination();
}
}
gRPC Client
To invoke the gRPC server, used the Bloom-rpc as a client.
installation details are in the link https://github.com/bloomrpc/bloomrpc
Let’s load the proto file into the client and invoke the gRPC server.
Detailed code in the following repo https://github.com/saravanastar/grpc-java