✒️
[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
  • Compile your program
  • Execute your CalculatorServer

Was this helpful?

Implement the server code in Java

In this section, we will create our server program. We will

  • Override the service base java class CalculatorGrpc.CalculatorImplBase generated from .proto. Implement the logic of the rpc methods (i.e Sum and Product).

  • Running a gRPC server to handle the request from clients.

# Go to workspace
cd ~/grpclab

# Create directory src/main/cslab/grpclab
mkdir -p src/main/java/cslab/grpclab

#Create CalculatorServer.java in src/main/cslab/grpclab
nano src/main/java/cslab/grpclab/CalculatorServer.java
CalculatorServer.java
package cslab.grpclab;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

public class CalculatorServer {

    // Extend the base class `CalculatorGrpc.CalculatorImplBase` generated by gRPC protobuf compiler
    private static class CalculatorService extends CalculatorGrpc.CalculatorImplBase {

        //The logic of `sum`
        @Override
        public void sum(cslab.grpclab.CalculatorOuterClass.CalculationRequest request,
                        StreamObserver<cslab.grpclab.CalculatorOuterClass.CalculationRespond> responseObserver) {
            // Handle the request from client from `CalculationRequest` object
            float a = request.getA();
            float b = request.getB();

            // Construct `CalculationRespond` object
            CalculatorOuterClass.CalculationRespond respond = CalculatorOuterClass.CalculationRespond.newBuilder()
                    .setResult(a + b) //Set the value of result
                    .build();
            responseObserver.onNext(respond);

            responseObserver.onCompleted();
        }

        //The logic of `product`, just similar as 'sum'
        @Override
        public void product(cslab.grpclab.CalculatorOuterClass.CalculationRequest request,
                            StreamObserver<cslab.grpclab.CalculatorOuterClass.CalculationRespond> responseObserver) {
            float a = request.getA();
            float b = request.getB();

            CalculatorOuterClass.CalculationRespond respond = CalculatorOuterClass.CalculationRespond.newBuilder()
                    .setResult(a * b)
                    .build();
            responseObserver.onNext(respond);

            responseObserver.onCompleted();
        }
    }
    
    //Running a gRPC server to handle the request from clients.
    public static void main(String[] args) throws Exception {
        int tcpPort = 8980;

        //Create a instance CalculatorService
        CalculatorService service = new CalculatorService();

        //Create a instance of gRPC.Server
        final Server server = ServerBuilder.forPort(tcpPort)
                                        .addService(service) //Register your service
                                        .build();

        System.out.println("Start the grpc server.");
        server.start();

        //Stop the server
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.err.println("*** shutting down gRPC server since JVM is shutting down");
                server.shutdown();
                System.err.println("*** server shut down");
            }
        });

        server.awaitTermination();
    }
}

Compile your program

Now you can try to compile your program into Java JAR.

mvn package

grpclab-1-jar-with-dependencies.jar will be generated under target which contains the bytecode of your program and dependency libraries.

Execute your CalculatorServer

You can launch a CalculatorServer by using command:

java -cp target/grpclab-1-jar-with-dependencies.jar cslab.grpclab.CalculatorServer 

You will see the following line is printed on the console:

Start the grpc server.

Press Ctrl+C to kill the gRPC server.

PreviousGenerate server and client code in JavaNextImplement the client in Java

Last updated 6 months ago

Was this helpful?