代码拉取完成,页面将自动刷新
package local
import (
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/system"
"gitee.com/h79/goutils/discovery/config"
"gitee.com/h79/goutils/discovery/registry"
"gitee.com/h79/goutils/discovery/service"
"time"
)
var _ service.Service = (*defaultService)(nil)
type defaultService struct {
conf config.Config
endPoints config.EndPoints
registry registry.Registry
stop chan bool
}
// NewService
// 注册一个服务
func NewService(conf config.Config, points config.EndPoints, reg registry.Registry) (service.Service, error) {
logger.Info("def.service: register service")
return &defaultService{
conf: conf,
endPoints: points,
registry: reg,
stop: make(chan bool),
}, nil
}
func (s *defaultService) Start() error {
if s.conf.Check.Ttl {
system.ChildRunning(s.keepAlive)
}
if s.registry != nil {
return s.registry.Register(&s.conf.Service, s.endPoints)
}
return nil
}
func (s *defaultService) Stop() {
if s.conf.Check.Ttl {
s.stop <- true
}
if s.registry != nil {
_ = s.registry.Deregister(&s.conf.Service, s.endPoints)
}
}
func (s *defaultService) Set(data service.Data) error {
return result.RErrNotSupport
}
func (s *defaultService) Get(key service.Key) ([]*service.Data, error) {
return nil, result.RErrNotSupport
}
func (s *defaultService) keepAlive() {
sys := system.Exit()
ticker := time.NewTicker(time.Second * s.conf.Check.Interval)
for {
select {
case _ = <-s.stop:
s.revoke()
logger.Error("def.service: server stop")
return
case <-ticker.C:
s.register()
case <-sys.Done():
s.stop <- true
case <-system.Closed():
s.stop <- true
}
}
}
func (s *defaultService) register() {
if s.registry == nil {
return
}
if err := s.registry.Register(&s.conf.Service, s.endPoints); err != nil {
logger.Error("def.service: register err= %v", err)
}
}
// 撤销
func (s *defaultService) revoke() {
if s.registry == nil {
return
}
if err := s.registry.Deregister(&s.conf.Service, s.endPoints); err != nil {
logger.Error("def.service: deregister err= %v", err)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。