54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
![]() |
package etcd
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
const (
|
||
|
rootKeyServices = "services"
|
||
|
rootKeyTopic = "topic"
|
||
|
)
|
||
|
|
||
|
type INode interface {
|
||
|
// 注册到etcd的key
|
||
|
EtcdKey() string
|
||
|
EtcdRootKey() string
|
||
|
MapKey() string
|
||
|
}
|
||
|
|
||
|
type ServiceNode struct {
|
||
|
Name string `json:"name"` // 服务名 多个同类服务依赖name区分:1,2,3等等
|
||
|
Type string `json:"type"` // 服务类型:lobby, game, gate等等
|
||
|
Address string `json:"address"` // 地址
|
||
|
Port int `json:"port"` // 端口
|
||
|
Version string `json:"version"` // 版本号
|
||
|
ServiceType ServiceType `json:"service_type"` // 服务类型
|
||
|
}
|
||
|
|
||
|
func (s ServiceNode) EtcdKey() string {
|
||
|
return fmt.Sprintf("/%s/%s/%s", rootKeyServices, s.Type, s.Name)
|
||
|
}
|
||
|
|
||
|
func (s ServiceNode) EtcdRootKey() string {
|
||
|
return rootKeyServices
|
||
|
}
|
||
|
|
||
|
func (s ServiceNode) MapKey() string {
|
||
|
return fmt.Sprintf("%s-%s", s.Type, s.Name)
|
||
|
}
|
||
|
|
||
|
type TopicNode struct {
|
||
|
Name string `json:"name"` // topic名
|
||
|
Creator string `json:"creator"` // topic创建者
|
||
|
}
|
||
|
|
||
|
func (s TopicNode) EtcdKey() string {
|
||
|
return fmt.Sprintf("/%s/%s/%s", rootKeyTopic, s.Creator, s.Name)
|
||
|
}
|
||
|
|
||
|
func (s TopicNode) EtcdRootKey() string {
|
||
|
return rootKeyTopic
|
||
|
}
|
||
|
|
||
|
func (s TopicNode) MapKey() string {
|
||
|
return fmt.Sprintf("%s", s.Name)
|
||
|
}
|