Hello guys, in this post I will introduce you about Vert.x and gRPC and why this combination fits soo well when we are using reactive applications.

First, let me give you an overview of gRPC and Vert.x.

Overview

gRPC is a high-performance, lightweight communication framework designed for making traditional RPC calls that were developed by Google.

Vert.x is an open source, reactive, polyglot toolkit or platform running on the Java Virtual Machine.

Set up

Well, I made a brief explanation about Vert.x and gRPC, now it is time to show how Vert.x and gRPC works together.

First, let us see what we will use to build a simple application:

Let is start, to create a maven project, run the command above:

mvn archetype:generate \
-DgroupId=io.vertx \
-DartifactId=hello-grpc-and-vertx \
-Dversion=3.5.3 \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

In your project add this pom.xml

Talk is cheap. Show me the code

gRPC provides four asynchronous types of requests:

In this post, we will see the unary request using Vert.x.

Unary Non-Blocking RPC

The unary call is the simplest and well-known type of call. It is the famous request and response call, so to do it using Vert.x and gRPC we need to declare a simple rpc call in our proto file and configure our server and client application.

Proto file

syntax = "proto3";
option java_multiple_files = true;
package io.vertx.grpc;

message Request {

int32 id = 1;
string content = 2;

}

message Response {

int32 id = 1;
string content = 2;

}

service Service {

rpc unary(Request) returns (Response);

}

Now, that we declared our methods and services, we can generate a code in any language that supports gRPC, in Java to do this, we need to run the command above:

mvn clean compile

Server

To handle the unaries calls we need to create and deploy a verticle that implements the vertx service and add it to the gRPC server, to do this, we need to create a class that extends AbstractVerticle and implements the ServiceVertxImplBase:

public class Server extends AbstractVerticle {

@Override
public void start() {

BindableService bindableService = new ServiceVertxImplBase() {

@Override
public void unary(Request request, Future<Response> response) {
System.out.println("Receveid " + request.toString());
response.complete(Response.newBuilder().build());
}

};

VertxServer rpcServer = VertxServerBuilder
.forPort(vertx, 8080)
.addService(bindableService)
.build();

rpcServer.start(result -> {
if (result.succeeded()) {
System.out.println("Server started at 8080");
} else {
System.out.println("Fail to start server " + result.cause().getMessage());
}
});

}

public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new Server());
}

}

To make unaries calls we need to create and deploy a verticle that contains a vertx stub, to do this, we need to create a class that extends AbstractVerticle and starts a channel with the server:

public class Client extends AbstractVerticle {

@Override
public void start() {

ManagedChannel channel = VertxChannelBuilder
.forAddress(vertx, "localhost", 8080)
.usePlaintext(true)
.build();

ServiceVertxStub stub = ServiceGrpc.newVertxStub(channel);

Request request = Request.newBuilder().build();

stub.unary(request, result -> {
if (result.succeeded()) {
System.out.println(result.result());
} else {
System.out.println("Fail to get response " + result.cause());
}
});

}

public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new Client());
}

}

Conclusion

In this post, we could see what is gRPC and protocol buffer and how simple is it to start. In the next posts, I will explain how to build microservices using gRPC with spring-boot and vert.x.

References

https://grpc.io/

https://vertx.io/

https://vertx.io/docs/vertx-grpc/java/

https://github.com/vert-x3/vertx-examples/tree/master/grpc-examples


Luram Archanjo

I have been working directly with software development for three years using Java language, open source frameworks and tools, with excellent experience in the execution of projects, from obtaining requirements until the implementation in production. I participated in the most varied sizes of projects, from small projects for internal uses to large projects, involving clients like Google and Edenred. Solid experience in developing microservices application using Java language with the frameworks and concepts such as: Spring (MVC, Security, Boot, Data), Openshift, Kubernetes, Docker, AWS, JMS, AMQP, ORM, API, DDD and CDI.

Leave a Reply

Your email address will not be published.