forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eureka.go
136 lines (109 loc) · 2.9 KB
/
eureka.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Package eureka provides a Eureka registry
package eureka
/*
Eureka is a plugin for Netflix Eureka service discovery
*/
import (
"context"
"net/http"
"time"
"github.com/hudl/fargo"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/registry"
"github.com/op/go-logging"
)
type fargoConnection interface {
RegisterInstance(*fargo.Instance) error
DeregisterInstance(*fargo.Instance) error
HeartBeatInstance(*fargo.Instance) error
GetInstance(string, string) (*fargo.Instance, error)
GetApp(string) (*fargo.Application, error)
GetApps() (map[string]*fargo.Application, error)
ScheduleAppUpdates(string, bool, <-chan struct{}) <-chan fargo.AppUpdate
}
type eurekaRegistry struct {
conn fargoConnection
opts registry.Options
}
func init() {
cmd.DefaultRegistries["eureka"] = NewRegistry
logging.SetLevel(logging.ERROR, "fargo")
}
func newRegistry(opts ...registry.Option) registry.Registry {
options := registry.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
var cAddrs []string
for _, addr := range options.Addrs {
if len(addr) == 0 {
continue
}
cAddrs = append(cAddrs, addr)
}
if len(cAddrs) == 0 {
cAddrs = []string{"http://localhost:8080/eureka/v2"}
}
if c, ok := options.Context.Value(contextHttpClient{}).(*http.Client); ok {
fargo.HttpClient = c
}
conn := fargo.NewConn(cAddrs...)
conn.PollInterval = time.Second * 5
return &eurekaRegistry{
conn: &conn,
opts: options,
}
}
func (e *eurekaRegistry) Options() registry.Options {
return e.opts
}
func (e *eurekaRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
instance, err := serviceToInstance(s)
if err != nil {
return err
}
if e.instanceRegistered(instance) {
return e.conn.HeartBeatInstance(instance)
}
return e.conn.RegisterInstance(instance)
}
func (e *eurekaRegistry) Deregister(s *registry.Service) error {
instance, err := serviceToInstance(s)
if err != nil {
return err
}
return e.conn.DeregisterInstance(instance)
}
func (e *eurekaRegistry) GetService(name string) ([]*registry.Service, error) {
app, err := e.conn.GetApp(name)
if err != nil {
return nil, err
}
return appToService(app), nil
}
func (e *eurekaRegistry) ListServices() ([]*registry.Service, error) {
var services []*registry.Service
apps, err := e.conn.GetApps()
if err != nil {
return nil, err
}
for _, app := range apps {
services = append(services, appToService(app)...)
}
return services, nil
}
func (e *eurekaRegistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
return newWatcher(e.conn, opts...), nil
}
func (e *eurekaRegistry) String() string {
return "eureka"
}
func (e *eurekaRegistry) instanceRegistered(instance *fargo.Instance) bool {
_, err := e.conn.GetInstance(instance.App, instance.UniqueID(*instance))
return err == nil
}
func NewRegistry(opts ...registry.Option) registry.Registry {
return newRegistry(opts...)
}