-
Notifications
You must be signed in to change notification settings - Fork 92
/
nacos.go
99 lines (84 loc) · 2.81 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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"strconv"
"strings"
)
import (
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
)
const (
DefaultGroupName string = "arana"
NamespaceIdKey string = "namespace_id"
GroupKey string = "group"
Username string = "username"
Password string = "password"
Server string = "endpoints"
ContextPath string = "context_path"
Scheme string = "scheme"
PageSizeKey string = "page-size"
PathSplit string = "::"
ServerSplit string = ","
)
func NewNacosV2NamingClient(options map[string]interface{}) (naming_client.INamingClient, error) {
properties := make(map[string]interface{})
properties[constant.KEY_CLIENT_CONFIG] = ParseNacosClientConfig(options)
properties[constant.KEY_SERVER_CONFIGS] = ParseNacosServerConfig(options)
return clients.CreateNamingClient(properties)
}
func ParseNacosServerConfig(options map[string]interface{}) []constant.ServerConfig {
cfgs := make([]constant.ServerConfig, 0)
scheme := "http"
if val, ok := options[Scheme]; ok {
scheme = val.(string)
}
contextPath := "/nacos"
if val, ok := options[ContextPath]; ok {
contextPath = val.(string)
}
if servers, ok := options[Server]; ok {
addresses := strings.Split(servers.(string), ServerSplit)
for i := range addresses {
addr := strings.Split(strings.TrimSpace(addresses[i]), ":")
ip := addr[0]
port, _ := strconv.ParseInt(addr[1], 10, 64)
cfgs = append(cfgs, constant.ServerConfig{
Scheme: scheme,
ContextPath: contextPath,
IpAddr: ip,
Port: uint64(port),
})
}
}
return cfgs
}
func ParseNacosClientConfig(options map[string]interface{}) constant.ClientConfig {
cc := constant.ClientConfig{}
if val, ok := options[NamespaceIdKey]; ok {
cc.NamespaceId = val.(string)
}
if val, ok := options[Username]; ok {
cc.Username = val.(string)
}
if val, ok := options[Password]; ok {
cc.Password = val.(string)
}
return cc
}