forked from dapr/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add configuration example Signed-off-by: Hauke Jung <hauke.jung@outlook.de> * Set tokio-stream as dev dependency Signed-off-by: Hauke Jung <hauke.jung@outlook.de> * cargo fmt Signed-off-by: Hauke Jung <hauke.jung@outlook.de> --------- Signed-off-by: Hauke Jung <hauke.jung@outlook.de>
- Loading branch information
Showing
5 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: configstore | ||
spec: | ||
type: configuration.redis | ||
version: v1 | ||
metadata: | ||
- name: redisHost | ||
value: localhost:6379 | ||
- name: redisPassword | ||
value: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Before you run the example make sure local redis state store is running by executing: | ||
```bash | ||
docker ps | ||
``` | ||
|
||
1. To run the example we need to first build the examples using the following command: | ||
|
||
```bash | ||
cargo build --examples | ||
``` | ||
|
||
2. Run the example with dapr using the following command: | ||
|
||
```bash | ||
dapr run --app-id=rustapp --resources-path ./examples/components --dapr-grpc-port 3500 -- cargo run --example configuration | ||
``` | ||
|
||
3. Change the value of the key `hello` in redis using the following command: | ||
|
||
```bash | ||
docker exec dapr_redis redis-cli MSET hello "world" | ||
``` | ||
|
||
|
||
If everything went well you should see the following output along with dapr logs: | ||
``` | ||
Configuration value: ConfigurationItem { value: "world", version: "", metadata: {} } | ||
App subscribed to config changes with subscription id: "d383169a-0893-4c64-adde-fc3145b56d07" | ||
Configuration value: {"hello": ConfigurationItem { value: "world", version: "", metadata: {} }} | ||
App unsubscribed from config changes | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use tokio_stream::StreamExt; | ||
|
||
const CONFIGSTORE_NAME: &str = "configstore"; | ||
type DaprClient = dapr::Client<dapr::client::TonicClient>; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// TODO: Handle this issue in the sdk | ||
// Introduce delay so that dapr grpc port is assigned before app tries to connect | ||
std::thread::sleep(std::time::Duration::new(2, 0)); | ||
|
||
// Get the Dapr port and create a connection | ||
let port: u16 = std::env::var("DAPR_GRPC_PORT")?.parse()?; | ||
let addr = format!("https://127.0.0.1:{}", port); | ||
|
||
// Create the client | ||
let mut client = DaprClient::connect(addr).await?; | ||
|
||
let key = String::from("hello"); | ||
|
||
// save key-value pair in the state store | ||
let response = client | ||
.get_configuration(CONFIGSTORE_NAME, vec![(&key)]) | ||
.await?; | ||
let val = response.items.get("hello").unwrap(); | ||
println!("Configuration value: {val:?}"); | ||
|
||
// Subscribe for configuration changes | ||
let mut stream = client | ||
.subscribe_configuration(CONFIGSTORE_NAME, vec![(&key)]) | ||
.await?; | ||
|
||
let mut subscription_id = String::new(); | ||
while let Some(result) = stream.next().await { | ||
let subscribe = result.unwrap(); | ||
if subscribe.items.is_empty() { | ||
// Update the subscription_id | ||
subscription_id = subscribe.id.clone(); | ||
println!("App subscribed to config changes with subscription id: {subscription_id:?} "); | ||
continue; | ||
} | ||
println!("Configuration value: {:?}", subscribe.items); | ||
unsubscribe(&mut client, &subscription_id).await; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
// Function to unsubscribe from configuration updates and exit the app | ||
async fn unsubscribe(client: &mut DaprClient, subscription_id: &str) { | ||
match client | ||
.unsubscribe_configuration("CONFIGSTORE_NAME", subscription_id) | ||
.await | ||
{ | ||
Ok(_) => println!("App unsubscribed from config changes"), | ||
Err(e) => println!("Error unsubscribing from config updates: {}", e), | ||
} | ||
std::process::exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters