-
Notifications
You must be signed in to change notification settings - Fork 14
/
types.go
90 lines (73 loc) · 2.01 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package discovery
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
zfield "github.com/tkeel-io/core/pkg/logger"
"github.com/tkeel-io/kit/log"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
)
const (
GrpcDiscoveryPrefix = "service://"
)
type Config struct {
Endpoints []string
DialTimeout int64
HeartTime int64
}
type Service struct {
AppID string `json:"app_id"`
Name string `json:"name"`
Host string `json:"host"`
Port int `json:"port"`
Metadata map[string]string `json:"metadata"`
}
func (s Service) Key() string {
return fmt.Sprintf("%s%s/%s", GrpcDiscoveryPrefix, s.AppID, s.Name)
}
func (s Service) Value() string {
bytes, _ := json.Marshal(s)
return string(bytes)
}
func (s Service) WatchKey() string {
return fmt.Sprintf("%s%s", GrpcDiscoveryPrefix, s.AppID)
}
type Register interface {
Register(ctx context.Context, node Service) error
}
var (
PUT EnventType = EnventType(mvccpb.PUT)
DELETE EnventType = EnventType(mvccpb.DELETE)
)
type EnventType mvccpb.Event_EventType
type ResolveHandler func(EnventType, Service)
type Resolver interface {
Resolve(ctx context.Context, handlers []ResolveHandler) error
}
type Discovery struct {
discoveryEnd *clientv3.Client
HeartTime int64
Config Config
}
func New(cfg Config) (*Discovery, error) {
log.Info("connect on discovery cluster",
zfield.Endpoints(cfg.Endpoints), zap.Int64("dial_timeout", cfg.DialTimeout))
discoveryEnd, err := clientv3.New(clientv3.Config{
Endpoints: cfg.Endpoints,
DialTimeout: time.Duration(cfg.DialTimeout) * time.Second,
})
if nil != err {
log.Error("connect on discovery cluster", zap.Error(err),
zfield.Endpoints(cfg.Endpoints), zap.Int64("dial_timeout", cfg.DialTimeout))
return nil, errors.Wrap(err, "connect Discovery Endpoint")
}
return &Discovery{
Config: cfg,
discoveryEnd: discoveryEnd,
HeartTime: cfg.HeartTime * int64(time.Second),
}, nil
}