For gRPC Conf 2020 session videos, visit the CNCF YouTube channel

Quick start

This guide gets you started with gRPC in Kotlin with a simple working example.

Prerequisites

  • Kotlin version 1.3 or higher
  • JDK version 7 or higher

Get the example code

The example code is part of the grpc-kotlin repo.

  1. Download the repo as a zip file and unzip it, or clone the repo:

    $ git clone https://github.com/grpc/grpc-kotlin
    
  2. Change to the examples directory:

    $ cd grpc-kotlin/examples
    

Run the example

From the examples directory:

  1. Compile the client and server

    $ ./gradlew installDist
    
  2. Run the server:

    $ ./server/build/install/server/bin/hello-world-server
    Server started, listening on 50051
    
  3. From another terminal, run the client:

    $ ./client/build/install/client/bin/hello-world-client
    Received: Hello world
    

Congratulations! You’ve just run a client-server app with gRPC.

Update the gRPC service

In this section, you’ll update the app with an extra server method. The app’s gRPC service, named Greeter, is defined using protocol buffers. To learn more about how to define a service in a .proto file, see Basics tutorial. For now, all you need to know is that both the server and the client stub have a SayHello() RPC method that takes a HelloRequest parameter from the client and returns a HelloReply from the server, and that the method is defined like this:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Open helloworld/hello_world.proto from the protos/src/main/proto/io/grpc/examples folder, and add a new SayHelloAgain() method, with the same request and response types:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Remember to save the file!

Update the app

When you build the example, the build process regenerates HelloWorldGrpcKt.kt, which contains the generated gRPC client and server classes. This also regenerates classes for populating, serializing, and retrieving our request and response types.

However, you still need to implement and call the new method in the hand-written parts of the example app.

Update the server

Open helloworld/HelloWorldServer.kt from the server/src/main/kotlin/io/grpc/examples folder. Implement the new method like this:

private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
  override suspend fun sayHello(request: HelloRequest) = HelloReply
      .newBuilder()
      .setMessage("Hello ${request.name}")
      .build()

    override suspend fun sayHelloAgain(request: HelloRequest) = HelloReply
      .newBuilder()
      .setMessage("Hello again ${request.name}")
      .build()
}

Update the client

Open helloworld/HelloWorldClient.kt from the client/src/main/kotlin/io/grpc/examples folder. Call the new method like this:

class HelloWorldClient(
    private val channel: ManagedChannel
) : Closeable {
  private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)

  suspend fun greet(name: String) {
    val request = HelloRequest.newBuilder().setName(name).build()
    val response = stub.sayHello(request)
    println("Received: ${response.message}")
    val againResponse = stub.sayHelloAgain(request)
    println("Received: ${againResponse.message}")
  }

  override fun close() {
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
  }
}

Run the updated app

Run the client and server like you did before. Execute the following commands from the examples directory:

  1. Compile the client and server:

    $ ./gradlew installDist
    
  2. Run the server:

    $ ./server/build/install/server/bin/hello-world-server
    Server started, listening on 50051
    
  3. From another terminal, run the client. This time, add a name as a command-line argument:

    $ ./client/build/install/client/bin/hello-world-client Alice
    Received: Hello Alice
    Received: Hello again Alice
    

What’s next