Merge pull request #21 from bufanyun/v2.0

增加链路追踪配置,优化服务退出流程,gf版本升级到v2.4.2
This commit is contained in:
maxbad 2023-06-06 19:30:57 +08:00 committed by GitHub
commit 6c242cece7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 94 additions and 33 deletions

View File

@ -15,7 +15,8 @@ require (
github.com/go-pay/gopay v1.5.91
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.4.1
github.com/gogf/gf/contrib/nosql/redis/v2 v2.4.1
github.com/gogf/gf/v2 v2.4.1
github.com/gogf/gf/contrib/trace/jaeger/v2 v2.4.2
github.com/gogf/gf/v2 v2.4.2
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/gorilla/websocket v1.5.0
github.com/kayon/iploc v0.0.0-20200312105652-bda3e968a794
@ -108,6 +109,7 @@ require (
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/otel v1.15.1 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.7.0 // indirect
go.opentelemetry.io/otel/sdk v1.15.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
go.uber.org/atomic v1.7.0 // indirect

View File

@ -172,8 +172,11 @@ github.com/gogf/gf/contrib/drivers/mysql/v2 v2.4.1 h1:ra6aO6+KUufCg9eK52oj6Q2SnC
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.4.1/go.mod h1:xubKxCvgbPt9r4yD3ecJoP3DC3sHqcJoMyS0eX837Zg=
github.com/gogf/gf/contrib/nosql/redis/v2 v2.4.1 h1:PzAu6efos26gXyBO+aaNZ40hlNsA2T/qLOgP2RgIFaM=
github.com/gogf/gf/contrib/nosql/redis/v2 v2.4.1/go.mod h1:0t7pBtXdfuemskzkdxyC2y9nUG63wdldvGRCaQiJ+MM=
github.com/gogf/gf/v2 v2.4.1 h1:snsuvDhNFiRoAuWBbKfIIng0KyMaRA87Qr03GLir5j8=
github.com/gogf/gf/contrib/trace/jaeger/v2 v2.4.2 h1:ib0xrG6SIxOWigUzoFo7p/Nb41CrC513pMFvcN4MAWg=
github.com/gogf/gf/contrib/trace/jaeger/v2 v2.4.2/go.mod h1:Ur1/JBxoFOqRTlFDiOjCINZiAOdEIJRw6nZ69zIceQ8=
github.com/gogf/gf/v2 v2.4.1/go.mod h1:tsbmtwcAl2chcYoq/fP9W2FZf06aw4i89X34nbSHo9Y=
github.com/gogf/gf/v2 v2.4.2 h1:31ekx+YKisG84/iEC2Ih6lB+g92+sB9R+VrfIbqfpac=
github.com/gogf/gf/v2 v2.4.2/go.mod h1:tsbmtwcAl2chcYoq/fP9W2FZf06aw4i89X34nbSHo9Y=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
@ -438,6 +441,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@ -501,6 +505,8 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
go.opentelemetry.io/otel v1.15.1 h1:3Iwq3lfRByPaws0f6bU3naAqOR1n5IeDWd9390kWHa8=
go.opentelemetry.io/otel v1.15.1/go.mod h1:mHHGEHVDLal6YrKMmk9LqC4a3sF5g+fHfrttQIB1NTc=
go.opentelemetry.io/otel/exporters/jaeger v1.7.0 h1:wXgjiRldljksZkZrldGVe6XrG9u3kYDyQmkZwmm5dI0=
go.opentelemetry.io/otel/exporters/jaeger v1.7.0/go.mod h1:PwQAOqBgqbLQRKlj466DuD2qyMjbtcPpfPfj+AqbSBs=
go.opentelemetry.io/otel/sdk v1.7.0/go.mod h1:uTEOTwaqIVuTGiJN7ii13Ibp75wJmYUDe374q6cZwUU=
go.opentelemetry.io/otel/sdk v1.15.1 h1:5FKR+skgpzvhPQHIEfcwMYjCBr14LWzs3uSqKiQzETI=
go.opentelemetry.io/otel/sdk v1.15.1/go.mod h1:8rVtxQfrbmbHKfqzpQkT5EzZMcbMBwTzNAggbEAM0KA=

View File

@ -7,7 +7,9 @@ package cmd
import (
"context"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gproc"
"hotgo/internal/consts"
"hotgo/utility/simple"
"os"
"sync"
@ -16,15 +18,27 @@ import (
var (
serverCloseSignal = make(chan struct{}, 1)
serverWg = sync.WaitGroup{}
once sync.Once
)
// signalHandlerForOverall 关闭信号处理
func signalHandlerForOverall(sig os.Signal) {
serverCloseSignal <- struct{}{}
serverCloseEvent(gctx.GetInitCtx())
}
// signalListen 信号监听
func signalListen(ctx context.Context, handler ...gproc.SigHandler) {
simple.SafeGo(ctx, func(ctx context.Context) {
gproc.AddSigHandlerShutdown(handler...)
gproc.Listen()
})
}
// serverCloseEvent 关闭事件
// 区别于服务收到退出信号后的处理,只会执行一次
func serverCloseEvent(ctx context.Context) {
once.Do(func() {
simple.Event().Call(consts.EventServerClose, ctx)
})
}

View File

@ -48,11 +48,11 @@ var (
// 注册全局中间件
group.Middleware(
service.Middleware().Ctx, // 必须第一个加载
service.Middleware().CORS,
service.Middleware().Blacklist,
service.Middleware().DemoLimit,
service.Middleware().ResponseHandler,
service.Middleware().Ctx, // 初始化请求上下文,一般需要第一个进行加载,后续中间件存在依赖关系
service.Middleware().CORS, // 跨域中间件,自动处理跨域问题
service.Middleware().Blacklist, // IP黑名单中间件如果请求IP被后台拉黑所有请求将被拒绝
service.Middleware().DemoLimit, // 演示系統操作限制当开启演示模式时所有POST请求将被拒绝
service.Middleware().ResponseHandler, // HTTP响应预处理在业务处理完成后对响应结果进行格式化和错误过滤将处理后的数据发送给请求方
)
// 注册后台路由
@ -98,9 +98,9 @@ var (
go func() {
<-serverCloseSignal
websocket.Stop()
service.TCPServer().Stop(ctx)
_ = s.Shutdown() // 主服务建议放在最后一个关闭
websocket.Stop() // 关闭websocket
service.TCPServer().Stop(ctx) // 关闭tcp服务器
_ = s.Shutdown() // 关闭http服务主服务建议放在最后一个关闭
g.Log().Debug(ctx, "http successfully closed ..")
serverWg.Done()
}()

View File

@ -0,0 +1,5 @@
package consts
const (
EventServerClose = "server.close" // 服务关闭事件
)

View File

@ -12,6 +12,7 @@ import (
"hotgo/internal/consts"
"hotgo/internal/model"
"hotgo/internal/service"
"hotgo/utility/simple"
)
// Site 基础
@ -21,7 +22,7 @@ type cSite struct{}
func (a *cSite) Index(ctx context.Context, _ *base.SiteIndexReq) (res *base.SiteIndexRes, err error) {
service.View().Render(ctx, model.View{Data: g.Map{
"name": "HotGo",
"name": simple.AppName(ctx),
"version": consts.VersionApp,
"debug": g.Cfg().MustGet(ctx, "hotgo.debug", true),
}})

View File

@ -8,6 +8,7 @@ package global
import (
"context"
"fmt"
"github.com/gogf/gf/contrib/trace/jaeger/v2"
"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
@ -22,6 +23,7 @@ import (
"hotgo/internal/model/entity"
"hotgo/internal/service"
"hotgo/utility/charset"
"hotgo/utility/simple"
"strings"
)
@ -43,6 +45,9 @@ func Init(ctx context.Context) {
RootPtah = gfile.Pwd()
fmt.Printf("欢迎使用HotGo\r\n当前运行环境%v, 运行根路径为:%v \r\nHotGo版本v%v, gf版本%v \n", SysType, RootPtah, consts.VersionApp, gf.VERSION)
// 初始化链路追踪
InitTrace(ctx)
// 设置缓存适配器
cache.SetAdapter(ctx)
@ -112,3 +117,20 @@ func LoggingServeLogHandler(ctx context.Context, in *glog.HandlerInput) {
g.Dump("LoggingServeLogHandler err:%+v", err)
}
}
// InitTrace 初始化链路追踪
func InitTrace(ctx context.Context) {
if !g.Cfg().MustGet(ctx, "jaeger.switch").Bool() {
return
}
tp, err := jaeger.Init(simple.AppName(ctx), g.Cfg().MustGet(ctx, "jaeger.endpoint").String())
if err != nil {
g.Log().Fatal(ctx, err)
}
simple.Event().Register(consts.EventServerClose, func(ctx context.Context, args ...interface{}) {
_ = tp.Shutdown(ctx)
g.Log().Debug(ctx, "jaeger closed ..")
})
}

View File

@ -13,6 +13,7 @@ import (
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/glog"
"hotgo/internal/consts"
"hotgo/utility/simple"
"reflect"
"sync"
"time"
@ -61,12 +62,13 @@ func NewServer(config *ServerConfig) (server *Server, err error) {
return
}
if config.Name == "" {
config.Name = "hotgo"
}
server = new(Server)
server.Ctx = gctx.New()
if config.Name == "" {
config.Name = simple.AppName(server.Ctx)
}
server.addr = config.Addr
server.name = config.Name
server.ln = gtcp.NewServer(server.addr, server.accept, config.Name)

View File

@ -11,8 +11,8 @@ import (
"github.com/Shopify/sarama"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gproc"
"os"
"hotgo/internal/consts"
"hotgo/utility/simple"
"time"
)
@ -109,7 +109,7 @@ func (r *KafkaMq) ListenReceiveMsgDo(topic string, receiveDo func(mqMsg MqMsg))
<-consumer.ready
Logger().Debug(ctx, "kafka consumer up and running!...")
gproc.AddSigHandlerShutdown(func(sig os.Signal) {
simple.Event().Register(consts.EventServerClose, func(ctx context.Context, args ...interface{}) {
Logger().Debug(ctx, "kafka consumer close...")
cancel()
if err = r.consumerIns.Close(); err != nil {
@ -202,7 +202,7 @@ func doRegisterKafkaProducer(connOpt KafkaConfig, mqIns *KafkaMq) (err error) {
return
}
gproc.AddSigHandlerShutdown(func(sig os.Signal) {
simple.Event().Register(consts.EventServerClose, func(ctx context.Context, args ...interface{}) {
g.Log().Debug(ctx, "kafka producer AsyncClose...")
mqIns.producerIns.AsyncClose()
})

View File

@ -25,6 +25,7 @@ import (
"hotgo/internal/model/input/sysin"
"hotgo/internal/service"
"hotgo/utility/charset"
"hotgo/utility/simple"
"hotgo/utility/useragent"
"hotgo/utility/validate"
"time"
@ -171,7 +172,7 @@ func (s *sSysEmsLog) Send(ctx context.Context, in sysin.SendEmsInp) (err error)
subject, ok := consts.EmsSubjectMap[in.Event]
if !ok {
subject = "HotGo"
subject = simple.AppName(ctx)
}
err = ems.Send(config, in.Email, subject, in.Content)
@ -235,7 +236,7 @@ func (s *sSysEmsLog) newView(ctx context.Context, in sysin.SendEmsInp, config *m
if basic == nil {
basic = new(model.BasicConfig)
basic.Name = "HotGo"
basic.Name = simple.AppName(ctx)
basic.Domain = "https://hotgo.facms.cn"
basic.Logo = "http://bufanyun.cn-bj.ufileos.com/haoka/attachment/images/2023-02-04/cq9kf7s66jt7hkpvbh.png"
basic.SystemOpen = true

View File

@ -26,6 +26,7 @@ import (
"hotgo/internal/model/input/sysin"
"hotgo/internal/service"
"hotgo/utility/excel"
"hotgo/utility/simple"
"hotgo/utility/validate"
)
@ -61,7 +62,7 @@ func (s *sSysLog) Export(ctx context.Context, in sysin.LogListInp) (err error) {
var (
titleList = []string{"ID", "应用", "提交类型", "模块", "提交url", "ip地址", "报错code", "报错信息", "对外id", "请求耗时", "创建时间", "用户", "访问地"}
fileName = "访问日志导出-" + gctx.CtxId(ctx) + ".xlsx"
sheetName = "HotGo"
sheetName = simple.AppName(ctx)
exportList []exportImage
row exportImage
)

View File

@ -26,7 +26,7 @@ func (s *sTCPServer) Start(ctx context.Context) {
g.Log().Debug(ctx, "TCPServer start..")
server, err := tcp.NewServer(&tcp.ServerConfig{
Name: "hotgo",
Name: simple.AppName(ctx),
Addr: g.Cfg().MustGet(ctx, "tcp.server.address").String(),
})

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,7 @@
# 应用名称
appName: "hotgo"
# hotgo配置
hotgo:
# debug开关开启后接口出现错误时会向前端输出堆栈信息默认为true
@ -231,6 +235,12 @@ database:
Prefix: "hg_"
# 链路追踪
jaeger:
switch: false # 链路追踪开关默认为false
endpoint: "127.0.0.1:6831" # jaeger-agent上报地址
# 生成代码
hggen:
allowedIPs: ["127.0.0.1", "*"] # 白名单*代表所有只有允许的IP后台才能使用生成代码功能

View File

@ -14,8 +14,8 @@ type sEvent struct {
var event *sEvent
// InstanceEvent 事件实例
func InstanceEvent() *sEvent {
// Event 事件实例
func Event() *sEvent {
if event == nil {
event = &sEvent{
list: make(map[string][]EventFunc),

View File

@ -18,6 +18,11 @@ import (
"hotgo/utility/encrypt"
)
// AppName 应用名称
func AppName(ctx context.Context) string {
return g.Cfg().MustGet(ctx, "appName", "hotgo").String()
}
// FilterMaskDemo 过滤演示环境下的配置隐藏字段
func FilterMaskDemo(ctx context.Context, src g.Map) g.Map {
if src == nil {