// ServiceNode 服务注册对象
type ServiceNode struct {
etcd *Etcd
node Node
}
// NewServiceNode 新建一个服务注册对象
func NewServiceNode(endpoints []string, dc, nid, name, nip string) *ServiceNode {
var serverNode ServiceNode
etcd, _ := NewEtcd(endpoints)
serverNode.etcd = etcd
serverNode.node = Node{
Ndc: dc,
Nid: nid,
Name: name,
Nip: nip,
Npayload: "0",
}
return &serverNode
}
// RegisterNode 注册服务节点
func (serverNode *ServiceNode) RegisterNode() error {
if serverNode.node.Ndc == "" || serverNode.node.Nid == "" || serverNode.node.Name == "" {
return fmt.Errorf("Node dc id or name must be non empty")
}
go serverNode.keepRegistered(serverNode.node)
return nil
}
// keepRegistered 注册一个服务节点到etcd服务管理上
func (serverNode *ServiceNode) keepRegistered(node Node) {
for {
err := serverNode.etcd.Keep(node.Nid, node.GetNodeValue())
if err != nil {
log.Printf("keepRegistered err = %s", err)
time.Sleep(5 * time.Second)
} else {
log.Printf("Node [%s] keepRegistered success!", node.Nid)
return
}
}
}
1
共 2 条评论, 1 页
评论区
写评论你这个代码有依赖呀,Etcd 先把这个库用rust实现一遍吧
关键点在于怎么用rust的tokio替换golang的协程吧 其他都是业务逻辑的事了 当然你这么问 别人也不太可能帮你把代码全部写一遍的😂