-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar.go
51 lines (45 loc) · 1.38 KB
/
registrar.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
package zk
import "github.com/tnnyio/log"
// Registrar registers service instance liveness information to ZooKeeper.
type Registrar struct {
client Client
service Service
logger log.Logger
}
// Service holds the root path, service name and instance identifying data you
// want to publish to ZooKeeper.
type Service struct {
Path string // discovery namespace, example: /myorganization/myplatform/
Name string // service name, example: addscv
Data []byte // instance data to store for discovery, example: 10.0.2.10:80
node string // Client will record the ephemeral node name so we can deregister
}
// NewRegistrar returns a ZooKeeper Registrar acting on the provided catalog
// registration.
func NewRegistrar(client Client, service Service, logger log.Logger) *Registrar {
return &Registrar{
client: client,
service: service,
logger: log.With(logger,
"service", service.Name,
"path", service.Path,
"data", string(service.Data),
),
}
}
// Register implements sd.Registrar interface.
func (r *Registrar) Register() {
if err := r.client.Register(&r.service); err != nil {
r.logger.Log("err", err)
} else {
r.logger.Log("action", "register")
}
}
// Deregister implements sd.Registrar interface.
func (r *Registrar) Deregister() {
if err := r.client.Deregister(&r.service); err != nil {
r.logger.Log("err", err)
} else {
r.logger.Log("action", "deregister")
}
}