Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): Add routeguide tutorial #21

Merged
merged 35 commits into from
Oct 18, 2019
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
28e66ef
started routeguide tutorial
alce Sep 30, 2019
2459ff6
remove original go code
alce Sep 30, 2019
4d7f143
Apply suggestions from code review
alce Oct 1, 2019
87d5ef3
update urls, make capitalization consistent
alce Oct 1, 2019
7007797
Merge remote-tracking branch 'upstream/master' into tutorial
alce Oct 1, 2019
d11d7b6
pin all alpha dependencies
alce Oct 1, 2019
cb84a13
Merge branch 'master' of github.com:LucioFranco/tonic
alce Oct 1, 2019
824043e
Merge branch 'master' into tutorial
alce Oct 1, 2019
2883d80
some method implementations
alce Oct 1, 2019
ea649a6
spurious commit, testing CI
alce Oct 1, 2019
e3ea43c
spurious 2
alce Oct 1, 2019
481c3a9
Merge branch 'master' of github.com:hyperium/tonic
alce Oct 2, 2019
0d676f4
Merge branch 'master' into tutorial
alce Oct 2, 2019
d6d2efd
server state
alce Oct 2, 2019
14a4e13
delete repeted lines, wording
alce Oct 2, 2019
bbb8561
improve route_chat, record_route and list_features
alce Oct 2, 2019
a000abd
a little love for the client
alce Oct 2, 2019
e617bef
cleanup stubbed content
alce Oct 2, 2019
1e47dfc
simplify loading features from json file
alce Oct 2, 2019
c3d598c
server section should be ready for review
alce Oct 2, 2019
12bf715
minor tweaks, wording
alce Oct 3, 2019
5e6d717
client setup
alce Oct 3, 2019
b018273
example: implement client list_features
alce Oct 3, 2019
1ad36b1
tweaks to server, a bit more client
alce Oct 5, 2019
6979497
typo
alce Oct 5, 2019
ea666ff
tutorial ready for review
alce Oct 5, 2019
6a452ca
typo
alce Oct 5, 2019
920426f
server section tweaks
alce Oct 6, 2019
460e82a
client tweaks
alce Oct 6, 2019
e0d558b
include server side `use`
alce Oct 6, 2019
f8a976b
upgrade tonic to alpha.2
alce Oct 8, 2019
193c35a
move notes map out of shared state
alce Oct 13, 2019
ddf33da
address blgBV's comments
alce Oct 14, 2019
e03408a
Merge remote-tracking branch 'origin/master' into tutorial
LucioFranco Oct 18, 2019
bf7428c
Update route guide tutorial to track latest master
LucioFranco Oct 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
client setup
  • Loading branch information
alce committed Oct 3, 2019
commit 5e6d717b5d4680691e0d288d6395c0ab0f901c88
53 changes: 42 additions & 11 deletions tonic-examples/routeguide-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,27 +556,53 @@ for example, add an [interceptor][authentication-example] or implement [routing]
<a name="client"></a>
## Creating the client

In this section, we'll look at creating a Rust client for our `RouteGuide` service. You can see our
complete example client code in [tonic-examples/src/routeguide/client.rs][routeguide-client]
In this section, we'll look at creating a Tonic client for our `RouteGuide` service. You can see our
complete example client code in [tonic-examples/src/routeguide/client.rs][routeguide-client].

Our crate will have two binary targets: `routeguide-client` and `routeguide-server`. We need to
edit our `Cargo.toml` accordingly:

[routeguide-client]: https://github.com/hyperium/tonic/blob/master/tonic-examples/src/routeguide/client.rs
```toml
[[bin]]
name = "routeguide-server"
path = "src/server.rs"

[[bin]]
name = "routeguide-client"
path = "src/client.rs"
```

Next, rename `main.rs` to `server.rs` and create a new file `client.rs`.

### Creating a client
```shell
$ mv src/main.rs src/server.rs
$ touch src/client.rs
```

To call service methods, we first need to create a gRPC *client* to communicate with the server.
Creating a client is as simple as:

```rust
let mut client = RouteGuideClient::connect("http://[::1]:10000")?;
pub mod route_guide {
tonic::include_proto!("routeguide");
}

use route_guide::client::RouteGuideClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = RouteGuideClient::connect("http://[::1]:10000")?;
}
```

[routeguide-client]: https://github.com/hyperium/tonic/blob/master/tonic-examples/src/routeguide/client.rs


### Calling service methods
Now let's look at how we call our service methods. Note that in Tonic, RPCs are asynchronous,
which means that the RPC call needs to be awaited.
which means that the RPC call needs to be `awaited`.

#### Simple RPC
Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local method.
Calling the simple RPC `GetFeature` is as straightforward as calling a local method.

```rust
let response = client
Expand All @@ -586,9 +612,7 @@ let response = client
}))
.await?;
```
As you can see, we call the method on the client we got earlier. In our method parameters we create
and populate a request protocol buffer object (in our case `Point`), and wrap it in a
`tonic::Request`
We call the `get_feature` client, passing a `Point` value wrapped in a `tonic::Request`.

#### Server-side streaming RPC

Expand All @@ -597,9 +621,16 @@ and populate a request protocol buffer object (in our case `Point`), and wrap it
## Try it out!

### Run the server
```shell
$ cargo run --bin routeguide-server
```

### Run the client
```shell
$ cargo run --bin routeguide-client
```

## Appendix
### tonic-build configuration
### Well Known Types