feat grpc模块
This commit is contained in:
@@ -53,11 +53,7 @@ type RedisConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ServeConfig struct {
|
type ServeConfig struct {
|
||||||
Grpc *struct {
|
Grpc *GrpcConfig `yaml:"grpc"`
|
||||||
Address string `yaml:"address"`
|
|
||||||
Port int32 `yaml:"port"`
|
|
||||||
TTL int64 `yaml:"ttl"`
|
|
||||||
} `yaml:"grpc"`
|
|
||||||
Socket *struct {
|
Socket *struct {
|
||||||
Web *AddressConfig `yaml:"web"`
|
Web *AddressConfig `yaml:"web"`
|
||||||
Raw *AddressConfig `yaml:"raw"`
|
Raw *AddressConfig `yaml:"raw"`
|
||||||
@@ -65,6 +61,12 @@ type ServeConfig struct {
|
|||||||
Http *AddressConfig `yaml:"http"`
|
Http *AddressConfig `yaml:"http"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GrpcConfig struct {
|
||||||
|
Address string `yaml:"address"`
|
||||||
|
Port int32 `yaml:"port"`
|
||||||
|
TTL int64 `yaml:"ttl"`
|
||||||
|
}
|
||||||
|
|
||||||
type AddressConfig struct {
|
type AddressConfig struct {
|
||||||
Address string `yaml:"address"`
|
Address string `yaml:"address"`
|
||||||
Port int32 `yaml:"port"`
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.hlsq.asia/mmorpg/service-common/config"
|
||||||
"git.hlsq.asia/mmorpg/service-common/discover"
|
"git.hlsq.asia/mmorpg/service-common/discover"
|
||||||
"git.hlsq.asia/mmorpg/service-common/log"
|
"git.hlsq.asia/mmorpg/service-common/log"
|
||||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||||
@@ -14,7 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type IService interface {
|
type IService interface {
|
||||||
Init(addr string, port int32)
|
Init()
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ type Base struct {
|
|||||||
ServiceName string
|
ServiceName string
|
||||||
SID string
|
SID string
|
||||||
Serve *grpc.Server
|
Serve *grpc.Server
|
||||||
EtcdTTL int64
|
Cfg *config.GrpcConfig
|
||||||
OnCustomGrpcServerOption func() []grpc.ServerOption
|
OnCustomGrpcServerOption func() []grpc.ServerOption
|
||||||
OnInit func(serve *grpc.Server)
|
OnInit func(serve *grpc.Server)
|
||||||
OnClose func()
|
OnClose func()
|
||||||
@@ -31,7 +32,7 @@ type Base struct {
|
|||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Base) Init(addr string, port int32) {
|
func (s *Base) Init() {
|
||||||
s.wg = &sync.WaitGroup{}
|
s.wg = &sync.WaitGroup{}
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
s.SID = utils.SnowflakeInstance().Generate().String()
|
s.SID = utils.SnowflakeInstance().Generate().String()
|
||||||
@@ -41,7 +42,7 @@ func (s *Base) Init(addr string, port int32) {
|
|||||||
defer discover.UnRegisterGrpcServer(s.SID)
|
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 {
|
if err != nil {
|
||||||
log.Errorf("%v ListenPort err: %v", s.Target, err)
|
log.Errorf("%v ListenPort err: %v", s.Target, err)
|
||||||
return
|
return
|
||||||
@@ -63,7 +64,7 @@ func (s *Base) Init(addr string, port int32) {
|
|||||||
s.OnInit(s.Serve)
|
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)
|
log.Errorf("%v RegisterGrpcServer err: %v", s.Target, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user