1 Star 1 Fork 0

flash520/utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
nacos.go 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
flash520 提交于 2021-06-29 10:08 . change: 使用 goImport 格式化代码
/**
* @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)
}
}()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/flash520/utils.git
git@gitee.com:flash520/utils.git
flash520
utils
utils
v1.3.0

搜索帮助