feat grpc模块
This commit is contained in:
@@ -53,11 +53,7 @@ type RedisConfig struct {
|
||||
}
|
||||
|
||||
type ServeConfig struct {
|
||||
Grpc *struct {
|
||||
Address string `yaml:"address"`
|
||||
Port int32 `yaml:"port"`
|
||||
TTL int64 `yaml:"ttl"`
|
||||
} `yaml:"grpc"`
|
||||
Grpc *GrpcConfig `yaml:"grpc"`
|
||||
Socket *struct {
|
||||
Web *AddressConfig `yaml:"web"`
|
||||
Raw *AddressConfig `yaml:"raw"`
|
||||
@@ -65,6 +61,12 @@ type ServeConfig struct {
|
||||
Http *AddressConfig `yaml:"http"`
|
||||
}
|
||||
|
||||
type GrpcConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Port int32 `yaml:"port"`
|
||||
TTL int64 `yaml:"ttl"`
|
||||
}
|
||||
|
||||
type AddressConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Port int32 `yaml:"port"`
|
||||
|
||||
34
module/grpc.go
Normal file
34
module/grpc.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
||||
)
|
||||
|
||||
// Grpc Grpc模块
|
||||
type Grpc struct {
|
||||
server service.IService
|
||||
}
|
||||
|
||||
func (m *Grpc) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Grpc) Start() error {
|
||||
m.server.Init()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Grpc) Stop() error {
|
||||
m.server.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Grpc) Bind(data ...any) Module {
|
||||
if data == nil || len(data) == 0 {
|
||||
return m
|
||||
}
|
||||
if ser, ok := data[0].(service.IService); ok {
|
||||
m.server = ser
|
||||
}
|
||||
return m
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user