1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package node
- import (
- client "go.etcd.io/etcd/client/v3"
- "log"
- "time"
- )
- var (
- defaultEndpoints = []string{"http://127.0.0.1:2379"}
- defaultDialTimeout = 5
- )
- type Node struct {
- scheme string //
- ip string //节点ip
- Client *client.Client
- }
- // NewNode 快速创建节点
- func NewNode(endpoints ...string) *Node {
- if len(endpoints) == 0 {
- endpoints = defaultEndpoints
- }
- // etcd 配置
- defaultConfig := client.Config{
- Endpoints: endpoints,
- DialTimeout: time.Duration(defaultDialTimeout) * time.Second,
- }
- return NewNodeWithConfig(defaultConfig)
- }
- // NewNodeWithConfig 根据全量配置文件创建节点
- func NewNodeWithConfig(conf client.Config) *Node {
- // 创建 etcd 客户端
- etcdClient, err := client.New(conf)
- if err != nil {
- log.Fatal("Error: cannot connect to etcd: ", err)
- }
- return &Node{
- Client: etcdClient,
- scheme: defaultScheme,
- }
- }
- // SetScheme 设置节点注册、监听scheme
- // 禁止在node Register后 或 NewWatcher 后修改
- func (n *Node) SetScheme(scheme string) {
- n.scheme = scheme
- }
|