forked from weibocom/motan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
109 lines (90 loc) · 2.44 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
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
package registry
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"sync"
"time"
"unsafe"
motan "github.com/weibocom/motan-go/core"
)
const (
DefaultSnapshotInterval = 10 * time.Second
DEFAULT_HEARTBEAT_INTERVAL = 10 * 1000 //ms
DEFAULT_TIMEOUT = 3 * 1000 //ms
DefaultSnapshotDir = "./snapshot"
)
//ext name
const (
Direct = "direct"
Consul = "consul"
ZK = "zookeeper"
)
type SnapShotNodeInfo struct {
ExtInfo string `json:"extInfo"`
Addr string `json:"address"`
}
type ServiceNode struct {
Group string `json:"group"`
Path string `json:"path"`
Nodes []SnapShotNodeInfo `json:"nodes"`
}
var (
// TODO support mulit registry
snapshotConf = &motan.SnapshotConf{SnapshotInterval: DefaultSnapshotInterval, SnapshotDir: DefaultSnapshotDir}
nodeRsSnapshotLock sync.RWMutex
)
func SetSanpshotConf(snapshotInterval time.Duration, snapshotDir string) {
snapshotConf.SnapshotDir = snapshotDir
snapshotConf.SnapshotInterval = snapshotInterval
}
func GetSanpshotConf() *motan.SnapshotConf {
return snapshotConf
}
func RegistDefaultRegistry(extFactory motan.ExtentionFactory) {
extFactory.RegistExtRegistry(Direct, func(url *motan.Url) motan.Registry {
return &DirectRegistry{url: url}
})
extFactory.RegistExtRegistry(ZK, func(url *motan.Url) motan.Registry {
return &ZkRegistry{url: url}
})
extFactory.RegistExtRegistry(Consul, func(url *motan.Url) motan.Registry {
return &ConsulRegistry{url: url}
})
}
func IsAgent(url *motan.Url) bool {
isAgent := false
if t, ok := url.Parameters["nodeType"]; ok {
if t == "agent" {
isAgent = true
}
}
return isAgent
}
func GetSubKey(url *motan.Url) string {
return url.Group + "/" + url.Path + "/service"
}
func getNodeKey(url *motan.Url) string {
return url.Group + "_" + url.Path
}
func SliceByteToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func StringToSliceByte(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func saveSnapshot(d string, nodes map[string]ServiceNode) {
nodeRsSnapshotLock.RLock()
for key, node := range nodes {
nodeRsSnapshot := JSONString(node)
// ioutil.ReadFile(filepath.Join(d, key))
ioutil.WriteFile(filepath.Join(d, key), StringToSliceByte(nodeRsSnapshot), 0777)
}
nodeRsSnapshotLock.RUnlock()
}
func JSONString(v interface{}) string {
bytes, _ := json.Marshal(v)
return string(bytes)
}