Nacos
这个章节将介绍Nacos如何使用。
简介
nacos组件是对nacos-group/nacos-sdk-go/v2
的二次封装。
该组件封装了一些最常用的方法,比如获取配置、监控配置、注册服务、卸载服务、获取健康实例等。
使用方法
初始化客户端
main.go
import (
"github.com/keepchen/go-sail/v3/lib/nacos"
)
func main() {
nacos.InitClient("appName", "endpoints", "namespace id")
}
获取配置
main.go
import (
"github.com/keepchen/go-sail/v3/lib/nacos"
sailConfig "github.com/keepchen/go-sail/v3/sail/config"
)
func main() {
var conf = &sailConfig.Config{}
err := nacos.GetConfig(group, dataID, conf, "yaml")
}
监听配置
main.go
import (
"github.com/keepchen/go-sail/v3/lib/nacos"
sailConfig "github.com/keepchen/go-sail/v3/sail/config"
)
func main() {
var conf = &sailConfig.Config{}
callback := func(namespace, group, dataId, data string) {
err = nacos.ParseConfig([]byte(data), conf, "yaml")
if err != nil {
fmt.Printf("<Nacos> listen config {%s:%s} change,but can't be unmarshal: %s\n", group, dataId, err.Error())
return
}
}
//listen config if it changed
err = nacos.ListenConfigWithCallback(group, dataID, callback)
if err != nil {
panic(err)
}
}
注册服务
main.go
import (
"github.com/keepchen/go-sail/v3/lib/nacos"
)
func main() {
ok, err := nacos.RegisterService(groupName, serviceName, ip, port, metadata)
}
下线服务
main.go
import (
"github.com/keepchen/go-sail/v3/lib/nacos"
)
func main() {
ok, err := nacos.UnregisterService(groupName, serviceName, ip, port)
}
获取健康实例
main.go
import (
"github.com/keepchen/go-sail/v3/lib/nacos"
)
func main() {
serviceUrl := nacos.GetHealthyInstanceUrl(group, serviceName, sail.GetLogger())
if len(serviceUrl) == 0 {
sail.GetLogger().Warn("no healthy instances")
return ""
}
}
其他
更多原生调用方法请查看nacos-group/nacos-sdk-go/v2官方文档。