-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
140 lines (123 loc) · 3.55 KB
/
setup.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
136
137
138
139
140
package nacosKit
import (
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/richelieu-yang/chimera/v3/src/copyKit"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"github.com/richelieu-yang/chimera/v3/src/core/intKit"
"github.com/richelieu-yang/chimera/v3/src/core/sliceKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"github.com/richelieu-yang/chimera/v3/src/log/logrusKit"
"github.com/richelieu-yang/chimera/v3/src/netKit"
"github.com/richelieu-yang/chimera/v3/src/urlKit"
"github.com/richelieu-yang/chimera/v3/src/validateKit"
"github.com/sirupsen/logrus"
"net/url"
)
var (
clientConfig *constant.ClientConfig
serverConfigs []constant.ServerConfig
)
func MustSetUp(config *Config, options ...constant.ClientOption) {
err := SetUp(config, options...)
if err != nil {
logrusKit.DisableQuote(nil)
logrus.Fatalf("%+v", err)
}
}
// SetUp
/*
@param options !!!:
(1) 建议配置 客户端的缓存目录(default is current path) constant.WithCacheDir
(2) 建议配置 客户端的日志目录(default is current path) constant.WithLogDir
(3) 建议配置 客户端的日志级别(default value is info) constant.WithLogLevel "debug" || "info"(默认)...
*/
func SetUp(config *Config, options ...constant.ClientOption) (err error) {
defer func() {
if err != nil {
clientConfig = nil
serverConfigs = nil
}
}()
/* (0) validate */
if err = validateKit.Struct(config); err != nil {
err = errorKit.Wrap(err, "Fail to verify")
return
}
/* (1) clientConfig */
options1 := []constant.ClientOption{
constant.WithNamespaceId(config.NamespaceId),
constant.WithBeatInterval(5000), // 5000ms
constant.WithNotLoadCacheAtStart(true),
}
options1 = append(options1, options...)
clientConfig = constant.NewClientConfig(options1...)
/* (2) serverConfigs */
if err = sliceKit.AssertNotEmpty(config.Addrs, "config.Addrs"); err != nil {
return
}
for _, addr := range config.Addrs {
if strKit.IsBlank(addr) {
continue
}
var u *url.URL
u, err = urlKit.Parse(addr)
if err != nil {
err = errorKit.Wrap(err, "Fail to parse address(%s).", addr)
return
}
var port uint64
port, err = intKit.ToUint64E(u.Port())
if err != nil {
err = errorKit.Wrap(err, "Invalid address(%s) with port string(%s).", addr, u.Port())
return
}
if err = netKit.AssertValidPort(int(port)); err != nil {
err = errorKit.Wrap(err, "Invalid address(%s) with port(%d).", addr, port)
return
}
serverConfigs = append(serverConfigs, constant.ServerConfig{
Scheme: u.Scheme,
IpAddr: u.Hostname(),
Port: port,
ContextPath: u.Path,
})
}
if sliceKit.IsEmpty(serverConfigs) {
err = errorKit.New("No valid address.")
return
}
/* (3) test(以防配置有问题) */
cc, err := NewConfigClient()
if err != nil {
return
}
defer cc.CloseClient()
nc, err := NewNamingClient()
if err != nil {
return
}
defer nc.CloseClient()
return
}
func GetClientConfigCopy(options ...constant.ClientOption) (*constant.ClientConfig, error) {
if clientConfig == nil || serverConfigs == nil {
return nil, NotSetUpError
}
// 深拷贝
clientConfig1 := copyKit.DeepCopy(clientConfig)
// 再次修改 *constant.ClientConfig
for _, option := range options {
option(clientConfig1)
}
return clientConfig, nil
}
// GetNamespaceId
/*
PS: 如果一个服务仅使用一个NamespaceId,可以调用此方法.
*/
func GetNamespaceId() (string, error) {
if clientConfig == nil || serverConfigs == nil {
return "", NotSetUpError
}
return clientConfig.NamespaceId, nil
}