|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Getting started with the Dapr client Rust SDK" |
| 4 | +linkTitle: "Client" |
| 5 | +weight: 20000 |
| 6 | +description: How to get up and running with the Dapr Rust SDK |
| 7 | +no_list: true |
| 8 | +--- |
| 9 | + |
| 10 | +The Dapr client package allows you to interact with other Dapr applications from |
| 11 | +a Rust application. |
| 12 | + |
| 13 | +{{% alert title="Note" color="primary" %}} |
| 14 | +The Dapr Rust-SDK is currently in Alpha. Work is underway to bring it to a |
| 15 | +stable release and will likely involve breaking changes. |
| 16 | +{{% /alert %}} |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- [Dapr CLI]({{% ref install-dapr-cli.md %}}) installed |
| 21 | +- Initialized [Dapr environment]({{% ref install-dapr-selfhost.md %}}) |
| 22 | +- [Rust installed](https://www.rust-lang.org/tools/install) |
| 23 | + |
| 24 | +## Import the client package |
| 25 | + |
| 26 | +Add Dapr to your `cargo.toml` |
| 27 | + |
| 28 | +```toml |
| 29 | +[dependencies] |
| 30 | +dapr = "0.16" |
| 31 | +``` |
| 32 | + |
| 33 | +You can either reference `dapr::Client` or bind the full path to a new name as follows: |
| 34 | + |
| 35 | +```rust |
| 36 | +use dapr::Client as DaprClient; |
| 37 | +``` |
| 38 | + |
| 39 | +## Instantiating the Dapr client |
| 40 | + |
| 41 | +```rust |
| 42 | +let addr = "https://127.0.0.1".to_string(); |
| 43 | + |
| 44 | +let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr, |
| 45 | +port).await?; |
| 46 | +``` |
| 47 | + |
| 48 | +Alternatively if you would like to specify a custom port, this can be done by using this connect method: |
| 49 | + |
| 50 | +```rust |
| 51 | +let mut client = dapr::Client::<dapr::client::TonicClient>::connect_with_port(addr, "3500".to_string()).await?; |
| 52 | +``` |
| 53 | + |
| 54 | +## Building blocks |
| 55 | + |
| 56 | +The Rust SDK allows you to interface with the |
| 57 | +[Dapr building blocks]({{% ref building-blocks %}}). |
| 58 | + |
| 59 | +### Service Invocation (gRPC) |
| 60 | + |
| 61 | +To invoke a specific method on another service running with Dapr sidecar, the |
| 62 | +Dapr client provides two options: |
| 63 | + |
| 64 | +Invoke a (gRPC) service |
| 65 | + |
| 66 | +```rust |
| 67 | +let response = client |
| 68 | + .invoke_service("service-to-invoke", "method-to-invoke", Some(data)) |
| 69 | + .await |
| 70 | + .unwrap(); |
| 71 | +``` |
| 72 | + |
| 73 | +For a full guide on service invocation, visit |
| 74 | +[How-To: Invoke a service]({{% ref howto-invoke-discover-services.md %}}). |
| 75 | + |
| 76 | +### State Management |
| 77 | + |
| 78 | +The Dapr Client provides access to these state management methods: `save_state` |
| 79 | +, `get_state`, `delete_state` that can be used like so: |
| 80 | + |
| 81 | +```rust |
| 82 | +let store_name = String::from("statestore"); |
| 83 | + |
| 84 | +let key = String::from("hello"); |
| 85 | +let val = String::from("world").into_bytes(); |
| 86 | + |
| 87 | +// save key-value pair in the state store |
| 88 | +client |
| 89 | + .save_state(store_name, key, val, None, None, None) |
| 90 | + .await?; |
| 91 | + |
| 92 | +let get_response = client |
| 93 | + .get_state("statestore", "hello", None) |
| 94 | + .await?; |
| 95 | + |
| 96 | +// delete a value from the state store |
| 97 | +client |
| 98 | + .delete_state("statestore", "hello", None) |
| 99 | + .await?; |
| 100 | +``` |
| 101 | + |
| 102 | +Multiple states can be sent with the `save_bulk_states` method. |
| 103 | + |
| 104 | +For a full guide on state management, visit |
| 105 | +[How-To: Save & get state]({{% ref howto-get-save-state.md %}}). |
| 106 | + |
| 107 | +### Publish Messages |
| 108 | + |
| 109 | +To publish data onto a topic, the Dapr client provides a simple method: |
| 110 | + |
| 111 | +```rust |
| 112 | +let pubsub_name = "pubsub-name".to_string(); |
| 113 | +let pubsub_topic = "topic-name".to_string(); |
| 114 | +let pubsub_content_type = "text/plain".to_string(); |
| 115 | + |
| 116 | +let data = "content".to_string().into_bytes(); |
| 117 | +client |
| 118 | + .publish_event(pubsub_name, pubsub_topic, pubsub_content_type, data, None) |
| 119 | + .await?; |
| 120 | +``` |
| 121 | + |
| 122 | +For a full guide on pub/sub, visit |
| 123 | +[How-To: Publish & subscribe]({{% ref howto-publish-subscribe.md %}}). |
| 124 | + |
| 125 | +## Related links |
| 126 | + |
| 127 | +[Rust SDK Examples](https://github.com/dapr/rust-sdk/tree/master/examples) |
0 commit comments