40 lines
961 B
Go
40 lines
961 B
Go
|
|
package gfetcd
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"path"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"github.com/coreos/etcd/clientv3"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
DEFAULT_DIAL_TIMEOUT = time.Second * 5
|
|||
|
|
DEFAULT_LEASE_TTL = 5
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 基础配置,公共字段
|
|||
|
|
type BasicConfig struct {
|
|||
|
|
DialTimeout int `toml:"dial_timeout"` // 连接超时时间
|
|||
|
|
EndPoints []string `toml:"endpoints"` // etcd的地址
|
|||
|
|
Prefix string `toml:"prefix"` // etcd服务的路径前缀,区分环境用
|
|||
|
|
Debug bool `toml:"debug"` // 使用debug模式
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func etcdDial(cfg *BasicConfig) (*clientv3.Client, error) {
|
|||
|
|
dialTimeout := DEFAULT_DIAL_TIMEOUT
|
|||
|
|
if cfg.DialTimeout > 0 {
|
|||
|
|
dialTimeout = time.Duration(cfg.DialTimeout) * time.Second
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return clientv3.New(clientv3.Config{
|
|||
|
|
Endpoints: cfg.EndPoints,
|
|||
|
|
DialTimeout: dialTimeout,
|
|||
|
|
DialKeepAliveTime: 2 * time.Second,
|
|||
|
|
DialKeepAliveTimeout: 2 * time.Second,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func ServiceKey(opts ...string) string {
|
|||
|
|
return path.Join(opts...)
|
|||
|
|
}
|