🦄🌈 YoyoGo is a simple, light and fast , dependency injection based micro-service framework written in Go.

中文 / English

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

Release Go GoVersion Report Documentation Contributors License

特色

  • 漂亮又快速的路由器
  • 中间件支持 (handler func & custom middleware)
  • 微服务框架抽象了分层,在一个框架体系兼容各种server实现,如 rest,grpc等
  • 受到许多出色的 Go Web 框架的启发,server可替换,目前实现了 fasthttpnet.http

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

也可以加入我的公众号,通过公众号入群菜单进入微信群

感兴趣的朋友

框架安装

go get github.com/yoyofx/yoyogo

安装依赖 (由于某些原因国内下载不了依赖)

go version < 1.13

window 下在 cmd 中执行:
set GO111MODULE=on
set GOPROXY=https://goproxy.io,direct,https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://athens.azurefd.net,https://gonexus.dev 
linux  下执行:
export GO111MODULE=on
export GOPROXY=https://goproxy.cn

go version >= 1.13

go env -w GOPROXY=https://goproxy.io,direct,https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://athens.azurefd.net,https://gonexus.dev

vendor

go mod vendor       // 将依赖包拷贝到项目目录中去

简单的例子

package main
import ...

func main() {
	WebApplication.CreateDefaultBuilder(func(rb router.IRouterBuilder) {
        rb.GET("/info",func (ctx *context.HttpContext) {    // 支持Group方式
            ctx.JSON(200, context.H{"info": "ok"})
        })
    }).Build().Run()       //默认端口号 :8080
}

实现进度

标准功能

  • 打印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

进阶范例

package main

import
...

func main() {
	webHost := CreateCustomWebHostBuilder().Build()
	webHost.Run()
}

// 自定义HostBuilder并支持 MVC 和 自动参数绑定功能,简单情况也可以直接使用CreateDefaultBuilder 。
func CreateCustomBuilder() *abstractions.HostBuilder {

	configuration := abstractions.NewConfigurationBuilder().
		AddEnvironment().
		AddYamlFile("config").Build()

	return WebApplication.NewWebHostBuilder().
		UseConfiguration(configuration).
		Configure(func(app *WebApplication.WebApplicationBuilder) {
			app.UseMiddleware(middlewares.NewCORS())
			//WebApplication.UseMiddleware(middlewares.NewRequestTracker())
			app.UseStaticAssets()
			app.UseEndpoints(registerEndpointRouterConfig)
			app.UseMvc(func(builder *mvc.ControllerBuilder) {
				//builder.AddViews(&view.Option{Path: "./static/templates"})
				builder.AddViewsByConfig()
				builder.AddController(contollers.NewUserController)
				builder.AddFilter("/v1/user/info", &contollers.TestActionFilter{})
			})
		}).
		ConfigureServices(func(serviceCollection *dependencyinjection.ServiceCollection) {
			serviceCollection.AddTransientByImplements(models.NewUserAction, new(models.IUserAction))
			//eureka.UseServiceDiscovery(serviceCollection)
			//consul.UseServiceDiscovery(serviceCollection)
			nacos.UseServiceDiscovery(serviceCollection)
		}).
		OnApplicationLifeEvent(getApplicationLifeEvent)
}

//region endpoint 路由绑定函数
func registerEndpoints(rb router.IRouterBuilder) {
	Endpoints.UseHealth(rb)
	Endpoints.UseViz(rb)
	Endpoints.UsePrometheus(rb)
	Endpoints.UsePprof(rb)
	Endpoints.UseJwt(rb)

	rb.GET("/error", func(ctx *context.HttpContext) {
		panic("http get error")
	})

	//POST 请求: /info/:id ?q1=abc&username=123
	rb.POST("/info/:id", func(ctx *context.HttpContext) {
		qs_q1 := ctx.Query("q1")
		pd_name := ctx.Param("username")

		userInfo := &UserInfo{}

		_ = ctx.Bind(userInfo) // 手动绑定请求对象

		strResult := fmt.Sprintf("Name:%s , Q1:%s , bind: %s", pd_name, qs_q1, userInfo)

		ctx.JSON(200, context.H{"info": "hello world", "result": strResult})
	})

	// 路由组功能实现绑定 GET 请求:  /v1/api/info
	rb.Group("/v1/api", func(router *router.RouterGroup) {
		router.GET("/info", func(ctx *context.HttpContext) {
			ctx.JSON(200, context.H{"info": "ok"})
		})
	})

	// GET 请求: HttpContext.RequiredServices获取IOC对象
	rb.GET("/ioc", func(ctx *context.HttpContext) {
		var userAction models.IUserAction
		_ = ctx.RequiredServices.GetService(&userAction)
		ctx.JSON(200, context.H{"info": "ok " + userAction.Login("zhang")})
	})
}

//endregion

//region 请求对象
type UserInfo struct {
	UserName string `param:"username"`
	Number   string `param:"q1"`
	Id       string `param:"id"`
}

// ----------------------------------------- MVC 定义 ------------------------------------------------------

// 定义Controller
type UserController struct {
	*mvc.ApiController
	userAction models.IUserAction // IOC 对象参数
}

// 构造器依赖注入
func NewUserController(userAction models.IUserAction) *UserController {
	return &UserController{userAction: userAction}
}

// 请求对象的参数化绑定
type RegiserRequest struct {
	mvc.RequestBody
	UserName string `param:"username"`
	Password string `param:"password"`
}

// Register函数自动绑定参数
func (this *UserController) Register(ctx *context.HttpContext, request *RegiserRequest) actionresult.IActionResult {
	result := mvc.ApiResult{Success: true, Message: "ok", Data: request}
	return actionresult.Json{Data: result}
}

// use userAction interface by ioc  
func (this *UserController) GetInfo() mvc.ApiResult {
	return this.OK(this.userAction.Login("zhang"))
}

// Web程序的开始与停止事件
func fireApplicationLifeEvent(life *abstractions.ApplicationLife) {
	printDataEvent := func(event abstractions.ApplicationEvent) {
		fmt.Printf("[yoyogo] Topic: %s; Event: %v\n", event.Topic, event.Data)
	}
	for {
		select {
		case ev := <-life.ApplicationStarted:
			go printDataEvent(ev)
		case ev := <-life.ApplicationStopped:
			go printDataEvent(ev)
			break
		}
	}
}
Owner
YoyoFx
🐳 Go & Java & .NET &Python. I love computer science, system design. @golang @spring-projects @dotnet @kubernetes @python
YoyoFx
Comments
  • 文档中的简单实例无法启动

    文档中的简单实例无法启动

    使用文档中的简单实例代码放到本地执行后,无法正常启动监听端口,没有出现文档中的效果图

    error:panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x20 pc=0x6e2685]

  • Bump github.com/valyala/fasthttp from 1.28.0 to 1.34.0

    Bump github.com/valyala/fasthttp from 1.28.0 to 1.34.0

    Bumps github.com/valyala/fasthttp from 1.28.0 to 1.34.0.

    Release notes

    Sourced from github.com/valyala/fasthttp's releases.

    v1.34.0

    • 59f94a3 Update github.com/klauspost/compress (#1237) (Mikhail Faraponov)
    • 62c15a5 Don't reset RequestCtx.s (#1234) (Erik Dubbelboer)
    • 7670c6e Fix windows tests (#1235) (Erik Dubbelboer)
    • f54ffa1 feature: Keep the memory usage of the service at a stable level (#1216) (Rennbon)
    • 15262ec Warn about unsafe ServeFile usage (#1228) (Erik Dubbelboer)
    • 1116d03 Fix panic while reading invalid trailers (Erik Dubbelboer)
    • 856ca8e Update dependencies (#1230) (Mikhail Faraponov)
    • 6b5bc7b Add windows support to normalizePath (Erik Dubbelboer)
    • f0b0cfe Don't log ErrBadTrailer by default (Erik Dubbelboer)
    • 6937fee fix: (useless check), skip Response body if http method HEAD (#1224) (Pavel Burak)
    • b85d2a2 Fix http proxy behavior (#1221) (Aoang)
    • ad8a07a RequestHeader support set no default ContentType (#1218) (Jack.Ju)
    • c94581c support configure HostClient (#1214) (lin longhjui)
    • 632e222 Client examples (#1208) (Sergey Ponomarev)
    • 6a3cc23 uri_test.go use example.com for clearness (#1212) (Sergey Ponomarev)
    • 9d665e0 Update dependencies (#1204) (Mikhail Faraponov)
    • 8d7953e Fix scheme check for not yet parsed requests (#1203) (ArminBTVS)

    v1.33.0

    • 61aa8b1 remove redundant code (#1202) (tyltr)
    • 4369776 fix(hijack): reuse RequestCtx (#1201) (Sergio VS)
    • 2aca3e8 fix(hijack): reset userValues after hijack handler execution (#1199) (Sergio VS)
    • 9123060 Updated dependencies (#1194) (Mikhail Faraponov)

    v1.32.0

    • 7eeb00e Make tests less flaky (#1189) (Erik Dubbelboer)
    • d19b872 Update tcpdialer.go (#1188) (Mikhail Faraponov)
    • c727b99 Release UseHostHeader in ReleaseRequest() (#1185) (Tolyar)
    • 6c0518b Fix UseHostHeader for DoTimeout + tests (#1184) (Tolyar)
    • 6b55811 Add MaxIdleWorkerDuration to Server. (#1183) (Kilos Liu)
    • 4517204 Allow to set Host header for Client (#1169) (Tolyar)
    • 258a4c1 fix: reset response after reset user values on keep-alive connections (#1176) (Sergio VS)
    • e9db537 Use %w to wrap errors (#1175) (Erik Dubbelboer)
    • 7db0597 Fix bad request trailer panic (Erik Dubbelboer)
    • 4aadf9a Fix parseTrailer panic (Erik Dubbelboer)
    • da7ff7a Add trailer support (#1165) (ichx)
    • 017f0aa fix: reset request after reset user values on keep-alive connections (#1162) (Sergio VS)
    • 3b117f8 feat: close idle connections when server shutdown (#1155) (ichx)
    • a94a2c3 Remove redundant code (#1154) (ichx)
    • f7c354c Fix race condition in Client.mCleaner (Erik Dubbelboer)
    • c078a9d Add string and bytes buffer convert trick in README (#1151) (ichx)
    • 3ff6aaa uri: isHttps() and isHttp() (#1150) (Sergey Ponomarev)
    • 8febad0 http.go: Request.SetURI() (Fix #1141) (#1148) (Sergey Ponomarev)
    • 2ca01c7 fix: Status Line parsing and writing (#1135) (Shivansh Vij)
    • 931d0a4 Fix lint (Erik Dubbelboer)
    • d613502 use sync.map is better (#1145) (halst)
    • c15e642 Don't run all race tests on windows (#1143) (Erik Dubbelboer)
    • 6006c87 chore (#1137) (tyltr)
    • 6d4db9b Fix race condition in getTCPAddrs (Erik Dubbelboer)

    ... (truncated)

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

  • nacos-viper-remote 连接阿里云MSE Nacos 取不回任何内容,也不报错

    nacos-viper-remote 连接阿里云MSE Nacos 取不回任何内容,也不报错

    nacos-viper-remote 连接阿里云MSE Nacos 取不回任何内容,也不报错

    	remote.SetOptions(&remote.Option{
    		Url:         Config.Mse.Server,
    		Port:        uint64(Config.Mse.Port),
    		NamespaceId: Config.Mse.Namespace,
    		GroupName:   Config.Mse.GroupName,
    		Config:      remote.Config{DataId: Config.Mse.ConfigName},
    		Auth:        nil,
    	})
    	remoteConfig := viper.New()
                 // 这里的 endpoint 填 localhost 也一样取不回任何内容
    	err := remoteConfig.AddRemoteProvider("nacos", "mse-8f7b0b60-nacos-ans.mse.aliyuncs.com", "")
    	if err != nil {
    		fmt.Println("加载远程配置失败 :", err.Error())
    	}
    	remoteConfig.SetConfigType("yaml")
    	err = remoteConfig.ReadRemoteConfig()
    	if err != nil {
    		fmt.Println("加载远程配置失败 :", err.Error())
    	}
    	fmt.Println("加载远程配置: Config = ", remoteConfig.GetString("log.level"))
    
    	loadFromViper(remoteConfig)
    
    	fmt.Println("加载远程配置: Config = ", ObjToJSON(Config))
    
  • 错误日志无法输出堆栈

    错误日志无法输出堆栈

    使用框架时程序出现500错误,但是控制台中只输出了错误的信息,并没有输出错误的堆栈,导致查找问题代码十分困难,建议输出全量堆栈信息。 2021/11/09 16:31:26.43 - [YOYOGO] - [INFO] [yoyogo] 2021/11/09 - 16:31:26.38 | 200 | 53 ms| localhost:8080 | GET /v1/tenant/tenantlist?pageSize=20&tCode=44444&pageIdnex=1&pageIndex=1 2021/11/09 16:51:45.85 - [YOYOGO] - [INFO] [yoyogo] Method: GET Url: /v1/cluster/list?current=1&pageSize=20 Content-Type: , Accept: / 2021/11/09 16:51:45.85 - [YOYOGO] - [ERROR] { "Result": -1 , "Data": "error", "Message": "interface conversion: interface {} is nil, not map[string]interface {}" }%!(EXTRA string=) 2021/11/09 16:51:45.85 - [YOYOGO] - [INFO] [yoyogo] 2021/11/09 - 16:51:45.85 | 500 | 8 ms| localhost:8080 | GET /v1/cluster/list?current=1&pageSize=20

  • 配置文件中写端口号没有生效

    配置文件中写端口号没有生效

    application: name: Alert metadata: "prod Env" server: type: "fasthttp" address: ":8089"


    [yoyogo] [2020/09/30 14:25:06.69] yoyogo framework version : v1.5.3.release [yoyogo] [2020/09/30 14:25:06.69] machine host ip : 172.20.10.97 [yoyogo] [2020/09/30 14:25:06.69] listening on port : 8080


    version 版本号也不对

  • v1.8.1

    v1.8.1

    Features

    1. Get config value for DSL, that support key or ref object.

    Such as YAML:

    env: ${CUSTOM_ENV}
    profile:
      dns: ${REMOTE_HOST}
      ip: ${REMOTE_IP:10.0.1.12}
      namespace: ${MYNAMESPACE:space.localhost}
    

    Go Example

    type Profile struct {
    	DNS string `config:"dns"`
    	IP  string `config:"ip"`
    	NS  string `config:"namespace"`
    }
    
    config := abstractions.NewConfigurationBuilder().
           AddEnvironment().
           AddYamlFile("config").Build()
    
    config.GetConfigObject("profile", &profile)
    assert.Equal(t, profile.NS, "space.yoyogo.run")
    assert.Equal(t, profile.DNS, "my host")
    assert.Equal(t, profile.IP, "10.0.1.12")
    
Sample full stack micro services application built using the go-Micro framework.
Sample full stack micro services application built using the go-Micro framework.

goTemp goTemp is a full stack Golang microservices sample application built using go-micro. The application is built as a series of services that prov

Dec 26, 2022
NewSQL distributed storage database based on micro service framework
NewSQL distributed storage database based on micro service framework

QLite 是基于微服务的 NewSQL 型数据库系统,与传统的一体化数据库不同,该系统将本该内置的多种数据结构(STL)拆分成多个服务模块,每个模块都是独立的一个节点,每个节点都与其主网关进行连接,从而形成分布式存储结构。

Jun 19, 2022
Micro-service framework in Go
Micro-service framework in Go

Kite Micro-Service Framework Kite is a framework for developing micro-services in Go. Kite is both the name of the framework and the micro-service tha

Jan 9, 2023
Another excellent micro service framework

xservice [WIP] Another excellent micro service framework Features RESTful API (base on echo/v4) gRPC & gRPC gateway service & Swagger document generat

May 28, 2022
😈 Simple micro-front-end framework.
😈 Simple micro-front-end framework.

Development and Maintenance Status RancherOS 1.x is no longer being actively maintained. There are two significant reasons behind this product decisio

Jan 5, 2022
Automatic Service Mesh and RPC generation for Go micro services, it's a humble alternative to gRPC with Istio.
Automatic Service Mesh and RPC generation for Go micro services, it's a humble alternative to gRPC with Istio.

Mesh RPC MeshRPC provides automatic Service Mesh and RPC generation for Go micro services, it's a humble alternative to gRPC with Istio. In a nutshell

Aug 22, 2022
Fastglue is an opinionated, bare bones wrapper that glues together fasthttp and fasthttprouter to act as a micro HTTP framework.

fastglue Overview fastglue is an opinionated, bare bones wrapper that glues together fasthttp and fasthttprouter to act as a micro HTTP framework. It

Jun 14, 2022
White Matrix Micro Service Generate CLI Tool

micro-service-gen-tool White Matrix Micro Service Generate CLI Tool usage templa

Jan 5, 2022
Micro service frame
Micro service frame

MicroFrame MicroFrame是基于go-micro结合go的plugin(需要go1.8或以上)开发的一个微服务框架。 go-micro本身已经做了非常好的抽象和插件化。MicroFrame没有直接采用go-micro,而是在它的基础上重新开发有下面一些原因。 对纯粹的业务开发屏蔽掉掉

Nov 9, 2021
Zdpgo micro service inventory - Golang开发的库存微服务

zdpgo_micro_service_goods Golang开发的商品微服务service层 生成proto 安装插件 go install google.

Feb 3, 2022
Go Micro is a framework for distributed systems development

Go Micro Go Micro is a framework for distributed systems development. Overview Go Micro provides the core requirements for distributed systems develop

Jan 3, 2023
a microservice framework for rapid development of micro services in Go with rich eco-system
a microservice framework for rapid development of micro services in Go with rich eco-system

中文版README Go-Chassis is a microservice framework for rapid development of microservices in Go. it focus on helping developer to deliver cloud native a

Dec 27, 2022
micro-draft-manager is a microservice that helps you to manage unstructured data in your application with sorting and full-text search

micro-draft-manager is a microservice that helps you to manage unstructured data in your application with sorting and full-text search. For example, y

Nov 24, 2021
Study Project for the application of micro services and requisition controls

Starting Endpoint GO with Retry Request Install GoLang for Linux Tutorial: LINK

Jul 4, 2022
A code generator that turns plain old Go services into RPC-enabled (micro)services with robust HTTP APIs.

Frodo is a code generator and runtime library that helps you write RPC-enabled (micro) services and APIs.

Dec 16, 2022
Micro is a platform for cloud native development
Micro is a platform for cloud native development

Micro Overview Micro addresses the key requirements for building services in the cloud. It leverages the microservices architecture pattern and provid

Dec 29, 2022
goTempM is a full stack Golang microservices sample application built on top of the Micro platform.
goTempM is a full stack Golang microservices sample application built on top of the Micro platform.

goTempM is a full stack Golang microservices sample application built on top of the Micro platform.

Sep 24, 2022
微服务架构-micro-基于go-zero zrpc etcd 单独集成orm-gorm 日志-uber/zap

micro目录结构 pkg ├── api 业务接口逻辑层 所有的业务逻辑存放目录。 │ ├── model 数据模型 数据管理层,仅用于操作管理数据,如数据库操作 ├── assets

Jul 12, 2022
A Micro-UTP, plug-able sanity checker for any on-prem JFrog platform instance

hello-frog About this plugin This plugin is a template and a functioning example for a basic JFrog CLI plugin. This README shows the expected structur

Dec 7, 2021