Search⌘ K
AI Features

Configuring a gRPC Client

Explore the process of configuring a gRPC client in Java by setting up a Maven module with necessary dependencies and build plugins. Learn how to organize your project structure and apply tools like the Protobuf and Spotless Maven plugins to generate code and maintain formatting for enhanced client implementation.

gRPC client project

We will add a new module implementing the client to the course project (created in Setting up the dev Environment lesson).

If you wish to follow along on your local machine, open the grpc-ftp-project in your IDE and create a "Module" named ftp-service-client with the following properties:

  • Choose "Java" as "Language."

  • Choose "Maven" as the "Build system."

  • We have used io.datajek as the "GroupId."

  • We have used ftp-service-client as the "ArtifactId."

This module will automatically be added to the POM file of the grpc-ftp-project as shown below:

<groupId>io.datajek</groupId>
<artifactId>grpc-ftp-project</artifactId>
<version>1.0.2</version>
<packaging>pom</packaging>
<name>grpc-ftp-project</name>
<modules>
<module>ftp-service-proto</module>
<module>ftp-service</module>
<module>ftp-service-client</module>
</modules>
POM file of the parent project

Copy the following code in the POM file of the newly created ftp-service-client project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.datajek</groupId>
<artifactId>grpc-ftp-project</artifactId>
<version>1.0.2</version>
</parent>
<groupId>io.datajek</groupId>
<artifactId>ftp-service-client</artifactId>
<version>1.0.2</version>
<name>ftp-service-client</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
<dependency>
<groupId>io.datajek</groupId>
<artifactId>ftp-service-proto</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<finalName>ftp-service-client-${project.version}</finalName>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>1.24.3</version>
<configuration>
<java>
<licenseHeader>
<file>/style/guild/license-header</file>
</licenseHeader>
<eclipse>
<file>/style/guild/full.xml</file>
<version>4.12.0</version>
</eclipse>
<removeUnusedImports />
<importOrder>
<file>/style/guild/style.importorder</file>
</importOrder>
</java>
<formats>
<format>
<includes>
<include>src/main/avro/**.avsc</include>
</includes>
<eclipseWtp>
<type>JSON</type>
<files>
<file>/style/guild/json.prefs</file>
</files>
</eclipseWtp>
</format>
</formats>
</configuration>
</plugin>
<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.45.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
POM file of the ftp-service-client project

The dependencies and build configuration in the pom.xml file are explained below:

Required dependencies

In the POM file of the module, we have added the dependencies for junit, grpc-protobufgrpc-nettygrpc-stub, mockito-core, and mockito-inline. Note that we have not used the <version> tag as the parent POM handles the version numbers for all child modules. We have also added the following dependencies: ...