✒️
[Lab] gRPC
  • Introduction
  • Environment setup and prerequisite
  • Write your .proto file
  • Generate server and client code in Java
  • Implement the server code in Java
  • Implement the client in Java
  • Implement the client in GO
Powered by GitBook
On this page

Was this helpful?

Generate server and client code in Java

After creating the .proto, we can generate the server and client Java code by using protobuf compiler (protoc) or Maven's gRPC Java plugin for protobuf compiler.

Maven is a build automation tool used primarily for Java projects. In this lab, we will use Maven for:

  1. Managing the dependency of gRPC

  2. Generate the gRPC server and client code

  3. Compile all the source code

pom.xml is needed (just like Makefile) to define the details of our project.

Create the pom.xml in the root of your workspace:

cd ~/grpclab
nano pom.xml
pom.xml
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cslab</groupId>
    <artifactId>grpclab</artifactId>

    <packaging>jar</packaging>
    <!-- Feel free to delete the comment at the end of these lines. It is just
         for safely updating the version in our release process. -->
    <version>1</version><!-- CURRENT_GRPC_VERSION -->
    <name>examples</name>
    <url>https://github.com/grpc/grpc-java</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <grpc.version>1.24.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
        <protobuf.version>3.10.0</protobuf.version>
        <protoc.version>3.10.0</protoc.version>
        <!-- required for jdk9 -->
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>${grpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>${grpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>${grpc.version}</version>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}: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-enforcer-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <id>enforce</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireUpperBoundDeps/>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <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>
        </plugins>
    </build>
</project>

Next, execute the following command in your workspace. The "-o" flag is specified to use local dependencies.

mvn -o generate-sources

Two Java files are generated:

~/grpclab/target/generated-sources/protobuf/grpc-java/cslab/grpclab/CalculatorGrpc.java

~/grpclab/target/generated-sources/protobuf/java/cslab/grpclab/CalculatorOuterClass.java

In CalculatorGrpc.java , it contains the Java classes which represents CalculatorServices and the rpc defined in .proto.

In CalculatorOuterClass.java, it contains the Java classes which represents CalculationRequest and CalculationResponse defined in .proto.

We won't edit these 2 files. We will use or extend them in the following sections.

PreviousWrite your .proto fileNextImplement the server code in Java

Last updated 6 months ago

Was this helpful?