yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图1
中文 / English

YoyoGo 简单、轻量、快速、基于依赖注入的微服务框架

yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图2
yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图3
yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图4
yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图5
yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图6
yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图7
yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图8

  • 漂亮又快速的路由器 & MVC 模式 .
  • 丰富的中间件支持 (handler func & custom middleware) .
  • 微服务框架抽象了分层,在一个框架体系兼容各种 server 实现,如 rest,grpc 等 .
  • 充分运用依赖注入 DI,管理运行时生命周期,为框架提供了强大的扩展性 .
  • 功能强大的微服务集成能力 (Nacos, Eureka, Consul, ETCD) .
  • 受到许多出色的 Go Web 框架的启发,并实现了多种 server : fasthttpnet.httpgrpc .

yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图9

QQ 交流群: 780385870 (Go 浪文学院) , 在这里感谢贾国锦帮忙设计的 logo 很漂亮。

也可以加入我的公众号,通过公众号入群菜单进入微信群,主要还是在微信上活动啦。

yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图10

  1. go get github.com/yoyofx/yoyogo

go version < 1.13

  1. window 下在 cmd 中执行:
  2. set GO111MODULE=on
  3. set GOPROXY=https://goproxy.cn,direct
  4. linux 下执行:
  5. export GO111MODULE=on
  6. export GOPROXY=https://goproxy.cn,direct

go version >= 1.13

  1. go env -w GOPROXY=https://goproxy.cn,direct

vendor

  1. go mod vendor // 将依赖包拷贝到项目目录中去
  1. package main
  2. import ...
  3. func main() {
  4. WebApplication.CreateDefaultBuilder(func(rb router.IRouterBuilder) {
  5. rb.GET("/info",func (ctx *context.HttpContext) { // 支持Group方式
  6. ctx.JSON(200, context.H{"info": "ok"})
  7. })
  8. }).Build().Run() //默认端口号 :8080
  9. }

yoyofx/yoyogo: 🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go. Support Nacos ,Consoul ,Etcd ,Eureka ,kubernetes. - 图11

标准功能

  • 打印 Logo 和日志(YoyoGo)
  • 统一程序输入参数和环境变量 (YoyoGo)
  • 简单路由器绑定句柄功能
  • HttpContext 上下文封装 (请求,响应)
  • 静态文件端点(静态文件服务器)
  • JSON 序列化结构(Context.H)
  • 获取请求文件并保存
  • 获取请求数据(form-data,x-www-form-urlencoded,Json ,XML,Protobuf 等)
  • Http 请求的绑定模型(Url, From,JSON,XML,Protobuf)

响应渲染功能

  • Render Interface
  • JSON Render
  • JSONP Render
  • Indented Json Render
  • Secure Json Render
  • Ascii Json Render
  • Pure Json Render
  • Binary Data Render
  • TEXT
  • Protobuf
  • MessagePack
  • XML
  • YAML
  • File
  • Image
  • Template
  • Auto formater Render

中间件

  • Logger
  • StaticFile
  • Router Middleware
  • CORS
  • Binding
  • JWT
  • RequestId And Tracker for SkyWorking

路由

  • GET,POST,HEAD,PUT,DELETE 方法支持
  • 路由解析树与表达式支持
  • RouteData 路由数据 (/api/:version/) 与 Binding 的集成
  • 路由组功能
  • MVC 默认模板功能
  • 路由过滤器 Filter

MVC

  • 路由请求触发 Controller&Action
  • Action 方法参数绑定
  • 内部对象的 DI 化
  • 关键对象的参数传递

Dependency injection

  • 抽象集成第三方 DI 框架
  • MVC 模式集成
  • 框架级的 DI 支持功能

扩展

  • 配置
  • WebSocket
  • JWT
  • swagger
  • GRpc
  • Prometheus
  1. package main
  2. import
  3. ...
  4. func main() {
  5. webHost := CreateCustomWebHostBuilder().Build()
  6. webHost.Run()
  7. }
  8. // 自定义HostBuilder并支持 MVC 和 自动参数绑定功能,简单情况也可以直接使用CreateDefaultBuilder 。
  9. func CreateCustomBuilder() *abstractions.HostBuilder {
  10. configuration := abstractions.NewConfigurationBuilder().
  11. AddEnvironment().
  12. AddYamlFile("config").Build()
  13. return WebApplication.NewWebHostBuilder().
  14. UseConfiguration(configuration).
  15. Configure(func(app *WebApplication.WebApplicationBuilder) {
  16. app.UseMiddleware(middlewares.NewCORS())
  17. //WebApplication.UseMiddleware(middlewares.NewRequestTracker())
  18. app.UseStaticAssets()
  19. app.UseEndpoints(registerEndpointRouterConfig)
  20. app.UseMvc(func(builder *mvc.ControllerBuilder) {
  21. //builder.AddViews(&view.Option{Path: "./static/templates"})
  22. builder.AddViewsByConfig()
  23. builder.AddController(contollers.NewUserController)
  24. builder.AddFilter("/v1/user/info", &contollers.TestActionFilter{})
  25. })
  26. }).
  27. ConfigureServices(func(serviceCollection *dependencyinjection.ServiceCollection) {
  28. serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction))
  29. //eureka.UseServiceDiscovery(serviceCollection)
  30. //consul.UseServiceDiscovery(serviceCollection)
  31. nacos.UseServiceDiscovery(serviceCollection)
  32. }).
  33. OnApplicationLifeEvent(getApplicationLifeEvent)
  34. }
  35. //region endpoint 路由绑定函数
  36. func registerEndpoints(rb router.IRouterBuilder) {
  37. Endpoints.UseHealth(rb)
  38. Endpoints.UseViz(rb)
  39. Endpoints.UsePrometheus(rb)
  40. Endpoints.UsePprof(rb)
  41. Endpoints.UseJwt(rb)
  42. rb.GET("/error", func(ctx *context.HttpContext) {
  43. panic("http get error")
  44. })
  45. //POST 请求: /info/:id ?q1=abc&username=123
  46. rb.POST("/info/:id", func(ctx *context.HttpContext) {
  47. qs_q1 := ctx.Query("q1")
  48. pd_name := ctx.Param("username")
  49. userInfo := &UserInfo{}
  50. _ = ctx.Bind(userInfo) // 手动绑定请求对象
  51. strResult := fmt.Sprintf("Name:%s , Q1:%s , bind: %s", pd_name, qs_q1, userInfo)
  52. ctx.JSON(200, context.H{"info": "hello world", "result": strResult})
  53. })
  54. // 路由组功能实现绑定 GET 请求: /v1/api/info
  55. rb.Group("/v1/api", func(router *router.RouterGroup) {
  56. router.GET("/info", func(ctx *context.HttpContext) {
  57. ctx.JSON(200, context.H{"info": "ok"})
  58. })
  59. })
  60. // GET 请求: HttpContext.RequiredServices获取IOC对象
  61. rb.GET("/ioc", func(ctx *context.HttpContext) {
  62. var userAction models.IUserAction
  63. _ = ctx.RequiredServices.GetService(&userAction)
  64. ctx.JSON(200, context.H{"info": "ok " + userAction.Login("zhang")})
  65. })
  66. }
  67. //endregion
  68. //region 请求对象
  69. type UserInfo struct {
  70. UserName string `param:"username"`
  71. Number string `param:"q1"`
  72. Id string `param:"id"`
  73. }
  74. // ----------------------------------------- MVC 定义 ------------------------------------------------------
  75. // 定义Controller
  76. type UserController struct {
  77. *mvc.ApiController
  78. userAction models.IUserAction // IOC 对象参数
  79. }
  80. // 构造器依赖注入
  81. func NewUserController(userAction models.IUserAction) *UserController {
  82. return &UserController{userAction: userAction}
  83. }
  84. // 请求对象的参数化绑定
  85. type RegiserRequest struct {
  86. mvc.RequestBody
  87. UserName string `param:"username"`
  88. Password string `param:"password"`
  89. }
  90. // Register函数自动绑定参数
  91. func (this *UserController) Register(ctx *context.HttpContext, request *RegiserRequest) actionresult.IActionResult {
  92. result := mvc.ApiResult{Success: true, Message: "ok", Data: request}
  93. return actionresult.Json{Data: result}
  94. }
  95. // use userAction interface by ioc
  96. func (this *UserController) GetInfo() mvc.ApiResult {
  97. return this.OK(this.userAction.Login("zhang"))
  98. }
  99. // Web程序的开始与停止事件
  100. func fireApplicationLifeEvent(life *abstractions.ApplicationLife) {
  101. printDataEvent := func(event abstractions.ApplicationEvent) {
  102. fmt.Printf("[yoyogo] Topic: %s; Event: %v\n", event.Topic, event.Data)
  103. }
  104. for {
  105. select {
  106. case ev := <-life.ApplicationStarted:
  107. go printDataEvent(ev)
  108. case ev := <-life.ApplicationStopped:
  109. go printDataEvent(ev)
  110. break
  111. }
  112. }
  113. }

https://github.com/yoyofx/yoyogo