mico

package module
v0.0.0-...-ad17899 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2025 License: MIT Imports: 23 Imported by: 0

README

Mico 微服务框架

简介

Mico 是一个基于 Go 语言的轻量级微服务框架,提供了构建分布式系统所需的核心功能,包括服务注册发现、负载均衡、gRPC/HTTP 服务支持、日志记录等。框架设计简洁易用,适合快速搭建微服务架构。

特性

  • 支持 gRPC 和 HTTP 服务
  • 服务注册与发现(基于 etcd)
  • 自定义负载均衡策略
  • 上下文元数据管理
  • 结构化日志记录(基于 zap)
  • 错误处理与封装
  • 配置化管理

安装

确保你已安装 Go 1.16 或更高版本。使用以下命令获取 Mico:

go get github.com/YCloud160/mico

快速开始

项目提供了示例应用 example/hello,展示如何使用 Mico 构建一个简单的 gRPC 服务。

示例服务
// server.go
type Greeter struct{}

func (g *Greeter) SayHello(ctx context.Context, req *pb.SayHelloReq) (*pb.SayHelloResp, error) {
    return &pb.SayHelloResp{Msg: "Hello, " + req.GetMsg()}, nil
}

func main() {
    // 初始化配置
    mico.InitAppConfig("app.yaml")

    // 添加 gRPC 服务
    mico.AddGrpcServer("hello", func(s *grpc.Server) {
        pb.RegisterGreeterServer(s, &Greeter{})
    })

    // 启动服务
    if err := mico.Run(); err != nil {
        log.Fatal(err)
    }
}
示例客户端
// client.go
func main() {
    // 初始化配置
    mico.InitAppConfig("app.yaml")

    // 创建 gRPC 客户端
    client, err := mico.NewGrpcClient("hello")
    if err != nil {
        log.Fatal(err)
    }

    // 调用服务
    req := &pb.SayHelloReq{Msg: "World"}
    var resp pb.SayHelloResp
    if err := client.Invoke(context.Background(), "/pb.Greeter/SayHello", req, &resp); err != nil {
        log.Fatal(err)
    }

    fmt.Println(resp.GetMsg())
}

配置

Mico 使用 YAML 文件进行配置,示例配置如下:

server:
  hello:
    addr: ":8080"
    type: grpc
client:
  default:
    target: "127.0.0.1:8080"
pubsub:
  etcd:
    endpoints:
      - "http://127.0.0.1:2379"

中间件

Mico 提供了多种中间件支持:

  • 服务发现:基于 etcd 的服务注册与发现
  • 负载均衡:支持自定义负载均衡策略
  • 日志:集成 zap 实现结构化日志记录
  • 解析器:支持 direct 和 etcd 解析器

日志

Mico 使用 zap 作为默认日志库,支持设置日志级别和上下文日志:

mico.InitLog("myapp", "server", "/var/log/myapp")
mico.SetLevel("debug")

mico.Debug("Debug message")
mico.Infoc(ctx, "Request received", mico.Any("request", req))

错误处理

Mico 提供了统一的错误封装机制:

err := mico.NewError(400, "bad request")
mico.Errorc(ctx, "Error occurred", mico.Any("error", err))

贡献

欢迎贡献代码和文档。请先阅读 CONTRIBUTING.md 获取贡献指南。

许可证

本项目采用 Apache-2.0 许可证。详情请查看 LICENSE 文件。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddGrpcServer

func AddGrpcServer(name string, f func(server *grpc.Server), options ...ServerOption)

func AddHttpServer

func AddHttpServer(name string, handler http.Handler, options ...ServerOption)

func AddServer

func AddServer(srv Server)

func GetPubSubConf

func GetPubSubConf() *pubsub.PubSubConfig

func InitAppConfig

func InitAppConfig(filepath string)

func Run

func Run() error

Types

type AppConf

type AppConf struct {
	ServerList   []*ServerConf        `mapstructure:"server"`
	Client       *ClientConf          `mapstructure:"client"`
	PubSubConfig *pubsub.PubSubConfig `mapstructure:"pubsub"`
}

type ClientConf

type ClientConf struct {
	RequestTimeout int32 `mapstructure:"requestTimeout"`
}

func GetClientConf

func GetClientConf() *ClientConf

type ClientOption

type ClientOption interface {
	// contains filtered or unexported methods
}

func WithDialTarget

func WithDialTarget(target string) ClientOption

func WithGrpcDialOptions

func WithGrpcDialOptions(grpcDialOptions ...grpc.DialOption) ClientOption

type ClientOptionFun

type ClientOptionFun func(*ClientOptions)

type ClientOptions

type ClientOptions struct {
	// contains filtered or unexported fields
}

type Endpoint

type Endpoint struct {
	Host   string
	Port   string
	Weight int32
	Proxy  string
	Tags   map[string]string
}

func (*Endpoint) ToPubSubEndpoint

func (ep *Endpoint) ToPubSubEndpoint(name string) *pubsub.Endpoint

type GrpcClient

type GrpcClient struct {
	// contains filtered or unexported fields
}

func NewGrpcClient

func NewGrpcClient(name string, options ...ClientOption) (*GrpcClient, error)

func (*GrpcClient) Invoke

func (c *GrpcClient) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error

func (*GrpcClient) NewStream

func (c *GrpcClient) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)

type GrpcServer

type GrpcServer struct {
	// contains filtered or unexported fields
}

func (*GrpcServer) Endpoint

func (srv *GrpcServer) Endpoint() *Endpoint

func (*GrpcServer) Name

func (srv *GrpcServer) Name() string

func (*GrpcServer) Serve

func (srv *GrpcServer) Serve(lis net.Listener) error

func (*GrpcServer) Shutdown

func (srv *GrpcServer) Shutdown() error

type HttpInterceptor

type HttpInterceptor func(w http.ResponseWriter, r *http.Request, handler http.HandlerFunc)

type HttpServer

type HttpServer struct {
	// contains filtered or unexported fields
}

func (*HttpServer) Endpoint

func (srv *HttpServer) Endpoint() *Endpoint

func (*HttpServer) Name

func (srv *HttpServer) Name() string

func (*HttpServer) Serve

func (srv *HttpServer) Serve(lis net.Listener) error

func (*HttpServer) ServeHTTP

func (srv *HttpServer) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*HttpServer) Shutdown

func (srv *HttpServer) Shutdown() error

type Server

type Server interface {
	Name() string
	Serve(lis net.Listener) error
	Shutdown() error
	Endpoint() *Endpoint
}

type ServerConf

type ServerConf struct {
	Name             string `mapstructure:"name"`
	Host             string `mapstructure:"host"`
	Port             string `mapstructure:"port"`
	Weight           int32  `mapstructure:"weight"`
	Proxy            string `mapstructure:"proxy"`
	MaxRequestSecond int32  `mapstructure:"maxRequestSecond"`
}

func GetServerConf

func GetServerConf(name string) *ServerConf

type ServerOption

type ServerOption interface {
	// contains filtered or unexported methods
}

func WithGrpcServerOptions

func WithGrpcServerOptions(grpcOptions ...grpc.ServerOption) ServerOption

func WithHttpInterceptors

func WithHttpInterceptors(httpInterceptors ...HttpInterceptor) ServerOption

type ServerOptionFun

type ServerOptionFun func(*ServerOptions)

type ServerOptions

type ServerOptions struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
example
hello command
hello/client command
middleware

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL