This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
common.go
134 lines (118 loc) · 3.56 KB
/
common.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
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed 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 polaris
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/cloudwego/kitex/pkg/discovery"
"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/model"
)
// GetPolarisConfig get polaris config from endpoints.
func GetPolarisConfig(configFile ...string) (api.SDKContext, error) {
var (
cfg config.Configuration
err error
)
if len(configFile) != 0 {
cfg, err = config.LoadConfigurationByFile(configFile[0])
} else {
cfg, err = config.LoadConfigurationByDefaultFile()
}
if err != nil {
return nil, err
}
sdkCtx, err := api.InitContextByConfig(cfg)
if err != nil {
return nil, err
}
return sdkCtx, nil
}
// SplitDescription splits description to namespace and serviceName.
func SplitDescription(description string) (string, string) {
str := strings.Split(description, ":")
namespace, serviceName := str[0], str[1]
return namespace, serviceName
}
// ChangePolarisInstanceToKitex transforms polaris instance to Kitex instance.
func ChangePolarisInstanceToKitex(PolarisInstance model.Instance) discovery.Instance {
weight := PolarisInstance.GetWeight()
if weight <= 0 {
weight = defaultWeight
}
addr := PolarisInstance.GetHost() + ":" + strconv.Itoa(int(PolarisInstance.GetPort()))
tags := map[string]string{
"namespace": PolarisInstance.GetNamespace(),
}
KitexInstance := discovery.NewInstance(PolarisInstance.GetProtocol(), addr, weight, tags)
// In KitexInstance , tags can be used as IDC、Cluster、Env 、namespace、and so on.
return KitexInstance
}
// GetLocalIPv4Address gets local ipv4 address when info host is empty.
func GetLocalIPv4Address() (string, error) {
addr, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, addr := range addr {
ipNet, isIpNet := addr.(*net.IPNet)
if isIpNet && !ipNet.IP.IsLoopback() {
ipv4 := ipNet.IP.To4()
if ipv4 != nil {
return ipv4.String(), nil
}
}
}
return "", fmt.Errorf("not found ipv4 address")
}
// GetInfoHostAndPort gets Host and port from info.Addr.
func GetInfoHostAndPort(Addr string) (string, int, error) {
infoHost, port, err := net.SplitHostPort(Addr)
if err != nil {
return "", 0, err
} else {
if port == "" {
return infoHost, 0, fmt.Errorf("registry info addr missing port")
}
if infoHost == "" {
ipv4, err := GetLocalIPv4Address()
if err != nil {
return "", 0, fmt.Errorf("get local ipv4 error, cause %v", err)
}
infoHost = ipv4
}
}
infoPort, err := strconv.Atoi(port)
if err != nil {
return "", 0, err
}
return infoHost, infoPort, nil
}
// GetInstanceKey generates instanceKey for one instance.
func GetInstanceKey(namespace, serviceName, host, port string) string {
var instanceKey strings.Builder
instanceKey.WriteString(namespace)
instanceKey.WriteString(":")
instanceKey.WriteString(serviceName)
instanceKey.WriteString(":")
instanceKey.WriteString(host)
instanceKey.WriteString(":")
instanceKey.WriteString(port)
return instanceKey.String()
}