-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdriver.go
More file actions
85 lines (68 loc) · 1.75 KB
/
driver.go
File metadata and controls
85 lines (68 loc) · 1.75 KB
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
package driver
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/dtm-labs/dtmdriver"
_ "github.com/dtm-labs/dtmdriver-ego/dsn"
"github.com/dtm-labs/dtmdriver-ego/manager"
"github.com/gotomicro/ego/client/egrpc/resolver"
"github.com/gotomicro/ego/core/constant"
"github.com/gotomicro/ego/core/eregistry"
"github.com/gotomicro/ego/server"
)
const (
DriverName = "dtm-driver-ego"
)
type (
egoDriver struct{}
)
var registerMap = make(map[string]eregistry.Registry, 0)
func (e *egoDriver) GetName() string {
return DriverName
}
func (e *egoDriver) RegisterAddrResolver() {
for name, reg := range registerMap {
resolver.Register(name, reg)
}
}
func (e *egoDriver) RegisterService(target string, endpoint string) error {
if target == "" { // empty target, no action
return nil
}
cfg, err := manager.Parse(target)
if err != nil {
return err
}
info := server.ApplyOptions(
server.WithScheme("grpc"),
server.WithAddress(endpoint),
server.WithKind(constant.ServiceProvider),
server.WithName(cfg.ServiceName),
)
reg := cfg.Registry
registerMap[cfg.Scheme] = reg
return reg.RegisterService(context.Background(), &info)
}
// etcd:///<服务名称>/api.hello/TransOut
// k8s:///<服务名称>/api.hello/TransOut
func (e *egoDriver) ParseServerMethod(uri string) (server string, method string, err error) {
// direct 直连服务
if !strings.Contains(uri, "///") {
sep := strings.IndexByte(uri, '/')
if sep == -1 {
return "", "", fmt.Errorf("bad url: '%s'. no '/' found", uri)
}
return uri[:sep], uri[sep:], nil
}
uri = strings.Replace(uri, "///", "//", -1)
u, err := url.Parse(uri)
if err != nil {
return "", "", nil
}
return u.Scheme + ":///" + u.Host, u.Path, nil
}
func init() {
dtmdriver.Register(&egoDriver{})
}