Home - Blog - How to create simple application interaction through gRPC

How to create simple application interaction through gRPC

01.05.2024

15 min

IT news
blog

To begin with, we will create a program that will be a maven project and add dependencies to it, as well as a plugin for the build:

 

 

Let’s create a user.proto file in src/main/resources in which we will write what will be used by our gRPC connection:

 

 

After that, we build our project and in this way 2 new classes generated based on the proto file should appear:

 

 

Now let’s create a service that will process requests and give responses:

 

Now we can write a server that will run our program, accept and forward these requests to the service:

 

 

Let’s try to run it and as we can see, everything starts without errors, if you have a desire, you can check how this service works through a GUI client, for example, Kreya, but we will create a GrpcClient and test through it.

 

In the same way, we create a project, add the same dependencies, and build a plugin as in our GrpcServer. Also, copy our created user.proto file to the src/main/resources directory.

 

After that, let’s build our GrpcClient using maven, just like for GrpcServer, and ensure the corresponding files have been created.

 

After all these actions, we will create the GrpcClient class, in which we will write the following code:

 

After that, let’s start our GrpcServer and then start GrpcClient and get the following information in the console:

 

 

Now let’s try to change our values to other values that do not match each other:

 

And let’s start our client again:

 

As you can see, everything works as intended and thus we have set up a connection between the two communication programs via gRPC.