-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
75 lines (66 loc) · 1.88 KB
/
resolver.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
package nacos_demo
import (
"context"
"fmt"
"github.com/cloudwego/hertz/pkg/app/client/discovery"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/vo"
)
type nacosResolver struct {
cli naming_client.INamingClient
opts options
}
// NewNacosResolver create a service resolver using nacos.
func NewNacosResolver(cli naming_client.INamingClient, opts ...Option) discovery.Resolver {
op := options{
cluster: "DEFAULT",
group: "DEFAULT_GROUP",
}
for _, option := range opts {
option(&op)
}
return &nacosResolver{cli: cli, opts: op}
}
// Target return a description for the given target that is suitable for being a key for cache.
func (n *nacosResolver) Target(ctx context.Context, target *discovery.TargetInfo) (description string) {
return target.Host
}
// Resolve a service info by desc.
func (n *nacosResolver) Resolve(ctx context.Context, desc string) (discovery.Result, error) {
res, err := n.cli.SelectInstances(vo.SelectInstancesParam{
ServiceName: desc,
HealthyOnly: true,
GroupName: n.opts.group,
Clusters: []string{n.opts.cluster},
})
if err != nil {
return discovery.Result{}, err
}
if len(res) == 0 {
return discovery.Result{}, fmt.Errorf("no instance remains for %v", desc)
}
instances := make([]discovery.Instance, 0, len(res))
for _, in := range res {
if !in.Enable {
continue
}
instances = append(instances, discovery.NewInstance(
"tcp",
fmt.Sprintf("%s:%d", in.Ip, in.Port),
int(in.Weight),
in.Metadata),
)
}
if len(instances) == 0 {
return discovery.Result{}, fmt.Errorf("no instance remains for %v", desc)
}
return discovery.Result{
CacheKey: desc,
Instances: instances,
}, nil
}
// Name returns the name of the resolver.
func (n *nacosResolver) Name() string {
return "nacos" + ":" + n.opts.cluster + ":" + n.opts.group
}
var _ discovery.Resolver = (*nacosResolver)(nil)