/
nacos.go
120 lines (103 loc) · 3.17 KB
/
nacos.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package nacos implements service Registry and Discovery using nacos.
package nacos
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gsvc"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/joy999/nacos-sdk-go/clients"
"github.com/joy999/nacos-sdk-go/clients/naming_client"
"github.com/joy999/nacos-sdk-go/common/constant"
"github.com/joy999/nacos-sdk-go/vo"
)
const (
cstServiceSeparator = "@@"
)
var (
_ gsvc.Registry = &Registry{}
)
// Registry is nacos registry.
type Registry struct {
client naming_client.INamingClient
clusterName string
groupName string
}
// Config is the configuration object for nacos client.
type Config struct {
ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig
ClientConfig *constant.ClientConfig `v:"required"` // See constant.ClientConfig
}
// New new a registry with address and opts
func New(address string, opts ...constant.ClientOption) (reg *Registry) {
endpoints := gstr.SplitAndTrim(address, ",")
if len(endpoints) == 0 {
panic(gerror.NewCodef(gcode.CodeInvalidParameter, `invalid nacos address "%s"`, address))
}
clientConfig := constant.NewClientConfig(opts...)
if len(clientConfig.NamespaceId) == 0 {
clientConfig.NamespaceId = "public"
}
serverConfigs := make([]constant.ServerConfig, 0, len(endpoints))
for _, endpoint := range endpoints {
tmp := gstr.Split(endpoint, ":")
ip := tmp[0]
port := gconv.Uint64(tmp[1])
if port == 0 {
port = 8848
}
serverConfigs = append(serverConfigs, *constant.NewServerConfig(ip, port))
}
ctx := gctx.New()
reg, err := NewWithConfig(ctx, Config{
ServerConfigs: serverConfigs,
ClientConfig: clientConfig,
})
if err != nil {
panic(gerror.Wrap(err, `create nacos client failed`))
}
return
}
// New creates and returns registry with Config.
func NewWithConfig(ctx context.Context, config Config) (reg *Registry, err error) {
// Data validation.
err = g.Validator().Data(config).Run(ctx)
if err != nil {
return nil, err
}
nameingClient, err := clients.NewNamingClient(vo.NacosClientParam{
ClientConfig: config.ClientConfig,
ServerConfigs: config.ServerConfigs,
})
if err != nil {
return
}
return NewWithClient(nameingClient), nil
}
// NewWithClient new the instance with INamingClient
func NewWithClient(client naming_client.INamingClient) *Registry {
r := &Registry{
client: client,
clusterName: "DEFAULT",
groupName: "DEFAULT_GROUP",
}
return r
}
// SetClusterName can set the clusterName. The default is 'DEFAULT'
func (reg *Registry) SetClusterName(clusterName string) *Registry {
reg.clusterName = clusterName
return reg
}
// SetGroupName can set the groupName. The default is 'DEFAULT_GROUP'
func (reg *Registry) SetGroupName(groupName string) *Registry {
reg.groupName = groupName
return reg
}