Compare commits

...

2 Commits

Author SHA1 Message Date
cb37409593 feat 模块增加after start 2026-01-20 12:11:28 +08:00
5b20da3af1 feat grpc模块 2026-01-19 10:53:15 +08:00
6 changed files with 19 additions and 51 deletions

View File

@@ -6,7 +6,9 @@ import (
"git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/module" "git.hlsq.asia/mmorpg/service-common/module"
"git.hlsq.asia/mmorpg/service-scene/config" "git.hlsq.asia/mmorpg/service-scene/config"
"git.hlsq.asia/mmorpg/service-scene/internal/grpc_server"
"github.com/judwhite/go-svc" "github.com/judwhite/go-svc"
"sync"
) )
type Program struct { type Program struct {
@@ -20,7 +22,7 @@ func (p *Program) Init(_ svc.Environment) error {
} }
p.moduleList = append(p.moduleList, base) p.moduleList = append(p.moduleList, base)
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB)) p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB))
p.moduleList = append(p.moduleList, &ModuleGrpcServer{}) p.moduleList = append(p.moduleList, (&module.Grpc{}).Bind(grpc_server.NewServer(config.Get().Serve.Grpc)))
p.moduleList = append(p.moduleList, (&module.Prometheus{}).Bind(config.Get().Metric)) p.moduleList = append(p.moduleList, (&module.Prometheus{}).Bind(config.Get().Metric))
p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameScene)) p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameScene))
p.moduleList = append(p.moduleList, &module.Discover{}) p.moduleList = append(p.moduleList, &module.Discover{})
@@ -38,8 +40,16 @@ func (p *Program) Init(_ svc.Environment) error {
} }
func (p *Program) Start() error { func (p *Program) Start() error {
ready := &sync.WaitGroup{}
ready.Add(len(p.moduleList))
for _, m := range p.moduleList { for _, m := range p.moduleList {
if err := m.Start(); err != nil { if err := m.Start(ready); err != nil {
return err
}
}
ready.Wait()
for _, m := range p.moduleList {
if err := m.AfterStart(); err != nil {
return err return err
} }
} }

View File

@@ -10,6 +10,7 @@ import (
// ModuleBase 基础模块,或者一些零散的模块 // ModuleBase 基础模块,或者一些零散的模块
type ModuleBase struct { type ModuleBase struct {
module.DefaultModule
} }
func (m *ModuleBase) Init() error { func (m *ModuleBase) Init() error {
@@ -24,15 +25,3 @@ func (m *ModuleBase) Init() error {
utils.InitSnowflake(int64(rand.Intn(1000))) utils.InitSnowflake(int64(rand.Intn(1000)))
return nil return nil
} }
func (m *ModuleBase) Start() error {
return nil
}
func (m *ModuleBase) Stop() error {
return nil
}
func (m *ModuleBase) Bind(_ ...any) module.Module {
return m
}

View File

@@ -1,32 +0,0 @@
package app
import (
"git.hlsq.asia/mmorpg/service-common/module"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"git.hlsq.asia/mmorpg/service-scene/config"
"git.hlsq.asia/mmorpg/service-scene/internal/grpc_server"
)
// ModuleGrpcServer Grpc服务模块
type ModuleGrpcServer struct {
server service.IService
}
func (m *ModuleGrpcServer) Init() error {
return nil
}
func (m *ModuleGrpcServer) Start() error {
m.server = grpc_server.NewServer(config.Get().Serve.Grpc.TTL)
m.server.Init(config.Get().Serve.Grpc.Address, config.Get().Serve.Grpc.Port)
return nil
}
func (m *ModuleGrpcServer) Stop() error {
m.server.Close()
return nil
}
func (m *ModuleGrpcServer) Bind(_ ...any) module.Module {
return m
}

2
go.mod
View File

@@ -3,7 +3,7 @@ module git.hlsq.asia/mmorpg/service-scene
go 1.24.0 go 1.24.0
require ( require (
git.hlsq.asia/mmorpg/service-common v0.0.0-20260117160658-22d48542a852 git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58
github.com/judwhite/go-svc v1.2.1 github.com/judwhite/go-svc v1.2.1
go.uber.org/zap v1.27.0 go.uber.org/zap v1.27.0
google.golang.org/grpc v1.77.0 google.golang.org/grpc v1.77.0

4
go.sum
View File

@@ -1,7 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260117160658-22d48542a852 h1:ehZ54MgGL3CO7KKDPxybFabIXxiQud1TGMcKjrSTcnw= git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58 h1:Bgn3rrISGqzyNA4ax5o4tiTy3tK3QcM/+tCr80fOBhE=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260117160658-22d48542a852/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw= git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw=
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

View File

@@ -1,6 +1,7 @@
package grpc_server package grpc_server
import ( import (
"git.hlsq.asia/mmorpg/service-common/config"
"git.hlsq.asia/mmorpg/service-common/discover/common" "git.hlsq.asia/mmorpg/service-common/discover/common"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service" "git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
@@ -12,12 +13,12 @@ type Server struct {
service.Base service.Base
} }
func NewServer(ttl int64) *Server { func NewServer(cfg *config.GrpcConfig) *Server {
s := &Server{ s := &Server{
Base: service.Base{ Base: service.Base{
Target: common.KeyDiscoverScene, Target: common.KeyDiscoverScene,
ServiceName: common.KeyDiscoverServiceNameScene, ServiceName: common.KeyDiscoverServiceNameScene,
EtcdTTL: ttl, Cfg: cfg,
}, },
} }
s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption