mirror of https://github.com/grpc/grpc.io.git
parent
e551e5798e
commit
d9a53abd9e
|
|
@ -1,6 +1,7 @@
|
||||||
---
|
---
|
||||||
title: Quick start
|
title: Quick start
|
||||||
description: This guide gets you started with gRPC in Kotlin with a simple working example.
|
description: This guide gets you started with gRPC in Kotlin with a simple working example.
|
||||||
|
spelling: cSpell:ignore println
|
||||||
weight: 10
|
weight: 10
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -9,21 +10,24 @@ weight: 10
|
||||||
- [Kotlin][] version 1.3 or higher
|
- [Kotlin][] version 1.3 or higher
|
||||||
- JDK version 7 or higher
|
- JDK version 7 or higher
|
||||||
|
|
||||||
### Download the example
|
### Get the example code
|
||||||
|
|
||||||
You'll need a local copy of the example code to work through this quick start.
|
The example code is part of the [grpc-kotlin][] repo.
|
||||||
Download the example code from our GitHub repository (the following command
|
|
||||||
clones the entire repository, but you just need the examples for this quick start
|
1. [Download the repo as a zip file][download] and unzip it, or clone
|
||||||
and other tutorials):
|
the repo:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Clone the repository at the latest release to get the example code:
|
|
||||||
$ git clone https://github.com/grpc/grpc-kotlin
|
$ git clone https://github.com/grpc/grpc-kotlin
|
||||||
# Navigate to the examples:
|
```
|
||||||
|
|
||||||
|
2. Change to the examples directory:
|
||||||
|
|
||||||
|
```sh
|
||||||
$ cd grpc-kotlin/examples
|
$ cd grpc-kotlin/examples
|
||||||
```
|
```
|
||||||
|
|
||||||
### Run a gRPC application
|
### Run the example
|
||||||
|
|
||||||
From the `examples` directory:
|
From the `examples` directory:
|
||||||
|
|
||||||
|
|
@ -36,29 +40,29 @@ From the `examples` directory:
|
||||||
2. Run the server:
|
2. Run the server:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ ./build/install/examples/bin/hello-world-server
|
$ ./server/build/install/server/bin/hello-world-server
|
||||||
|
Server started, listening on 50051
|
||||||
```
|
```
|
||||||
|
|
||||||
3. From another terminal, run the client:
|
3. From another terminal, run the client:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ ./build/install/examples/bin/hello-world-client
|
$ ./client/build/install/client/bin/hello-world-client
|
||||||
|
Received: Hello world
|
||||||
```
|
```
|
||||||
|
|
||||||
Congratulations! You've just run a client-server application with gRPC.
|
Congratulations! You've just run a client-server app with gRPC.
|
||||||
|
|
||||||
### Update a gRPC service
|
### Update the gRPC service
|
||||||
|
|
||||||
Now let's look at how to update the application with an extra method on the
|
In this section, you'll update the app with an extra server method. The app's
|
||||||
server for the client to call. Our gRPC service is defined using [Protocol
|
gRPC service, named `Greeter`, is defined using [protocol buffers][pb]. To learn
|
||||||
Buffers][pb]; you can find out lots more about how to define a service in a
|
more about how to define a service in a `.proto` file, see [Basics tutorial][].
|
||||||
`.proto` file in [Basics tutorial](../basics/). For now
|
For now, all you need to know is that both the server and the client stub have a
|
||||||
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
|
`SayHello()` RPC method that takes a `HelloRequest` parameter from the client
|
||||||
and returns a `HelloReply` from the server, and that this method is defined like
|
and returns a `HelloReply` from the server, and that the method is defined like
|
||||||
this:
|
this:
|
||||||
|
|
||||||
|
|
||||||
```protobuf
|
```protobuf
|
||||||
// The greeting service definition.
|
// The greeting service definition.
|
||||||
service Greeter {
|
service Greeter {
|
||||||
|
|
@ -76,9 +80,10 @@ message HelloReply {
|
||||||
string message = 1;
|
string message = 1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Let's update this so that the `Greeter` service has two methods. Edit
|
|
||||||
`src/main/proto/hello_world.proto` and update it with a new `SayHelloAgain()`
|
Open `helloworld/hello_world.proto` from the
|
||||||
method, with the same request and response types:
|
[protos/src/main/proto/io/grpc/examples][protos-src] folder, and add a
|
||||||
|
new `SayHelloAgain()` method, with the same request and response types:
|
||||||
|
|
||||||
```protobuf
|
```protobuf
|
||||||
// The greeting service definition.
|
// The greeting service definition.
|
||||||
|
|
@ -102,21 +107,21 @@ message HelloReply {
|
||||||
|
|
||||||
Remember to save the file!
|
Remember to save the file!
|
||||||
|
|
||||||
### Update and run the application
|
### Update the app
|
||||||
|
|
||||||
When we recompile the example, normal compilation will regenerate
|
When you build the example, the build process regenerates `HelloWorldGrpcKt.kt`,
|
||||||
`HelloWorldGrpcKt.kt`, which contains our generated gRPC client and server classes.
|
which contains the generated gRPC client and server classes. This also
|
||||||
This also regenerates classes for populating, serializing, and retrieving our
|
regenerates classes for populating, serializing, and retrieving our request and
|
||||||
request and response types.
|
response types.
|
||||||
|
|
||||||
However, we still need to implement and call the new method in the human-written
|
However, you still need to implement and call the new method in the
|
||||||
parts of our example application.
|
hand-written parts of the example app.
|
||||||
|
|
||||||
#### Update the server
|
#### Update the server
|
||||||
|
|
||||||
In the same directory, open
|
Open `helloworld/HelloWorldServer.kt` from the
|
||||||
`src/main/kotlin/io/grpc/examples/helloworld/HelloWorldServer.kt`. Implement the
|
[server/src/main/kotlin/io/grpc/examples][server-src] folder. Implement the new
|
||||||
new method like this:
|
method like this:
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
|
private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
|
||||||
|
|
@ -134,22 +139,22 @@ private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
|
||||||
|
|
||||||
#### Update the client
|
#### Update the client
|
||||||
|
|
||||||
In the same directory, open
|
Open `helloworld/HelloWorldClient.kt` from the
|
||||||
`src/main/kotlin/io/grpc/examples/helloworld/HelloWorldClient.kt`. Call the new
|
[client/src/main/kotlin/io/grpc/examples][client-src] folder. Call the new
|
||||||
method like this:
|
method like this:
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
class HelloWorldClient constructor(
|
class HelloWorldClient(
|
||||||
private val channel: ManagedChannel
|
private val channel: ManagedChannel
|
||||||
) : Closeable {
|
) : Closeable {
|
||||||
private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)
|
private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)
|
||||||
|
|
||||||
suspend fun greet(name: String) = coroutineScope {
|
suspend fun greet(name: String) {
|
||||||
val request = HelloRequest.newBuilder().setName(name).build()
|
val request = HelloRequest.newBuilder().setName(name).build()
|
||||||
val response = async { stub.sayHello(request) }
|
val response = stub.sayHello(request)
|
||||||
println("Received: ${response.await().message}")
|
println("Received: ${response.message}")
|
||||||
val againResponse = async { stub.sayHelloAgain(request) }
|
val againResponse = stub.sayHelloAgain(request)
|
||||||
println("Received: ${againResponse.await().message}")
|
println("Received: ${againResponse.message}")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
|
|
@ -158,7 +163,7 @@ class HelloWorldClient constructor(
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Run!
|
### Run the updated app
|
||||||
|
|
||||||
Run the client and server like you did before. Execute the following commands
|
Run the client and server like you did before. Execute the following commands
|
||||||
from the `examples` directory:
|
from the `examples` directory:
|
||||||
|
|
@ -172,19 +177,15 @@ from the `examples` directory:
|
||||||
2. Run the server:
|
2. Run the server:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ ./build/install/examples/bin/hello-world-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
|
3. From another terminal, run the client. This time, add a name as a
|
||||||
command-line argument:
|
command-line argument:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ ./build/install/examples/bin/hello-world-client Alice
|
$ ./client/build/install/client/bin/hello-world-client Alice
|
||||||
```
|
|
||||||
|
|
||||||
You'll see the following output:
|
|
||||||
|
|
||||||
```nocode
|
|
||||||
Received: Hello Alice
|
Received: Hello Alice
|
||||||
Received: Hello again Alice
|
Received: Hello again Alice
|
||||||
```
|
```
|
||||||
|
|
@ -193,8 +194,14 @@ from the `examples` directory:
|
||||||
|
|
||||||
- Learn how gRPC works in [Introduction to gRPC](/docs/what-is-grpc/introduction/)
|
- Learn how gRPC works in [Introduction to gRPC](/docs/what-is-grpc/introduction/)
|
||||||
and [Core concepts](/docs/what-is-grpc/core-concepts/).
|
and [Core concepts](/docs/what-is-grpc/core-concepts/).
|
||||||
- Work through the [Basics tutorial](../basics/).
|
- Work through the [Basics tutorial][].
|
||||||
- Explore the [API reference](../api).
|
- Explore the [API reference](../api).
|
||||||
|
|
||||||
|
[Basics tutorial]: ../basics/
|
||||||
|
[download]: https://github.com/grpc/grpc-kotlin/archive/master.zip
|
||||||
|
[grpc-kotlin]: https://github.com/grpc/grpc-kotlin
|
||||||
[Kotlin]: https://kotlinlang.org
|
[Kotlin]: https://kotlinlang.org
|
||||||
[pb]: https://developers.google.com/protocol-buffers
|
[pb]: https://developers.google.com/protocol-buffers
|
||||||
|
[client-src]: https://github.com/grpc/grpc-kotlin/tree/master/examples/client/src/main/kotlin/io/grpc/examples
|
||||||
|
[protos-src]: https://github.com/grpc/grpc-kotlin/tree/master/examples/protos/src/main/proto/io/grpc/examples
|
||||||
|
[server-src]: https://github.com/grpc/grpc-kotlin/tree/master/examples/server/src/main/kotlin/io/grpc/examples
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue