最近在公司内封装的go mvc框架代码接近收尾,核心功能与大部分常用SDK均以实现,压测效果也符合预期,所以我整理了一下整个框架的代码,将一个可直接运行的版本分享给大家做学习使用。
作为个人来说,也许安装一下gin、gorm等第三方类库,然后简单代码初始化一下就可以开发了。
但是作为一个企业广泛推广的框架,其必须具备标准的封装与可配置化设计,确保所有项目均可以基于框架快速创立并且开发风格一致。
因此,企业级框架有一些核心问题是必须解决的:
- 主流SDK的二次封装:包括mysql、redis、http等,其目的是约束业务研发同学只有一种写法,代码可读性与可维护性更强。
- 调用链trace埋点(对接了大众点评cat):包括mysql、redis等,其目的是对接APM系统,用于业务报警、监听、调用链分析,因此也必然要求上述的SDK二次封装,以便侵入式埋点。
- 中间件附加功能:通过中间件模式,为框架增加panic捕获、请求超时控制,提供请求级的context透传,以便灵活订制请求处理流程。
- 支持web和cron模式:同一套框架,同时满足web模式与cron定时任务模式,提供类似于java quartz的相似体验。
下面是框架的简单用法介绍,更多细节大家可以下载运行与阅读代码。
web模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package main import ( "flag" "fmt" "github.com/gin-gonic/gin" "github.com/owenliang/myf-go/app" "github.com/owenliang/myf-go/mcontext" "os" ) func Test(ctx *mcontext.MyfContext) { ctx.JSON(200, &gin.H{ "error_code": 0, "error_msg": "", "data": nil, }) } func main() { flag.Parse() if myfApp, err := app.New(); err == nil { myfApp.Gin.GET("/test", myfApp.WithMyfContext(Test)) myfApp.Run() } else { fmt.Fprintln(os.Stderr, err) } } |
cron模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package main import ( "flag" "fmt" "github.com/owenliang/myf-go/cron" "github.com/owenliang/myf-go/cron/middlewares" "github.com/owenliang/myf-go/mcontext" "time" ) func task1(myfCtx *mcontext.MyfContext) { // <- myfCtx.Done() } func main() { flag.Parse() if myfCron, err := cron.New(); err == nil { myfCron.AddJob("mytask", "* * * * *", middlewares.WithTimeout(5 * time.Second), myfCron.WithMyfContext(task1)) myfCron.Run() } else { fmt.Println(err) } } |
配置文件
采用toml格式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
domain = "myf-go.com" debug = 1 # web模式 [App] listen = "0.0.0.0:8087" # cron模式 [Cron] waitGraceExit = 5000 # cat配置 [Cat] isOpen = true #http-client配置 [HttpClient] timeout = 6000 #mysql配置 [Mysql] #[[Mysql.Group]] # name = "default" # database = "gin" # username = "root" # password = "baidu@123" # [Mysql.Group.Master] # maxConn = 10 # idleConn = 5 # idleTime = 5000 # [[Mysql.Group.Master.Instance]] # host = "127.0.0.1" # port = 3306 # [Mysql.Group.Slave] # maxConn = 10 # idleConn = 5 # idleTime = 5000 # [[Mysql.Group.Slave.Instance]] # host = "127.0.0.1" # port = 3306 #redis配置 [Redis] #[[Redis.Group]] # name = "default" # password = "" # [Redis.Group.Master] # dialTimeout = 5000 # readTimeout = 5000 # writeTimeout = 5000 # [[Redis.Group.Master.Instance]] # host = "127.0.0.1" # port = 6379 # [Redis.Group.Slave] # dialTimeout = 5000 # readTimeout = 5000 # writeTimeout = 5000 # [[Redis.Group.Slave.Instance]] # host = "127.0.0.1" # port = 6379 [Mongo] #[[Mongo.Group]] # name = "default" # username = "default" # password = "" # database = "DefaultDB" # maxPoolSize = 10 # minPoolSize = 5 # [[Mongo.Group.Instance]] # host = "default_router01" # port = 27017 # [[Mongo.Group.Instance]] # host = "default_router02" # port = 27017 |
完整代码
地址:https://github.com/owenliang/myf-go,具体使用方法见README。
源码阅读入口:
- web模式:https://github.com/owenliang/myf-go/blob/master/test/app/main.go
- cron模式:https://github.com/owenliang/myf-go/blob/master/test/cron/main.go
项目依赖了gin、gorm、大众点评cat(代码已fork私有化管理、上报性能方面正在做代码改造)。
如果文章帮助您解决了工作难题,您可以帮我点击屏幕上的任意广告,或者赞助少量费用来支持我的持续创作,谢谢~

鱼儿男神