Git Product home page Git Product logo

mcube's Introduction

mcube

Go Report Card Release MIT License

官方文档

mcube是一款用于构建渐进式微服务(单体-->微服务)的框架, 让应用从单体无缝过渡到微服务, 同时提供丰富的配置即用的功能配置, 只需简单配置就可拥有:

  • Log: 支持文件滚动和Trace的日志打印
  • Metric: 支持应用自定义指标监控
  • Trace: 集成支持完整的全链路追踪(HTTP Server/GRPC Server/数据库...)以及自定义埋点
  • CORS: 资源跨域共享
  • Health Check: HTTP 和 GRPC 健康检查
  • API DOC: 基于Swagger的 API 文档

除了上面这些功能配置,还会用到很多三方工具, 也是配置即用:

  • MySQL: Grom集成
  • MongoDB 官方驱动集成
  • Redis: go-redis集成
  • Kafka: kafka-go集成
  • 分布式缓存: 当前只适配了Redis
  • 分布式锁: 当前只适配了Redis

框架架构

快速开始

下面是演示一个TestObject对象的注册与获取的基础功能:

package main

import (
	"context"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/infraboard/mcube/v2/ioc"
	"github.com/infraboard/mcube/v2/ioc/config/datasource"
	"github.com/infraboard/mcube/v2/ioc/server"
	"gorm.io/gorm"
)

func main() {
	// 注册HTTP接口类
	ioc.Api().Registry(&ApiHandler{})

	// 开启配置文件读取配置
	server.DefaultConfig.ConfigFile.Enabled = true
	server.DefaultConfig.ConfigFile.Path = "etc/application.toml"

	// 启动应用
	err := server.Run(context.Background())
	if err != nil {
		panic(err)
	}
}

type ApiHandler struct {
	// 继承自Ioc对象
	ioc.ObjectImpl

	// mysql db依赖
	db *gorm.DB
}

// 覆写对象的名称, 该名称名称会体现在API的路径前缀里面
// 比如: /simple/api/v1/module_a/db_stats
// 其中/simple/api/v1/module_a 就是对象API前缀, 命名规则如下:
// <service_name>/<path_prefix>/<object_version>/<object_name>
func (h *ApiHandler) Name() string {
	return "module_a"
}

// 初始化db属性, 从ioc的配置区域获取共用工具 gorm db对象
func (h *ApiHandler) Init() error {
	h.db = datasource.DB()
	return nil
}

// API路由
func (h *ApiHandler) Registry(r gin.IRouter) {
	r.GET("/db_stats", func(ctx *gin.Context) {
		db, _ := h.db.DB()
		ctx.JSON(http.StatusOK, gin.H{
			"data": db.Stats(),
		})
	})
}

应用开发

标准化工程配置

统一了项目的配置加载方式:

环境变量 配置文件 TOML YAML JSON 下面是项目配置文件(etc/application.toml)内容:

[app]
name = "simple"
key  = "this is your app key"

[http]
host = "127.0.0.1"
port = 8020

[datasource]
host = "127.0.0.1"
port = 3306
username = "root"
password = "123456"
database = "test"

[log]
level = "debug"

[log.file]
enable = true
file_path = "logs/app.log"

即插即用的组件

通过简单的配置就能为项目添加:

检查检查(Health Chcek) 应用指标监控(Metric)

import (
  // 开启Health健康检查
  _ "github.com/infraboard/mcube/v2/ioc/apps/health/gin"
  // 开启Metric
  _ "github.com/infraboard/mcube/v2/ioc/apps/metric/gin"
)

启动过后, 在日志里就能看到这2个功能开启了:

2024-01-05T11:30:00+08:00 INFO   health/gin/check.go:52 > Get the Health using http://127.0.0.1:8020/healthz component:HEALTH_CHECK
2024-01-05T11:30:00+08:00 INFO   metric/gin/metric.go:51 > Get the Metric using http://127.0.0.1:8020/metrics component:METRIC

当然你也可以通过配置来修改功能的URL路径:

[health]
  path = "/healthz"

[metric]
  enable = true
  provider = "prometheus"
  endpoint = "/metrics"

mcube's People

Contributors

yumaojun03 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mcube's Issues

make pb无法在Windows使用解决方案

您好,有幸看到你的mcube工具集,发现由于windows路径原因无法在windows Git Bash环境执行make pb命令,以下各人的解决方法,已验证有效,请参考!

MOD_DIR_WINDOWS := $(shell go env GOPATH | sed -e 's/\\\\\(.\)/\/\1/g' | sed 's/://g')/pkg/mod

pb_windows:  ## Copy mcube protobuf files to common/pb for Windows

	@mkdir -pv common/pb/github.com/infraboard/mcube/pb
        @rm -rf common/pb/github.com/infraboard/mcube/pb/*
	@cp -r /${MOD_DIR_WINDOWS}/${MCUBE_MODULE}@${MCUBE_VERSION}/pb/* common/pb/github.com/infraboard/mcube/pb
	@rm -rf common/pb/github.com/infraboard/mcube/pb/*/*.go

make install err

tigeryu@mac mcube-2.0.7 % make dep
tigeryu@mac mcube-2.0.7 % make install
no required module provides package github.com/infraboard/mcube/v2/cmd/mcube; to add it:
go get github.com/infraboard/mcube/v2/cmd/mcube
make: *** [install] Error 1
tigeryu@mac mcube-2.0.7 % make build
package cmd/mcube/main.go is not in GOROOT (/usr/local/go/src/cmd/mcube/main.go)
make: *** [build] Error 1
tigeryu@mac mcube-2.0.7 % go get github.com/infraboard/mcube/v2/cmd/mcube
go: module github.com/infraboard/mcube@upgrade found (v1.19.22), but does not contain package github.com/infraboard/mcube/v2/cmd/mcube
tigeryu@mac mcube-2.0.7 %

初始化项目,由于mergo地址变了,下载依赖报错

报错信息:
go: apps/book imports github.com/imdario/mergo: github.com/imdario/[email protected]: parsing go.mod: module declares its path as: dario.cat/mergo but was required as: github.com/imdario/mergo
原因:
依赖地址变了,原地址:imdario/mergo
现在地址:dario.cat/mergo
需要手动下载go get dario.cat/mergo
并修改apps->book->app.go 文件

安装之后执行mcube执行mcube -v 不显示版本

安装版本1.9.1之后执行mcube执行mcube -v 不显示版本。看到cmd里的root代码,是直接返回help
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
没有加version的逻辑

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.