添加service id

This commit is contained in:
liuxiaobo 2025-05-25 23:13:59 +08:00
parent aff70d66d8
commit cd54354df2
2 changed files with 9 additions and 13 deletions

View File

@ -24,37 +24,32 @@ func NewRegistry[T INode](endpoints []string, rootKey, username, password string
return e, err return e, err
} }
func (e *Registry[T]) Register(node INode) error { func (r *Registry[T]) Register(node INode) error {
bs, err := json.Marshal(node) bs, err := json.Marshal(node)
if err != nil { if err != nil {
return err return err
} }
return e.etcdRegistryImpl.Register(node.EtcdKey(), string(bs)) return r.etcdRegistryImpl.Register(node.EtcdKey(), string(bs))
} }
// 获取当前服务 // 获取当前服务
func (sr *Registry[T]) saveNode(jsonBytes []byte) { func (r *Registry[T]) saveNode(jsonBytes []byte) {
var tmp = resultT[T]{Err: nil} var tmp = resultT[T]{Err: nil}
if err := json.Unmarshal(jsonBytes, &tmp.Value); err != nil { if err := json.Unmarshal(jsonBytes, &tmp.Value); err != nil {
log.ErrorF(err.Error()) log.ErrorF(err.Error())
} }
sr.nodes.Store(tmp.Value.MapKey(), tmp.Value) r.nodes.Store(tmp.Value.MapKey(), tmp.Value)
} }
// 获取当前根节点下所有节点信息 // 获取当前根节点下所有节点信息
func (sr *Registry[T]) GetNodes() []T { func (r *Registry[T]) GetNodes() *sync.Map {
var nodes []T return &r.nodes
sr.nodes.Range(func(k, v interface{}) bool {
nodes = append(nodes, v.(T))
return true
})
return nodes
} }
// 获取当前根节点下所有节点信息 // 获取当前根节点下所有节点信息
func (sr *Registry[T]) FindNode(key string) (T, error) { func (r *Registry[T]) FindNode(key string) (T, error) {
var tmp = resultT[T]{Err: nil} var tmp = resultT[T]{Err: nil}
v, ok := sr.nodes.Load(key) v, ok := r.nodes.Load(key)
if !ok { if !ok {
return tmp.Value, fmt.Errorf("%v not exist", key) return tmp.Value, fmt.Errorf("%v not exist", key)
} }

View File

@ -15,6 +15,7 @@ type INode interface {
} }
type ServiceNode struct { type ServiceNode struct {
ServiceId int32 `json:"service_id"` // 服务id 由proto定义gate通过该字段找到serviceNode
Name string `json:"name"` // 服务名 多个同类服务依赖name区分:1,2,3等等 Name string `json:"name"` // 服务名 多个同类服务依赖name区分:1,2,3等等
Type string `json:"type"` // 服务类型:lobby, game, gate等等 Type string `json:"type"` // 服务类型:lobby, game, gate等等
Address string `json:"address"` // 地址 Address string `json:"address"` // 地址