// // 删除Service Endpoint // func (s *ServiceEndpoint) DeleteServiceEndpoint(top *zk.Topology) { path := top.ProductServiceEndPointPath(s.Service, s.ServiceId) if ok, _ := top.Exist(path); ok { zkhelper.DeleteRecursive(top.ZkConn, path, -1) log.Println(Red("DeleteServiceEndpoint"), "Path: ", path) } }
func GetServiceEndpoint(top *zk.Topology, service string, serviceId string) (endpoint *ServiceEndpoint, err error) { path := top.ProductServiceEndPointPath(service, serviceId) data, _, err := top.ZkConn.Get(path) if err != nil { return nil, err } endpoint = &ServiceEndpoint{} err = json.Unmarshal(data, endpoint) if err != nil { return nil, err } else { return endpoint, nil } }
// // 注册一个服务的Endpoints // func (s *ServiceEndpoint) AddServiceEndpoint(topo *zk.Topology) error { path := topo.ProductServiceEndPointPath(s.Service, s.ServiceId) data, err := json.Marshal(s) if err != nil { return err } // 创建Service(XXX: Service本身不包含数据) zk.CreateRecursive(topo.ZkConn, os_path.Dir(path), "", 0, zkhelper.DefaultDirACLs()) // 当前的Session挂了,服务就下线 // topo.FlagEphemeral // 参考: https://www.box.com/blog/a-gotcha-when-using-zookeeper-ephemeral-nodes/ // 如果之前的Session信息还存在,则先删除;然后再添加 topo.ZkConn.Delete(path, -1) var pathCreated string pathCreated, err = topo.ZkConn.Create(path, []byte(data), int32(zookeeper.FlagEphemeral), zkhelper.DefaultFileACLs()) log.Println(Green("AddServiceEndpoint"), "Path: ", pathCreated, ", Error: ", err) return err }