Compare commits

...

2 Commits

Author SHA1 Message Date
c0f6cd90c6 feat 模块增加after start 2026-01-20 12:11:26 +08:00
5aeff7c786 feat grpc模块 2026-01-19 10:53:12 +08:00
12 changed files with 47 additions and 72 deletions

View File

@@ -6,7 +6,10 @@ import (
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/module"
"git.hlsq.asia/mmorpg/service-gateway/config"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/grpc_server"
"github.com/judwhite/go-svc"
"sync"
)
type Program struct {
@@ -22,8 +25,8 @@ func (p *Program) Init(_ svc.Environment) error {
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB))
p.moduleList = append(p.moduleList, &ModuleWebServer{})
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
p.moduleList = append(p.moduleList, &ModuleGrpcServer{})
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
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.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway))
p.moduleList = append(p.moduleList, &module.Discover{})
@@ -41,11 +44,20 @@ func (p *Program) Init(_ svc.Environment) error {
}
func (p *Program) Start() error {
ready := &sync.WaitGroup{}
ready.Add(len(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
}
}
global.ServiceStatus = global.ServiceStatusReady
log.Infof(fmt.Sprintf("%v Start successful...", config.Get().App.Name))
return nil

View File

@@ -10,6 +10,7 @@ import (
// ModuleBase 基础模块,或者一些零散的模块
type ModuleBase struct {
module.DefaultModule
}
func (m *ModuleBase) Init() error {
@@ -24,15 +25,3 @@ func (m *ModuleBase) Init() error {
utils.InitSnowflake(int64(rand.Intn(1000)))
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-gateway/config"
"git.hlsq.asia/mmorpg/service-gateway/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
}

View File

@@ -5,10 +5,12 @@ import (
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
"runtime"
"sync"
)
// ModuleLoginQueue 登录队列模块
type ModuleLoginQueue struct {
module.DefaultModule
login *login.Login
queueUp *login.QueueUp
}
@@ -19,8 +21,9 @@ func (m *ModuleLoginQueue) Init() error {
return nil
}
func (m *ModuleLoginQueue) Start() error {
func (m *ModuleLoginQueue) Start(ready *sync.WaitGroup) error {
m.login.Start(runtime.NumCPU())
ready.Done()
return nil
}
@@ -28,7 +31,3 @@ func (m *ModuleLoginQueue) Stop() error {
m.login.Stop()
return nil
}
func (m *ModuleLoginQueue) Bind(_ ...any) module.Module {
return m
}

View File

@@ -14,6 +14,7 @@ import (
// ModuleWebServer Web服务模块
type ModuleWebServer struct {
module.DefaultModule
wg *sync.WaitGroup
server *http.Server
}
@@ -23,7 +24,7 @@ func (m *ModuleWebServer) Init() error {
return nil
}
func (m *ModuleWebServer) Start() error {
func (m *ModuleWebServer) Start(ready *sync.WaitGroup) error {
m.wg.Add(1)
go func() {
defer m.wg.Done()
@@ -31,6 +32,7 @@ func (m *ModuleWebServer) Start() error {
Addr: fmt.Sprintf("%v:%v", config.Get().Serve.Http.Address, config.Get().Serve.Http.Port),
Handler: http_gateway.InitRouter(),
}
ready.Done()
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("http server failed: %v", err.Error())
}
@@ -46,7 +48,3 @@ func (m *ModuleWebServer) Stop() error {
m.wg.Wait()
return nil
}
func (m *ModuleWebServer) Bind(_ ...any) module.Module {
return m
}

View File

@@ -14,6 +14,7 @@ import (
// ModuleWebsocketServer Websocket服务模块
type ModuleWebsocketServer struct {
module.DefaultModule
wg *sync.WaitGroup
server *websocket.WSServer
}
@@ -28,10 +29,11 @@ func (m *ModuleWebsocketServer) Init() error {
return nil
}
func (m *ModuleWebsocketServer) Start() error {
func (m *ModuleWebsocketServer) Start(ready *sync.WaitGroup) error {
m.wg.Add(1)
go func() {
defer m.wg.Done()
ready.Done()
_ = m.server.Run(
fmt.Sprintf("tcp4://0.0.0.0:%v", config.Get().Serve.Socket.Web.Port),
true,
@@ -55,7 +57,3 @@ func (m *ModuleWebsocketServer) Stop() error {
m.wg.Wait()
return nil
}
func (m *ModuleWebsocketServer) Bind(_ ...any) module.Module {
return m
}

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.24.0
require (
bou.ke/monkey v1.0.2
git.hlsq.asia/mmorpg/service-common v0.0.0-20260117160658-22d48542a852
git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58
github.com/alicebob/miniredis/v2 v2.35.0
github.com/gin-contrib/cors v1.7.6
github.com/gin-gonic/gin v1.11.0

4
go.sum
View File

@@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
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-20260117160658-22d48542a852/go.mod h1:Dazg+4woCv9Jk7jgT2qUSGWhZOXx/0WYfJO+FCUDyhw=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260120040718-1edebb439c58 h1:Bgn3rrISGqzyNA4ax5o4tiTy3tK3QcM/+tCr80fOBhE=
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/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=

View File

@@ -1,5 +1,17 @@
package global
var ServiceStatus = ServiceStatusStarting
type ServiceStatusCode int32
const (
ServiceStatusStarting ServiceStatusCode = iota // 启动中
ServiceStatusReady // 就绪
ServiceStatusStopping // 停止中
)
var GatewaySID string
const (
KeyGatewayAccessToken = "gateway:token:access:%v"
KeyGatewayRefreshToken = "gateway:token:refresh:%v"

View File

@@ -1,10 +1,11 @@
package grpc_server
import (
"git.hlsq.asia/mmorpg/service-common/config"
"git.hlsq.asia/mmorpg/service-common/discover/common"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"google.golang.org/grpc"
)
@@ -13,12 +14,12 @@ type Server struct {
service.Base
}
func NewServer(ttl int64) *Server {
func NewServer(cfg *config.GrpcConfig) *Server {
s := &Server{
Base: service.Base{
Target: common.KeyDiscoverGateway,
ServiceName: common.KeyDiscoverServiceNameGateway,
EtcdTTL: ttl,
Cfg: cfg,
},
}
s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption
@@ -32,7 +33,7 @@ func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
}
func (s *Server) OnInit(serve *grpc.Server) {
client.GatewaySID = s.SID
global.GatewaySID = s.SID
grpc_pb.RegisterGatewayServer(serve, s)
}

View File

@@ -13,8 +13,6 @@ import (
"time"
)
var GatewaySID string
type Client struct {
sync.WaitGroup
conn socket.ISocketConn // Socket

View File

@@ -45,7 +45,7 @@ func (c *Client) handle(event Event) {
if c.Status == 0 {
c.Status = 1
UserMgr.Add(c.USN, c)
redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, GatewaySID)
redis.GetClient().HSet(c.ctx, fmt.Sprintf(global.KeyGatewayInfo, c.USN), global.HFieldInfoGatewaySID, global.GatewaySID)
c.WriteMessage(ss_pb.MessageID_MESSAGE_ID_LOGIN_SUCCESS, &ss_pb.S2C_LoginSuccess{
InstanceID: 1,
})
@@ -61,7 +61,7 @@ func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
}
resp, err := client.Enter(c.ctx, &grpc_pb.EnterReq{
USN: c.USN,
GatewaySID: GatewaySID,
GatewaySID: global.GatewaySID,
InstanceID: msg.InstanceID,
})
if err != nil {