This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
forked from weibocom/motan-go
/
directRegistry.go
82 lines (73 loc) · 1.93 KB
/
directRegistry.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
package registry
import (
"strconv"
"strings"
motan "github.com/Loopring/motan-go/core"
"github.com/Loopring/motan-go/log"
)
type DirectRegistry struct {
url *motan.URL
urls []*motan.URL
}
func (d *DirectRegistry) GetURL() *motan.URL {
return d.url
}
func (d *DirectRegistry) SetURL(url *motan.URL) {
d.url = url
d.urls = parseURLs(url)
}
func (d *DirectRegistry) GetName() string {
return "direct"
}
func (d *DirectRegistry) InitRegistry() {
}
func (d *DirectRegistry) Subscribe(url *motan.URL, listener motan.NotifyListener) {
}
func (d *DirectRegistry) Unsubscribe(url *motan.URL, listener motan.NotifyListener) {
}
func (d *DirectRegistry) Discover(url *motan.URL) []*motan.URL {
if d.urls == nil {
d.urls = parseURLs(d.url)
}
result := make([]*motan.URL, 0, len(d.urls))
for _, u := range d.urls {
newURL := *url
newURL.Host = u.Host
newURL.Port = u.Port
result = append(result, &newURL)
}
return result
}
func (d *DirectRegistry) Register(serverURL *motan.URL) {
vlog.Infof("direct registry:register url :%+v\n", serverURL)
}
func (d *DirectRegistry) UnRegister(serverURL *motan.URL) {
}
func (d *DirectRegistry) Available(serverURL *motan.URL) {
}
func (d *DirectRegistry) Unavailable(serverURL *motan.URL) {
}
func (d *DirectRegistry) GetRegisteredServices() []*motan.URL {
return nil
}
func (d *DirectRegistry) StartSnapshot(conf *motan.SnapshotConf) {}
func parseURLs(url *motan.URL) []*motan.URL {
urls := make([]*motan.URL, 0)
if len(url.Host) > 0 && url.Port > 0 {
urls = append(urls, url)
} else if address, exist := url.Parameters[motan.AddressKey]; exist {
for _, add := range strings.Split(address, ",") {
hostport := strings.Split(add, ":")
if len(hostport) == 2 {
port, err := strconv.Atoi(hostport[1])
if err == nil {
u := &motan.URL{Host: hostport[0], Port: port}
urls = append(urls, u)
}
}
}
} else {
vlog.Warningf("direct registry parse fail.url:L %+v", url)
}
return urls
}