1 Star 0 Fork 0

micro-tools/micro

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
base.go 3.57 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-06-05 23:58 +08:00 . init
package base
import (
"context"
"errors"
"fmt"
"gitee.com/micro-tools/micro/module"
gsnowflake "gitee.com/micro-tools/wf/extend/utils/gSnowflake"
"gitee.com/micro-tools/wf/extend/utils/gmicro/server"
"gitee.com/micro-tools/wf/extend/utils/gmicro/service"
"gitee.com/micro-tools/wf/os/gtimer"
"gitee.com/micro-tools/wf/util/gconv"
"gitee.com/micro-tools/wf/util/grand"
"google.golang.org/grpc"
"os"
"time"
)
type Module struct {
context.Context
serviceStopeds chan bool
exit context.CancelFunc
App module.App
subclass module.RPCModule
service service.Service
}
func (m *Module) OnInit(subclass module.RPCModule, app module.App, opt ...server.Option) {
m.App = app
m.subclass = subclass
opts := server.Options{
Metadata: map[string]string{},
}
for _, o := range opt {
o(&opts)
}
if opts.Registry == nil {
opt = append(opt, server.Registry(app.Registry()))
}
if opts.RegisterInterval == 0 {
opt = append(opt, server.RegisterInterval(app.Options().RegisterInterval))
}
if opts.RegisterTTL == 0 {
opt = append(opt, server.RegisterTTL(app.Options().RegisterTTL))
}
if opts.Port == 0 {
opts.Port = grand.N(1000, 4000)
opt = append(opt, server.Port(opts.Port))
}
if len(opts.Address) == 0 {
opt = append(opt, server.Address(fmt.Sprintf(":%d", opts.Port)))
}
if len(opts.Name) == 0 {
opt = append(opt, server.Name(subclass.GetType()))
}
if len(opts.ID) == 0 {
opt = append(opt, server.ID(gconv.String(gsnowflake.GetId())))
}
sv := server.NewServer(opt...)
sv.OnInit(m.App.Logger())
hostname, _ := os.Hostname()
sv.Options().Metadata["hostname"] = hostname
sv.Options().Metadata["pid"] = fmt.Sprintf("%d", os.Getpid())
ctx, cancel := context.WithCancel(context.Background())
m.exit = cancel
m.serviceStopeds = make(chan bool)
m.service = service.NewService(
service.Server(sv),
service.Bind(opts.RPCBind),
service.Tracing(app.Options().Tracing.Status),
service.RegisterTTL(app.Options().RegisterTTL),
service.RegisterInterval(app.Options().RegisterInterval),
service.Context(ctx),
)
if app.Options().RegistryEnable {
app.Logger().Info("registry service is enable")
go func() {
err := m.service.Run()
if err != nil {
m.App.Logger().Warningf("service run fail id(%s) error(%s)", m.GetServerID(), err)
}
close(m.serviceStopeds)
gtimer.SetInterval(time.Second*10, func() {
os.Exit(-1)
})
}()
} else {
app.Logger().Info("registry service is disabled")
go func() {
select {
case <-m.service.Options().Context.Done():
}
close(m.serviceStopeds)
gtimer.SetInterval(time.Second*10, func() {
os.Exit(-1)
})
}()
}
}
func (m *Module) GetApp() module.App {
return m.App
}
func (m *Module) GetServer() server.Server {
return m.service.Server()
}
func (m *Module) Call(ctx context.Context, serviceName string, fnc func(context.Context, *grpc.ClientConn) error) error {
return m.App.Call(ctx, serviceName, fnc)
}
func (m *Module) GetClient(serviceName string) (error, *grpc.ClientConn) {
serverSseeion, err := m.App.GetRouteServer(serviceName)
if err != nil {
return err, nil
}
client := serverSseeion.GetClient()
if client == nil {
return errors.New(fmt.Sprintf("%s连接未获取", serviceName)), nil
}
return nil, client
}
func (m *Module) OnDestroy() {
//注销模块
//一定别忘了关闭RPC
m.exit()
select {
case <-m.serviceStopeds:
//等待注册中心注销完成
}
_ = m.GetServer().OnDestroy()
}
func (m *Module) GetServerID() string {
if m.service != nil && m.service.Server() != nil {
return m.service.Server().ID()
}
return "no server"
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/micro-tools/micro.git
git@gitee.com:micro-tools/micro.git
micro-tools
micro
micro
v1.0.2

搜索帮助