代码拉取完成,页面将自动刷新
/**
* @Author: koulei
* @Description: TODO
* @File: nacos
* @Version: 1.0.0
* @Date: 2021/5/11 22:43
*/
package nacos
import (
"os"
"os/signal"
"sync"
"syscall"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/util"
"github.com/nacos-group/nacos-sdk-go/vo"
)
var (
namingClient naming_client.INamingClient
err error
)
type Nacos struct {
}
// CreateNacos 初始化nacos 并将应用注册到nacos
func CreateNacos(node, contextPath, nameSpaceID, appName string, Port, appPort uint64) *Nacos {
var once sync.Once
once.Do(func() {
sc := []constant.ServerConfig{
{
IpAddr: node,
Port: Port,
ContextPath: contextPath,
},
}
cc := constant.ClientConfig{
NamespaceId: nameSpaceID,
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
RotateTime: "1h",
MaxAge: 3,
LogLevel: "info",
}
namingClient, err = clients.CreateNamingClient(map[string]interface{}{
"serverConfigs": sc,
"clientConfig": cc,
})
if err != nil {
panic(err)
}
RegisterServiceInstance(vo.RegisterInstanceParam{
Ip: util.LocalIP(),
Port: appPort,
ServiceName: appName,
Weight: 10,
Enable: true,
Healthy: true,
Ephemeral: true,
Metadata: map[string]string{"idc": "chengdu"},
})
signalNotify(appName, appPort)
})
return &Nacos{}
}
func signalNotify(appName string, appPort uint64) {
signs := make(chan os.Signal, 1)
signal.Notify(signs, syscall.SIGHUP, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
go func() {
switch <-signs {
case syscall.SIGHUP:
fallthrough
case syscall.SIGINT:
fallthrough
case syscall.SIGKILL:
fallthrough
case syscall.SIGTERM:
DeRegisterServiceInstance(vo.DeregisterInstanceParam{
Ip: util.LocalIP(),
Port: appPort,
Cluster: "",
ServiceName: appName,
GroupName: "",
Ephemeral: true,
})
os.Exit(0)
}
}()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。