-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
38 lines (34 loc) · 1.07 KB
/
discovery.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
package service
import (
"errors"
"fmt"
"github.com/smallnest/rpcx/client"
"strings"
)
func CreateServiceDiscovery(basePath, addr string) (client.ServiceDiscovery, error) {
i := strings.Index(addr, "://")
if i < 0 {
return nil, errors.New("wrong format registry address, the right format is registry_type://address")
}
regType := addr[:i]
addr = addr[i+3:]
switch regType {
case "peer2peer":
return client.NewPeer2PeerDiscovery(fmt.Sprintf("tcp@%s", addr), "")
case "multiple":
var pairs []*client.KVPair
regs := strings.Split(addr, ",")
for _, v := range regs {
pairs = append(pairs, &client.KVPair{Key: v})
}
return client.NewMultipleServersDiscovery(pairs)
case "zookeeper":
return client.NewZookeeperDiscoveryTemplate(basePath, []string{addr}, nil)
case "consul":
return client.NewConsulDiscoveryTemplate(basePath, []string{addr}, nil)
case "redis":
return client.NewRedisDiscoveryTemplate(basePath, []string{addr}, nil)
default:
return nil, fmt.Errorf("wrong registry type %s, only support peer2peer, multiple zookeeper consul redis", regType)
}
}