Commit 009e92dd authored by luxq's avatar luxq

update registry

parent 8945889f
......@@ -12,6 +12,10 @@ import (
"time"
)
const (
REGISTRY_SERVICE_INSTANCE = "REGISTRY_SERVICE_INSTANCE"
)
type RegistryInfo struct {
Timestamp int64 `redis:"timestamp" json:"timestamp"`
Instance string `redis:"instance" json:"instance"` // example: hostip + hostname
......@@ -30,7 +34,6 @@ func (g RegistryInfo) Message() json.RawMessage {
type Register interface {
ServiceType() common.ServiceType
Instance() string
Status() string
DetailInfo() (json.RawMessage, error)
}
......@@ -39,16 +42,19 @@ type Registry struct {
rdb *redis.Client
rw sync.RWMutex
register Register
opts []InstanceOpt
quit chan struct{}
}
type InstanceOpt func() string
type RedisConnParam struct {
Addr string
Password string
DbIndex int
}
func NewRegistry(redisParam RedisConnParam, register Register) *Registry {
func NewRegistry(redisParam RedisConnParam, register Register, opts ...InstanceOpt) *Registry {
switch register.ServiceType() {
case common.SERVICE_NODE_MANAGER, common.SERVICE_API_GATEWAY, common.SERVICE_BACKEND, common.SERVICE_SCHEDULER, common.SERVICE_WORKER:
//nothing
......@@ -65,6 +71,7 @@ func NewRegistry(redisParam RedisConnParam, register Register) *Registry {
return &Registry{
rdb: rdb,
register: register,
opts: opts,
quit: make(chan struct{}),
}
}
......@@ -92,17 +99,17 @@ func (s *Registry) Stop() {
}
func (s *Registry) registry(rdb *redis.Client) error {
k := fmt.Sprintf("%s%s", common.GetServiceKeyPrefix(s.register.ServiceType()), s.register.Instance())
detail, err := s.register.DetailInfo()
if err != nil {
log.WithError(err).Error("get detail info failed")
return err
}
status := s.register.Status()
instance := os.Getenv("SERVICE_INSTANCE")
if len(instance) == 0 {
instance = s.register.Instance()
instance := os.Getenv(REGISTRY_SERVICE_INSTANCE)
for _, opt := range s.opts {
instance = opt()
}
k := fmt.Sprintf("%s%s", common.GetServiceKeyPrefix(s.register.ServiceType()), instance)
err = rdb.HSet(context.Background(), k, RegistryInfo{
Timestamp: time.Now().Unix(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment