init
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package nacos
|
||||
|
||||
import (
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
cc *constant.ClientConfig
|
||||
sc []constant.ServerConfig
|
||||
)
|
||||
|
||||
func GetIP(domain string) string {
|
||||
// 确保域名不为空
|
||||
if domain == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 使用 net.LookupIP 查找域名的 IP 地址
|
||||
ips, err := net.LookupIP(domain)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 检查是否找到了 IP
|
||||
if len(ips) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 返回第一个 IPv4 地址(如果有)
|
||||
for _, ip := range ips {
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
return ipv4.String()
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有 IPv4,返回第一个 IP(可能是 IPv6)
|
||||
return ips[0].String()
|
||||
}
|
||||
|
||||
func InitNacosRegistryConfig() {
|
||||
if cc != nil && sc != nil {
|
||||
return
|
||||
}
|
||||
nacosHosts := strings.Split(os.Getenv("NACOS_HOSTS"), ",")
|
||||
nacosPort, _ := strconv.Atoi(os.Getenv("NACOS_PORT"))
|
||||
for _, host := range nacosHosts {
|
||||
serverConfig := constant.NewServerConfig(GetIP(host), uint64(nacosPort))
|
||||
sc = append(sc, *serverConfig)
|
||||
}
|
||||
|
||||
LogDir := os.Getenv("LOG_SAVE_PATH")
|
||||
cc = &constant.ClientConfig{
|
||||
NamespaceId: os.Getenv("NACOS_NAMESPACE"),
|
||||
TimeoutMs: 5000,
|
||||
NotLoadCacheAtStart: true,
|
||||
LogDir: LogDir,
|
||||
//CacheDir: "/tmp/nacos/cache",
|
||||
LogLevel: "debug",
|
||||
Username: os.Getenv("NACOS_USER"),
|
||||
Password: os.Getenv("NACOS_PASSWORD"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user