使用boot统一注册执行代码

This commit is contained in:
ayflying
2025-04-02 15:49:42 +08:00
parent 743d232c38
commit 75624ff0b7
6 changed files with 49 additions and 43 deletions

View File

@@ -1,6 +1,7 @@
package elasticsearch
import (
"github.com/ayflying/utility_go/internal/boot"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
@@ -14,17 +15,20 @@ const (
quoteChar = "`"
)
func Init() {
var (
err error
driverObj = New()
driverNames = g.SliceStr{"es", "elasticsearch"}
)
for _, driverName := range driverNames {
if err = gdb.Register(driverName, driverObj); err != nil {
panic(err)
func init() {
boot.AddFunc(func() {
var (
err error
driverObj = New()
driverNames = g.SliceStr{"es", "elasticsearch"}
)
for _, driverName := range driverNames {
if err = gdb.Register(driverName, driverObj); err != nil {
panic(err)
}
}
}
})
}
// New create and returns a driver that implements gdb.Driver, which supports operations for MySQL.

View File

@@ -2,11 +2,8 @@ package boot
import (
v1 "github.com/ayflying/utility_go/api/system/v1"
"github.com/ayflying/utility_go/drivers/db/elasticsearch"
"github.com/ayflying/utility_go/pkg/aycache"
"github.com/ayflying/utility_go/service"
"github.com/gogf/gf/v2/os/gctx"
"math"
)
var (
@@ -22,16 +19,6 @@ func Boot() (err error) {
return service.GameAct().Saves()
})
//初始化ES
elasticsearch.Init()
//初始化指标
service.SystemCron().AddCron(v1.CronType_MINUTE, func() error {
aycache.QPS.Set(math.Round(float64(aycache.QPSCount) / 60))
aycache.QPSCount = 0
return nil
})
//初始化自启动方法
for _, v := range _func {
v()

View File

@@ -1,6 +1,7 @@
package ip2region
import (
"github.com/ayflying/utility_go/internal/boot"
"github.com/ayflying/utility_go/service"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
@@ -19,13 +20,16 @@ type sIp2region struct {
}
func New() *sIp2region {
s := &sIp2region{}
s.Load()
return s
return &sIp2region{}
}
func init() {
service.RegisterIp2Region(New())
boot.AddFunc(func() {
service.Ip2Region().Load()
})
}
// Load 加载到内存中
@@ -37,6 +41,7 @@ func (s *sIp2region) Load() {
var dbPath = "/runtime/library/ip2region.xdb"
if gfile.IsEmpty(dbPath) {
g.Log().Debug(ctx, "等待下载ip库文件")
//下载文件
putData, err2 := g.Client().Discovery(nil).
Get(ctx, "https://resource.luoe.cn/attachment/ip2region.xdb")

View File

@@ -4,8 +4,6 @@ import (
"github.com/ayflying/utility_go/pkg/aycache/drive"
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
"github.com/gogf/gf/v2/os/gcache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type Mod struct {
@@ -14,14 +12,26 @@ type Mod struct {
var (
QPSCount int
QPS = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "Cache_QPS",
Help: "当前缓存QPS数量",
},
)
//QPS = promauto.NewGauge(
// prometheus.GaugeOpts{
// Name: "Cache_QPS",
// Help: "当前缓存QPS数量",
// },
//)
)
func init() {
//boot.AddFunc(func() {
// //初始化指标
// service.SystemCron().AddCron(v1.CronType_MINUTE, func() error {
// QPS.Set(math.Round(float64(QPSCount) / 60))
// QPSCount = 0
// return nil
// })
//})
}
func New(_name ...string) gcache.Adapter {
var cacheAdapterObj gcache.Adapter

View File

@@ -4,6 +4,7 @@ import (
"github.com/ayflying/utility_go/config"
"github.com/ayflying/utility_go/internal/boot"
_ "github.com/ayflying/utility_go/internal/logic"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
@@ -13,12 +14,11 @@ var (
)
func init() {
go func() {
// 初始化配置
var err = boot.Boot()
if err != nil {
panic(err)
}
}()
g.Log().Debug(ctx, "utility_go init启动完成")
// 初始化配置
var err = boot.Boot()
if err != nil {
panic(err)
}
}

View File

@@ -1,9 +1,9 @@
package utility_go_test
import (
"github.com/ayflying/utility_go/internal/boot"
_ "github.com/ayflying/utility_go/internal/logic"
//_ "github.com/ayflying/utility_go/internal/logic"
"github.com/ayflying/utility_go/internal/boot"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"testing"