consul-rs-plus

A plus consul client package for Rust, more functions for microservice.

install

set in Cargo dependencies toml [dependencies] consul-rs-plus = "0.1.7"

Usage

```rust extern crate consulrsplus; use consulrsplus::Client;

fn main() { let mut c = Client::new("localhost", 8500); // debug enable c.debug = true;

let ok = c.kv_set("test-key", "test_value").unwrap();
assert_eq!(ok, true);

let kvpairs = c.kv_get("test-key").unwrap();
let kvpair = &kvpairs[0];
let v = kvpair.get_value().unwrap();
assert_eq!(b"test_value"[..].to_vec(), v);

let ok = c.kv_delete("test-key").unwrap();
assert_eq!(ok, true);

} ```

Test

the test case write in function code file or tests folder, all nromal operation test in lib.rs. ```rust

[cfg(test)]

mod tests { use crate::Client; use base64::Config; use crate::config;

#[test] fn testkvget() { let host = config::CONFIG["consuladdr"]; let client = Client::new(host, 8500); let mykeys = client.kvget("my-key").unwrap(); for k in mykeys { println!("k: {:?}", k); } }

} ```

Micro service nodes watch

if you write the micro service framework, the service register like rpcx-plus, folder tree style, for example, Echo service in folder(key, value) is /mytest/Echo/tcp@8.8.8.8:999, you can find the sample code for watch service change below, they are in code repo. you can cache the service info when the service change(the folder tree change). ```rust

[tokio::test]

async fn testwatchfoldertreetmpsc() { envlogger::init(); let folder = "mytest".tostring(); let mut nodesservice: Vec = Vec::new(); // service cache let (sx, mut rx) = tmpsc::channel(1); let kv = KVPair::new(); let client = Client::new("consultest", 8500); let mut index = kv.getfolderindex(&client, &folder); log::info!("index orgin ------- {}", index); tokio::task::spawn(async move { loop { thread::sleep(time::Duration::fromsecs(5)); let mut indexck = kv.getfolderindex(&client, &folder); log::info!("indexck ------- {}", indexck); if !indexck.eq(index.asstr()) { log::info!("=== get newest nodes service, send coroutine ==="); let nodesv = kv.getfolderallkeys(&client, &folder); let nodesvcl = nodesv.clone(); log::info!("[send] === in spawn nodesvcl: {:?}", nodesvcl); sx.send(nodesvcl).await.unwrap(); // todo: just make the channel full! index = indexck; } else { log::info!("=== nodesservice no change ==="); } } }); }

``` you can run this test case in code repo.