-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.go
57 lines (42 loc) · 1.17 KB
/
Registry.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
package main
import (
"log"
"time"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/client"
)
func RegisterSelf(serviceName, address, port, endpoint string) error {
myUri := "http://" + address + port + "/" + endpoint
cfg := client.Config{
Endpoints: []string{"http://127.0.0.1:2379"},
Transport: client.DefaultTransport,
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
kapi := client.NewKeysAPI(c)
key := "/ftcpops/registry/" + serviceName
log.Printf("Registering %q", serviceName)
_, err = kapi.Set(context.Background(), key, myUri, nil)
return err
}
func GetServiceURI(serviceName string) string {
cfg := client.Config{
Endpoints: []string{"http://127.0.0.1:2379"},
Transport: client.DefaultTransport,
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
kapi := client.NewKeysAPI(c)
key := "/ftcpops/registry/" + serviceName
resp, err := kapi.Get(context.Background(), key, nil)
if err != nil {
log.Fatal(err)
}
return resp.Node.Value
}