1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
service.go 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* by huqiuyun
* 启动一个服务时,注册到consul中心,从而可以收到配置信息的变化通知
*/
package consul
import (
"fmt"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/server"
"gitee.com/h79/goutils/common/system"
"gitee.com/h79/goutils/discovery/config"
"gitee.com/h79/goutils/discovery/registry"
"gitee.com/h79/goutils/discovery/service"
consul "github.com/hashicorp/consul/api"
"time"
)
var _ service.Service = (*clService)(nil)
type clService struct {
Base
id string
serviceName string
conf config.Config
stop chan bool
}
func NewService(cfg config.Config, points config.EndPoints, registry registry.Registry) (service.Service, error) {
// initial consul client config
cli, err := NewClientWithPoints(points)
if err != nil {
return nil, err
}
logger.Info("Consul.service: create consul client")
id := fmt.Sprintf("service: %s", cfg.Server.To())
serviceName := cfg.Node.NameWith("")
reg := &consul.AgentServiceRegistration{
ID: id,
Name: serviceName,
Tags: cfg.Tags,
Port: cfg.Server.Port,
Address: cfg.Server.Host,
}
logger.Info("Consul.service: register info= %+v", reg)
if err = cli.Agent().ServiceRegister(reg); err != nil {
return nil, result.Error(result.ErrServiceInternal,
fmt.Sprintf("CONSUL.SERVICE: register service= %v", err))
}
err = registerHealth(cfg.Check, id, serviceName, cli)
if err != nil {
return nil, result.Error(result.ErrServiceInternal,
fmt.Sprintf("[CONSUL.SERVICE] register service check : %v", err))
}
ser := &clService{
Base: Base{
client: cli,
watcher: nil,
},
id: id,
serviceName: serviceName,
conf: cfg,
stop: make(chan bool)}
return ser, nil
}
func registerHealth(conf server.Health, id string, name string, client *consul.Client) error {
// initial register service check
asc := consul.AgentServiceCheck{Status: consul.HealthPassing}
duration := fmt.Sprintf("%ds", conf.Interval)
if conf.Ttl {
asc.TTL = duration
} else {
asc.Interval = duration
switch conf.Protocol {
case server.KGRpcProtocol:
asc.GRPC = conf.URL.To()
case server.KHttpProtocol:
asc.HTTP = conf.URL.To()
case server.KTCPProtocol:
asc.TCP = conf.URL.To()
}
}
check := &consul.AgentCheckRegistration{
ID: id,
ServiceID: id,
Name: name,
AgentServiceCheck: asc,
}
return client.Agent().CheckRegister(check)
}
// Start
/**
* Service interface
* 使用 goroutine go Start
*/
func (s *clService) Start() error {
system.ChildRunning(s.keepAlive)
return nil
}
// Stop Service interface
func (s *clService) Stop() {
s.stop <- true
}
func (s *clService) keepAlive() {
exit := system.Exit()
ticker := time.NewTicker(time.Second * s.conf.Check.Interval)
for {
select {
case <-s.stop:
s.revoke()
logger.Error("Consul.service: server stop")
return
case <-ticker.C:
if err := s.client.Agent().UpdateTTL(s.id, "", consul.HealthPassing); err != nil {
logger.Error("Consul.service: update ttl err= %v", err)
}
case <-exit.Done():
s.stop <- true
case <-system.Closed():
s.stop <- true
}
}
}
func (s *clService) revoke() {
err := s.client.Agent().ServiceDeregister(s.id)
if err != nil {
logger.Error("Consul.service: deregister service err= %v", err)
}
err = s.client.Agent().CheckDeregister(s.id)
if err != nil {
logger.Error("Consul.service: deregister check err= %v", err)
}
logger.Info("Consul.service: deregister")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.9.14

搜索帮助