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"),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package nacos
|
||||
|
||||
import (
|
||||
"apiServer_service/utils/loger"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/clients"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/vo"
|
||||
)
|
||||
|
||||
// NewNacosConfigClient 创建一个 Nacos 配置服务
|
||||
func NewNacosConfigClient() (*config_client.IConfigClient, error) {
|
||||
InitNacosRegistryConfig()
|
||||
cli, err := clients.NewConfigClient(
|
||||
vo.NacosClientParam{
|
||||
ClientConfig: cc,
|
||||
ServerConfigs: sc,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cli, nil
|
||||
}
|
||||
|
||||
// AddConfig 新增配置
|
||||
func AddConfig(dataId, group, content string) error {
|
||||
client, err := NewNacosConfigClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = (*client).PublishConfig(vo.ConfigParam{
|
||||
DataId: dataId,
|
||||
Group: group,
|
||||
Content: content,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfig 获取配置
|
||||
func GetConfig(dataId, group string) string {
|
||||
client, err := NewNacosConfigClient()
|
||||
if err != nil {
|
||||
loger.Error("获取配置客户端失败", err)
|
||||
return ""
|
||||
}
|
||||
content, err := (*client).GetConfig(vo.ConfigParam{
|
||||
DataId: dataId,
|
||||
Group: group,
|
||||
})
|
||||
if err != nil {
|
||||
loger.Error("获取配置失败", err)
|
||||
return ""
|
||||
}
|
||||
return content
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package nacos
|
||||
|
||||
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/model"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/vo"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
cli naming_client.INamingClient
|
||||
groupName string
|
||||
)
|
||||
|
||||
// NewNacosRegistry 创建一个nacos注册中心
|
||||
func NewNacosRegistry() (*naming_client.INamingClient, error) {
|
||||
InitNacosRegistryConfig()
|
||||
if cli != nil {
|
||||
return &cli, nil
|
||||
}
|
||||
groupName = os.Getenv("NACOS_GROUP_NAME")
|
||||
var err error
|
||||
cli, err = clients.NewNamingClient(
|
||||
vo.NacosClientParam{
|
||||
ClientConfig: cc,
|
||||
ServerConfigs: sc,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cli, nil
|
||||
}
|
||||
|
||||
// RegisterService 注册当前服务到nacos中
|
||||
func RegisterService() error {
|
||||
client, err := NewNacosRegistry()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
serviceName := os.Getenv("NACOS_SERVICE_NAME")
|
||||
host := os.Getenv("NACOS_SERVICE_HOST")
|
||||
port, err := strconv.Atoi(os.Getenv("NACOS_SERVICE_PORT"))
|
||||
if err != nil {
|
||||
port = 8848
|
||||
}
|
||||
weight, err := strconv.Atoi(os.Getenv("NACOS_SERVICE_WEIGHT"))
|
||||
if err != nil {
|
||||
weight = 10
|
||||
}
|
||||
_, err = (*client).RegisterInstance(vo.RegisterInstanceParam{
|
||||
Ip: host,
|
||||
Port: uint64(port),
|
||||
ServiceName: serviceName,
|
||||
Weight: float64(weight),
|
||||
Enable: true,
|
||||
Healthy: true,
|
||||
Ephemeral: false,
|
||||
GroupName: groupName,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DiscoverServiceList 发现服务列表
|
||||
func DiscoverServiceList(serviceName string) ([]model.Instance, error) {
|
||||
client, err := NewNacosRegistry()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
instances, err := (*client).SelectInstances(vo.SelectInstancesParam{
|
||||
ServiceName: serviceName,
|
||||
HealthyOnly: false,
|
||||
GroupName: groupName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return instances, nil
|
||||
}
|
||||
|
||||
// DiscoverService 发现一个服务
|
||||
func DiscoverService(serviceName string) (model.Instance, error) {
|
||||
client, err := NewNacosRegistry()
|
||||
if err != nil {
|
||||
return model.Instance{}, err
|
||||
}
|
||||
instances, err := (*client).SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
|
||||
ServiceName: serviceName,
|
||||
GroupName: groupName,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Instance{}, err
|
||||
}
|
||||
return *instances, nil
|
||||
}
|
||||
Reference in New Issue
Block a user