一个简单粗暴的ETCD V3版本KV功能的client
发布于 5 年前 作者 scarletmu 8119 次浏览 来自 分享

Github NPM

Intro

公司项目要用ETCD,看了看轮子好像只有V2版本的API,然后现在官方V3的文档不是很完善,能看到可以用restful做的好像只有KV这部分的功能。 所以先简单自己裹了个轮子,之后等官方正式release了再更进 也许有的朋友不知道 ETCD,它简单地说也是一个类似Redis的key-value库,但更多的设计为面向分布式

Install && Init

npm install etcd4js

Usage

Now Txn() is still in progress.
V3 Api 需要base64过的key/value以及range_end,本库会自动转换.

const etcd4js = require('etcd4js')
let store = new etcd4js('127.0.0.1:2379')

let key = 'hello';
let value = 'world';

//Use Default Option
store.Put(key, value)
.then((res) => {
  //result
})
.catch((err) => {
  //error
})

//Use Personal Option
store.Range(key, {count_only: true})
.then((res) => {
  //Only Return Count Result
})
.catch((err) => {
  //Error
})

API

.Put(key, [opt])

Offical Doc: Put puts the given key into the key-value store. A put request increments the revision of the key-value store and generates one event in the event history.

Default option value

{
  lease: 0,
  prev_kv: false
}

.Range(key, value, [opt])

Offical Doc: Range gets the keys in the range from the key-value store.

Default option value

{
  range_end:  //like your key,,
  limit: 0,
  revision: 0,
  seriliazable: false,
  keys_only: false,
  count_only: false,
  raw: false //Personal Option, Default false means that will auto decrypt base64 key/value in return
}

.DeleteRange(key, [opt])

Offical Doc: DeleteRange deletes the given range from the key-value store. A delete request increments the revision of the key-value store and generates a delete event in the event history for every deleted key.

Default option value

{
  range_end: //like your key
  prev_kv: false
}

.Compact(revision, [opt])

Offical Doc: Compact compacts the event history in the etcd key-value store. The key-value store should be periodically compacted or the event history will continue to grow indefinitely.

Default option value

{
  physical: false
}
2 回复

你们公司node 一般做啥业务呢

你应该加一点配置管理的内容,不然直说和redis类似会害死人的,哈哈

回到顶部