跳到主要内容

etcd_client

简介

基于pkg.etcdv3实现的etcd客户端

组件接口

Client.gs

etcd客户端对象

函数原型函数作用
object client()获取连接对象
object watch(string key, string key_end, function func, function succeed_callback = nil)监听key的值变化
object watch_prefix(string prefix, function func, function succeed_callback = nil)监听前缀的值变化
void cancel_watch(string key)取消监听
array lease_grant(int seconds, int id = 0)获取租约
array lease_revoke(int lease_id)取消租约
array lease_time_to_live(int lease_id)获取租约的信息
array get(string key)获取指定key的值
array get_range(string start_key, string end_key)获取一组key的值
array get_prefix(string prefix)获取指定前缀的值
array delete(string key)删除指定key的值
array delete_range(string start_key, string end_key)删除一组key的值
array delete_prefix(string prefix)删除指定前缀的值
array put(string key, mixed value, int lease_id = 0)设置指定key的值
array insert(string key, mixed value, int lease_id = 0)插入数据(如果key已存在,则操作失败)
array txn(array cmp_list, array then_list, array else_list = nil)事务操作
array multi_put(map kv_dict, int lease_id = 0)批量设置值
array multi_delete(array key_list)批量删除值
array multi_get(array key_list)批量获取值

etcd_client.gs

函数原型函数作用
object new_client(string host, int port = 2379)创建一个etcd客户端

Watcher.gs

处理监听的对象

函数原型函数作用
void watch()启动监听(创建独立线程)

样例

public object client()
{
if (_client_ob)
return _client_ob;;

object client_ob = etcd_client.new_client("127.0.0.1");
if (! client_ob)
return nil;

_client_ob = client_ob;
return _client_ob;
}

public void test()
{
function func = (WatchResponse resp) {
printf("WatchResponse: %O\n", resp);
};
function succeed_callback = (WatchResponse resp) {
printf("succeed_callback: %O\n", resp);
};
client().watch( "aaa", "aaa999", func, succeed_callback);
}

public void test1()
{
coroutine.create(nil, () {
//test();
});

coroutine.sleep(1);
string key = "x2";
client().insert(key, 1234);
printf("%s: new value: %M\n", key, _client_ob.get(key));
}

public void test2()
{
bool success;
let success, mixed lease_id = client().lease_grant(1000);
printf("1->success: %M, lease_id: %O\n", success, lease_id);

client().put("aaa", 123, lease_id);

let success, mixed result = client().lease_time_to_live(lease_id);
printf("2->success: %M, result: %O\n", success, result);
}