Compare commits

..

3 Commits

Author SHA1 Message Date
ayflying
bce7131d9f 开启qps 2025-04-02 15:55:43 +08:00
ayflying
a53f7b718d 修改目录 2025-04-02 15:53:05 +08:00
ayflying
75624ff0b7 使用boot统一注册执行代码 2025-04-02 15:49:42 +08:00
6 changed files with 48 additions and 36 deletions

View File

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

View File

@@ -2,11 +2,8 @@ package boot
import ( import (
v1 "github.com/ayflying/utility_go/api/system/v1" 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/ayflying/utility_go/service"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
"math"
) )
var ( var (
@@ -22,16 +19,6 @@ func Boot() (err error) {
return service.GameAct().Saves() 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 { for _, v := range _func {
v() v()

View File

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

View File

@@ -1,11 +1,15 @@
package aycache package aycache
import ( import (
v1 "github.com/ayflying/utility_go/api/system/v1"
"github.com/ayflying/utility_go/internal/boot"
"github.com/ayflying/utility_go/pkg/aycache/drive" "github.com/ayflying/utility_go/pkg/aycache/drive"
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive" drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
"github.com/ayflying/utility_go/service"
"github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/gcache"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"math"
) )
type Mod struct { type Mod struct {
@@ -22,6 +26,18 @@ var (
) )
) )
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 { func New(_name ...string) gcache.Adapter {
var cacheAdapterObj gcache.Adapter var cacheAdapterObj gcache.Adapter

View File

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

View File

@@ -1,9 +1,9 @@
package utility_go_test package utility_go_test
import ( 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/frame/g"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
"testing" "testing"