-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobin.go
60 lines (44 loc) · 1.25 KB
/
robin.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
package main
import (
"fmt"
"github.com/inconshreveable/log15"
"go-micro-service-demo/robinwithweight"
)
var serviceMap map[string]*robinwithweight.RobinwithWeight
func init() {
serviceMap = make(map[string]*robinwithweight.RobinwithWeight)
}
func GetServiceFromMapWithRoundRobin(serviceName string) *robinwithweight.Service {
a := serviceMap[serviceName]
if a != nil {
return a.GetUrl()
} else {
}
return nil
}
func GenServiceMap() {
serviceMap = make(map[string]*robinwithweight.RobinwithWeight) //renew a map ,because if a service dereg,all health wont have that service
fmt.Println("+----------------- GenServiceMap ------------------------+")
for _, v := range allhealthchecks {
if v.ServiceID == "" {
continue
}
fmt.Println("| ", v.ServiceName, v.ServiceID, v.Status)
a := serviceMap[v.ServiceName]
if a == nil {
a = &robinwithweight.RobinwithWeight{}
serviceMap[v.ServiceName] = a
}
if v.Status == "passing" {
x := agentServices[v.ServiceID]
if x == nil {
log15.Error("agentServices get error", "ServiceID", v.ServiceID)
} else {
a.Addservice(v.ServiceID, x.Address, x.Port, 1)
}
} else {
a.Delservice(v.ServiceID)
}
}
fmt.Println("+--------------------------------------------------------+")
}