...

/

Configuring the gRPC Server

Configuring the gRPC Server

Create a project to implement the gRPC server and add the required dependencies.

Creating a gRPC server

Creating a gRPC server is a two-step process:

  1. We will override the FTP service base class generated from the service definition to do the actual "work" of our service.

  2. We will run a gRPC server to listen for client requests and return service responses using the logic implemented in the service class in the previous step.

As of now, the course project (created earlier) has one module named ftp-service-proto. Now we will add another module for the server implementation.

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 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 as the "ArtifactId."

This module will automatically be added to the POM file of the grpc-ftp-project as can be seen 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>
</modules>
POM file of the parent project

Copy the following code to the POM file of the ftp-service module:

<?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>
<artifactId>ftp-service</artifactId>
<version>1.0.2</version>
<name>ftp-service</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>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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
</dependency>
<dependency>
<groupId>io.datajek</groupId>
<artifactId>ftp-service-proto</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>io.datajek</groupId>
<artifactId>ftp-service-client</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<finalName>ftp-service-${project.version}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>io.datajek.ftpservice.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resource-one</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>config.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
POM file of ftp-service project

The dependencies and build configuration in ...