-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
executable file
·135 lines (101 loc) · 3.3 KB
/
builder.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package nacos
import (
"context"
"fmt"
"net"
"strconv"
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"github.com/pkg/errors"
"google.golang.org/grpc/resolver"
)
func init() {
fmt.Print("----> nacos init <-----")
resolver.Register(&builder{})
}
// schemeName for the urls
// All target URLs like 'nacos://.../...' will be resolved by this resolver
const schemeName = "nacos"
// builder implements resolver.Builder and use for constructing all consul resolvers
type builder struct{}
func (b *builder) Build(url resolver.Target, conn resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
fmt.Print("==> grpc-go/resolver/resolver.go Build 接口实现 <====")
tgt, err := parseURL(url.URL)
if err != nil {
return nil, errors.Wrap(err, "Wrong nacos URL")
}
host, ports, err := net.SplitHostPort(tgt.Addr)
if err != nil {
return nil, fmt.Errorf("failed parsing address error: %v", err)
}
port, _ := strconv.ParseUint(ports, 10, 16)
fmt.Print("********Host, port ******")
fmt.Print("**port:", port)
fmt.Print("**host:", host)
fmt.Print("********Host, port ******")
sc := []constant.ServerConfig{
*constant.NewServerConfig(host, port, constant.WithContextPath("/nacos")),
}
/*
sc := []constant.ServerConfig{
*constant.NewServerConfig(host, port),
}
cc := &constant.ClientConfig{
AppName: tgt.AppName,
NamespaceId: tgt.NamespaceID,
AccessKey: tgt.User,
SecretKey: tgt.Password,
TimeoutMs: uint64(tgt.Timeout),
NotLoadCacheAtStart: tgt.NotLoadCacheAtStart,
UpdateCacheWhenEmpty: tgt.UpdateCacheWhenEmpty,
} */
// start
cc := constant.NewClientConfig(
constant.WithTimeoutMs(10*1000),
constant.WithBeatInterval(2*1000),
constant.WithNotLoadCacheAtStart(true),
constant.WithAccessKey(tgt.User),
constant.WithSecretKey(tgt.Password),
constant.WithNamespaceId(tgt.NamespaceID),
constant.WithOpenKMS(false),
//constant.WithKMSVersion(constant.KMSv3),
constant.WithRegionId("cn-hangzhou-e"),
)
// end
if tgt.CacheDir != "" {
cc.CacheDir = tgt.CacheDir
}
if tgt.LogDir != "" {
cc.LogDir = tgt.LogDir
}
if tgt.LogLevel != "" {
cc.LogLevel = tgt.LogLevel
}
fmt.Print("===>nacos:", tgt)
fmt.Print("nacos<=====")
cli, err := clients.NewNamingClient(vo.NacosClientParam{
ServerConfigs: sc,
ClientConfig: cc,
})
if err != nil {
return nil, errors.Wrap(err, "Couldn't connect to the nacos API")
}
ctx, cancel := context.WithCancel(context.Background())
pipe := make(chan []string)
tgt.GroupName = tgt.GroupName
fmt.Print("***GroupName", tgt.GroupName)
go cli.Subscribe(&vo.SubscribeParam{
ServiceName: tgt.Service,
Clusters: tgt.Clusters,
GroupName: tgt.GroupName,
SubscribeCallback: newWatcher(ctx, cancel, pipe).CallBackHandle, // required
})
go populateEndpoints(ctx, conn, pipe)
return &resolvr{cancelFunc: cancel}, nil
}
// Scheme returns the scheme supported by this resolver.
// Scheme is defined at https://github.com/grpc/grpc/blob/master/doc/naming.md.
func (b *builder) Scheme() string {
return schemeName
}