feat grpc模块

This commit is contained in:
2026-01-19 10:53:13 +08:00
parent 22d48542a8
commit a47557920c
3 changed files with 47 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package service
import (
"fmt"
"git.hlsq.asia/mmorpg/service-common/config"
"git.hlsq.asia/mmorpg/service-common/discover"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/utils"
@@ -14,7 +15,7 @@ import (
)
type IService interface {
Init(addr string, port int32)
Init()
Close()
}
@@ -23,7 +24,7 @@ type Base struct {
ServiceName string
SID string
Serve *grpc.Server
EtcdTTL int64
Cfg *config.GrpcConfig
OnCustomGrpcServerOption func() []grpc.ServerOption
OnInit func(serve *grpc.Server)
OnClose func()
@@ -31,7 +32,7 @@ type Base struct {
wg *sync.WaitGroup
}
func (s *Base) Init(addr string, port int32) {
func (s *Base) Init() {
s.wg = &sync.WaitGroup{}
s.wg.Add(1)
s.SID = utils.SnowflakeInstance().Generate().String()
@@ -41,7 +42,7 @@ func (s *Base) Init(addr string, port int32) {
defer discover.UnRegisterGrpcServer(s.SID)
// 监听端口
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Cfg.Port))
if err != nil {
log.Errorf("%v ListenPort err: %v", s.Target, err)
return
@@ -63,7 +64,7 @@ func (s *Base) Init(addr string, port int32) {
s.OnInit(s.Serve)
// 服务注册
if err = discover.RegisterGrpcServer(s.Target, s.SID, fmt.Sprintf("%v:%d", addr, port), s.EtcdTTL); err != nil {
if err = discover.RegisterGrpcServer(s.Target, s.SID, fmt.Sprintf("%v:%d", s.Cfg.Address, s.Cfg.Port), s.Cfg.TTL); err != nil {
log.Errorf("%v RegisterGrpcServer err: %v", s.Target, err)
return
}