8 个版本
0.1.8 | 2020年4月14日 |
---|---|
0.1.7 | 2020年4月13日 |
0.1.3 | 2020年2月28日 |
0.1.1 | 2020年1月24日 |
#4 in #consul
每月33次下载
60KB
1K SLoC
rsoffkv
此库旨在为 3 种不同的分布式 KV 存储提供统一接口:etcd、Zookeeper、Consul。
Rsoffkv 是我们 C++ 库 liboffkv 的封装。设计细节可以在 liboffkv 仓库中找到。
构建
- 安装 vcpkg 并设置
VCPKG_ROOT
- 安装依赖项(您可以为支持的某些 KV 存储构建 rsoffkv;在这种情况下,您可以在构建脚本中更改
ENABLE_
的值)
vcpkg install ppconsul offscale-libetcd-cpp zkpp
- 使用 cargo 构建
cargo build
- (可选) 运行文档测试
cargo test
示例
use rsoffkv::client::Client;
use rsoffkv::result::OffkvError;
use rsoffkv::txn::{Transaction, TxnCheck, TxnOp, TxnOpResult};
use std::{thread,time};
fn main() {
// firstly specify service {zk | consul | etcd} and its address
// you can also specify a prefix all keys will start with
let client = Client::new("consul://127.0.0.1:8500", "/prefix").unwrap();
// Each method returns std::Result
match client.create("/key", "value", false) {
Ok(initial_version) =>
println!("Key \"/prefix/key\" successfully created. Initial version: {}",
initial_version),
Err(OffkvError::EntryExists) =>
println!("Error: key \"/prefix/key\" already exists!"),
};
// WATCH EXAMPLE
let (result, watch_handle) = client.exists("/key", true).unwrap();
thread::spawn(|| {
let another_client = Client::new("consul://127.0.0.1:8500", "/prefix", false).unwrap();
thread::sleep(time::Duration::from_secs(5));
another_client.erase("/key", 0).unwrap();
});
// now the key exists
assert!(result);
// wait for changes
watch_handle.wait();
// if the waiting was completed, the existence state must be different
let (result, _) = client.exists("/key", false).unwrap();
assert!(!result);
// TRANSACTION EXAMPLE
match client.commit(
// firstly list your checks
checks: vec![
TxnCheck{key: "/key", version: initial_version},
],
// then operations
ops: vec![
TxnOp::Create{key: "/key/child", value: "value", leased: false},
TxnOp::Set{key: "/key", value: "new value"},
],
) {
// on success a vector with changed version is returned
Ok(_) => println!("Success!"),
// on failure an index of the first failed operation is returned
Err(OffkvError::TxnFailed(failed_op)) => println!("Failed at {}", failed_op),
};
}
依赖项
~0.4–265KB