使用boot统一注册执行代码
This commit is contained in:
@@ -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,7 +15,8 @@ const (
|
|||||||
quoteChar = "`"
|
quoteChar = "`"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func init() {
|
||||||
|
boot.AddFunc(func() {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
driverObj = New()
|
driverObj = New()
|
||||||
@@ -25,6 +27,8 @@ func Init() {
|
|||||||
panic(err)
|
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.
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -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 加载到内存中
|
||||||
@@ -37,6 +41,7 @@ func (s *sIp2region) Load() {
|
|||||||
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")
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"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/gogf/gf/v2/os/gcache"
|
"github.com/gogf/gf/v2/os/gcache"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mod struct {
|
type Mod struct {
|
||||||
@@ -14,14 +12,26 @@ type Mod struct {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
QPSCount int
|
QPSCount int
|
||||||
QPS = promauto.NewGauge(
|
//QPS = promauto.NewGauge(
|
||||||
prometheus.GaugeOpts{
|
// prometheus.GaugeOpts{
|
||||||
Name: "Cache_QPS",
|
// Name: "Cache_QPS",
|
||||||
Help: "当前缓存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 {
|
func New(_name ...string) gcache.Adapter {
|
||||||
|
|
||||||
var cacheAdapterObj gcache.Adapter
|
var cacheAdapterObj gcache.Adapter
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
Reference in New Issue
Block a user