mirror of https://github.com/grpc/grpc.io.git
parent
e551e5798e
commit
d9a53abd9e
|
|
@ -1,6 +1,7 @@
|
|||
---
|
||||
title: Quick start
|
||||
description: This guide gets you started with gRPC in Kotlin with a simple working example.
|
||||
spelling: cSpell:ignore println
|
||||
weight: 10
|
||||
---
|
||||
|
||||
|
|
@ -9,21 +10,24 @@ weight: 10
|
|||
- [Kotlin][] version 1.3 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.
|
||||
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
|
||||
and other tutorials):
|
||||
The example code is part of the [grpc-kotlin][] repo.
|
||||
|
||||
1. [Download the repo as a zip file][download] and unzip it, or clone
|
||||
the repo:
|
||||
|
||||
```sh
|
||||
# Clone the repository at the latest release to get the example code:
|
||||
$ git clone https://github.com/grpc/grpc-kotlin
|
||||
# Navigate to the examples:
|
||||
```
|
||||
|
||||
2. Change to the examples directory:
|
||||
|
||||
```sh
|
||||
$ cd grpc-kotlin/examples
|
||||
```
|
||||
|
||||
### Run a gRPC application
|
||||
### Run the example
|
||||
|
||||
From the `examples` directory:
|
||||
|
||||
|
|
@ -36,29 +40,29 @@ From the `examples` directory:
|
|||
2. Run the server:
|
||||
|
||||
```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:
|
||||
|
||||
```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
|
||||
server for the client to call. Our gRPC service is defined using [Protocol
|
||||
Buffers][pb]; you can find out lots more about how to define a service in a
|
||||
`.proto` file in [Basics tutorial](../basics/). For now
|
||||
all you need to know is that both the server and the client "stub" have a
|
||||
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][pb]. 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 this method is defined like
|
||||
and returns a `HelloReply` from the server, and that the method is defined like
|
||||
this:
|
||||
|
||||
|
||||
```protobuf
|
||||
// The greeting service definition.
|
||||
service Greeter {
|
||||
|
|
@ -76,9 +80,10 @@ message HelloReply {
|
|||
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()`
|
||||
method, with the same request and response types:
|
||||
|
||||
Open `helloworld/hello_world.proto` from the
|
||||
[protos/src/main/proto/io/grpc/examples][protos-src] folder, and add a
|
||||
new `SayHelloAgain()` method, with the same request and response types:
|
||||
|
||||
```protobuf
|
||||
// The greeting service definition.
|
||||
|
|
@ -102,21 +107,21 @@ message HelloReply {
|
|||
|
||||
Remember to save the file!
|
||||
|
||||
### Update and run the application
|
||||
### Update the app
|
||||
|
||||
When we recompile the example, normal compilation will regenerate
|
||||
`HelloWorldGrpcKt.kt`, which contains our generated gRPC client and server classes.
|
||||
This also regenerates classes for populating, serializing, and retrieving our
|
||||
request and response types.
|
||||
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, we still need to implement and call the new method in the human-written
|
||||
parts of our example application.
|
||||
However, you still need to implement and call the new method in the
|
||||
hand-written parts of the example app.
|
||||
|
||||
#### Update the server
|
||||
|
||||
In the same directory, open
|
||||
`src/main/kotlin/io/grpc/examples/helloworld/HelloWorldServer.kt`. Implement the
|
||||
new method like this:
|
||||
Open `helloworld/HelloWorldServer.kt` from the
|
||||
[server/src/main/kotlin/io/grpc/examples][server-src] folder. Implement the new
|
||||
method like this:
|
||||
|
||||
```kotlin
|
||||
private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
|
||||
|
|
@ -134,22 +139,22 @@ private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
|
|||
|
||||
#### Update the client
|
||||
|
||||
In the same directory, open
|
||||
`src/main/kotlin/io/grpc/examples/helloworld/HelloWorldClient.kt`. Call the new
|
||||
Open `helloworld/HelloWorldClient.kt` from the
|
||||
[client/src/main/kotlin/io/grpc/examples][client-src] folder. Call the new
|
||||
method like this:
|
||||
|
||||
```kotlin
|
||||
class HelloWorldClient constructor(
|
||||
class HelloWorldClient(
|
||||
private val channel: ManagedChannel
|
||||
) : Closeable {
|
||||
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 response = async { stub.sayHello(request) }
|
||||
println("Received: ${response.await().message}")
|
||||
val againResponse = async { stub.sayHelloAgain(request) }
|
||||
println("Received: ${againResponse.await().message}")
|
||||
val response = stub.sayHello(request)
|
||||
println("Received: ${response.message}")
|
||||
val againResponse = stub.sayHelloAgain(request)
|
||||
println("Received: ${againResponse.message}")
|
||||
}
|
||||
|
||||
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
|
||||
from the `examples` directory:
|
||||
|
|
@ -172,19 +177,15 @@ from the `examples` directory:
|
|||
2. Run the server:
|
||||
|
||||
```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
|
||||
command-line argument:
|
||||
|
||||
```sh
|
||||
$ ./build/install/examples/bin/hello-world-client Alice
|
||||
```
|
||||
|
||||
You'll see the following output:
|
||||
|
||||
```nocode
|
||||
$ ./client/build/install/client/bin/hello-world-client Alice
|
||||
Received: Hello 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/)
|
||||
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).
|
||||
|
||||
[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
|
||||
[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