Setup and run a gRPC project

A gRPC program in Java with Maven builder

Lucian Ritan
4 min readApr 21, 2021

In this article, we will set up a gRPC project through two approaches. The first approach is starting from an existing project, and the second one is from scratch.

Starting from an existing project

The project we start from is named: Book Finder, it is a personal project.

  1. Download the project Book Finder, or clone it and revert it to the commit named: Added an example of gRPC HelloWorld application.
  2. Open each project separately. Depending on the IDE chosen (I recommend IntelliJ), the projects will be automatically built or not. If not, press the build button or CTRL + F9 in IntelliJ.
  3. Maven install.
  4. Run the server.
  5. Now run as many client projects as you want.
Result after running the client

The client will automatically disconnect after receiving the response. The server will continue running after receiving the client requests even if the clients close the connection.

You will get an error: java cannot find a symbol. This error does not affect the program execution. If you want to fix the error now, insert the dependency below in your pom, the only drawback found is that we can’t generate documentation.

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>

P.S: On this run, I used java SDK 12. File -> Project Structure (Ctrl + Alt + Shift + S ) in IntelliJ to check yours. If your current SDK does not work, check with another SDK, you can download a few directly from IntelliJ.

From the ground up

  1. Create projects.
  2. Add dependencies.
  3. Porto file/files.
  4. Generate files.
  5. Service implementation.
  6. Server main.
  7. Client main.

First and foremost, let’s create two Maven projects

You can name your projects as you wish, but I suggest you Client and Server for simplicity. As for the artifact and group id, my choice is:

group id: org.example

artifact id: client. For client.

artifact id: server. For server.

Add all gRPC dependencies

You can use these dependencies for both projects (Client and Server).

  • grpc-netty-shaded - transport
  • grpc-protobuf - a client-server common language
  • grpc-stub - client implementation

And we also need a plugin that generates Java classes from your proto file.

Some mentions:

  • You will find pom.xml in the root folder.
  • Your proto files must be in the resources folder.
  • Your generated classes will be in the java folder.

If you want to change the locations, change the path between tags <inputDirectories> and <outputTarget>.

Let’s say you create a new folder in the resources folder named proto, and here you will put all of your proto files. Now you must change the <inputDirectories> tag as below.

<inputDirectories>
<include>src/main/resources/proto</include>
</inputDirectories>

Proto file

Because I don’t want to go into too many details, I will give you directly the proto file. I will leave some comments on the code though.

syntax = "proto3"; /* default is proto2 */
option java_package = "proto";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
/*type name = id; */
string name = 1; /* Id must be bigger than 0 and be the same on client and server */
}
message HelloReply {
string message = 1;
}

We will have the same proto on both the server and the client. Do not forget to create the proto file where you mentioned in pom.xml.

Server project structure (IntelliJ)

Maven install

Maven install (IntelliJ)

After command execution, you will have a new structure.

New files generated

Comand install generates a folder proto along with two new java files.

Service Implementation

Let’s create a new folder where we will put our server logic.

Server main

Client main

And that is it!

As soon as possible, I will do other articles about gRPC starting from the same project. If you find this article helpful, give the repo a star. ⭐

--

--