-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
276 additions
and
42 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
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
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,11 @@ | ||
package etcd | ||
|
||
import "crypto/tls" | ||
|
||
type Conf struct { | ||
Endpoints []string `yaml:"endpoints" toml:"endpoints" json:"endpoints"` //地址列表,如: localhost:2379 | ||
Username string `yaml:"username" toml:"username" json:"username"` //账号 | ||
Password string `yaml:"password" toml:"password" json:"password"` //密码 | ||
Timeout int `yaml:"timeout" toml:"timeout" json:"timeout"` //连接超时时间(毫秒)默认10000ms | ||
Tls *tls.Config `yaml:"-" toml:"-" json:"-"` //tls配置 | ||
} |
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,65 @@ | ||
//@doc https://github.com/etcd-io/etcd/tree/main/client/v3 | ||
|
||
package etcd | ||
|
||
import ( | ||
"time" | ||
|
||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
var client *clientv3.Client | ||
|
||
// Init 初始化连接实例 | ||
func Init(conf Conf) { | ||
if conf.Timeout == 0 { | ||
conf.Timeout = 10000 | ||
} | ||
|
||
cfg := clientv3.Config{ | ||
Endpoints: conf.Endpoints, | ||
DialTimeout: time.Duration(conf.Timeout) * time.Millisecond, | ||
} | ||
|
||
if conf.Tls != nil { | ||
cfg.TLS = conf.Tls | ||
} | ||
if len(conf.Username) != 0 && len(conf.Password) != 0 { | ||
cfg.Username = conf.Username | ||
cfg.Password = conf.Password | ||
} | ||
|
||
cli, err := clientv3.New(cfg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
client = cli | ||
} | ||
|
||
// GetInstance 获取连接实例 | ||
func GetInstance() *clientv3.Client { | ||
return client | ||
} | ||
|
||
// New 新建连接实例 | ||
func New(conf Conf) (*clientv3.Client, error) { | ||
if conf.Timeout == 0 { | ||
conf.Timeout = 10000 | ||
} | ||
|
||
cfg := clientv3.Config{ | ||
Endpoints: conf.Endpoints, | ||
DialTimeout: time.Duration(conf.Timeout) * time.Millisecond, | ||
} | ||
|
||
if conf.Tls != nil { | ||
cfg.TLS = conf.Tls | ||
} | ||
if len(conf.Username) != 0 && len(conf.Password) != 0 { | ||
cfg.Username = conf.Username | ||
cfg.Password = conf.Password | ||
} | ||
|
||
return clientv3.New(cfg) | ||
} |
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,27 @@ | ||
package etcd | ||
|
||
import ( | ||
"context" | ||
|
||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
// Watch 监听key | ||
func Watch(key string, fn func(k, v []byte)) { | ||
watchChan := GetInstance().Watch(context.Background(), key) | ||
for watchResp := range watchChan { | ||
for _, value := range watchResp.Events { | ||
fn(value.Kv.Key, value.Kv.Value) | ||
} | ||
} | ||
} | ||
|
||
// WatchWithPrefix 监听key,带前缀 | ||
func WatchWithPrefix(key string, fn func(k, v []byte)) { | ||
watchChan := GetInstance().Watch(context.Background(), key, clientv3.WithPrefix()) | ||
for watchResp := range watchChan { | ||
for _, value := range watchResp.Events { | ||
fn(value.Kv.Key, value.Kv.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
Oops, something went wrong.