forked from gliderlabs/registrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
36 lines (30 loc) · 794 Bytes
/
etcd.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
package main
import (
"net"
"net/url"
"strconv"
"github.com/coreos/go-etcd/etcd"
)
type EtcdRegistry struct {
client *etcd.Client
path string
}
func NewEtcdRegistry(uri *url.URL) ServiceRegistry {
urls := make([]string, 0)
if uri.Host != "" {
urls = append(urls, "http://"+uri.Host)
}
return &EtcdRegistry{client: etcd.NewClient(urls), path: uri.Path}
}
func (r *EtcdRegistry) Register(service *Service) error {
path := r.path + "/" + service.Name + "/" + service.ID
port := strconv.Itoa(service.Port)
addr := net.JoinHostPort(service.IP, port)
_, err := r.client.Create(path, addr, 0)
return err
}
func (r *EtcdRegistry) Deregister(service *Service) error {
path := r.path + "/" + service.Name + "/" + service.ID
_, err := r.client.Delete(path, false)
return err
}