Zmicro
A go microservice kit. 轻量级、高性能、易学、易通、易掌控的微服务开发工具
Install / Use
/learn @zmicro-team/ZmicroREADME
zmicro
文档
https://iobrother.com/
简介
zmicro 是一套微服务开发解决方案,旨在帮助中小企业与广大 go 爱好者打造一套可落地的微服务方案
zmicro 集成了流行的 web 框架 gin 与 极简的 rpc 框架 rpcx
目标
- 极简:简单易学,易于开发与维护
- 效率:通过工具生成 gin 代码,rpcx 代码,错误代码,以及 API 文档,提高开发效率
- 性能:WEB框架 gin 与 RPC 框架 rpcx 在性能上处于业界领先
快速开始
proto文件
syntax = "proto3";
option go_package = "github.com/zmicro-team/zmicro/examples/proto";
package proto;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
安装代码生成插件
go install github.com/gogo/protobuf/protoc-gen-gofast@latest
go install github.com/rpcxio/protoc-gen-rpcx@latest
protoc-gen-rpcx以上方法安装不成功,是因为作者还没发布新版本,请直接下载代码,编译安装
protoc -I. -I${GOPATH}/src \
--gofast_out=. --gofast_opt=paths=source_relative \
--rpcx_out=. --rpcx_opt=paths=source_relative *.proto
上述命令生成了 hello.pb.go 与 hello.rpcx.pb.go 两个文件。 hello.pb.go 文件是由protoc-gen-gofast插件生成的, 当然你也可以选择官方的protoc-gen-go插件来生成。 hello.rpcx.pb.go 是由protoc-gen-rpcx插件生成的,它包含服务端的一个骨架, 以及客户端的代码。
服务端配置文件
app:
name: "example"
rpc:
addr: ":5188"
服务端代码
package main
import (
"context"
"fmt"
"github.com/zmicro-team/zmicro"
"github.com/zmicro-team/zmicro/core/log"
"github.com/zmicro-team/zmicro/examples/proto"
"github.com/smallnest/rpcx/server"
)
func main() {
app := zmicro.New(zmicro.InitRpcServer(InitRpcServer))
if err := app.Run(); err != nil {
log.Fatal(err.Error())
}
}
func InitRpcServer(s *server.Server) error {
if err := s.RegisterName("Greeter", &GreeterImpl{}, ""); err != nil {
return err
}
return nil
}
type GreeterImpl struct{}
func (s *GreeterImpl) SayHello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloReply) error {
*rsp = proto.HelloReply{
Message: fmt.Sprintf("hello %s!", req.Name),
}
return nil
}
客户端代码
package main
import (
"context"
"github.com/zmicro-team/zmicro/core/log"
"github.com/zmicro-team/zmicro/core/transport/rpc/client"
"github.com/zmicro-team/zmicro/examples/proto"
)
func main() {
c := client.NewClient(client.WithServiceName("Greeter"), client.WithServiceAddr("127.0.0.1:5188"))
cli := proto.NewGreeterClient(c.GetXClient())
req := &proto.HelloRequest{
Name: "zmicro",
}
rsp, err := cli.SayHello(context.Background(), req)
if err != nil {
log.Error(err.Error())
return
}
log.Infof("reply: %s", rsp.Message)
}
启动服务器
go run server.go
启动客户端
go run client.go
输出
{"level":"info","ts":"2022-05-02T16:34:17.754+0800","caller":"log/log.go:59","msg":"reply: hello zmicro!"}
源码地址
https://github.com/zmicro-team/zmicro/tree/master/examples/greeter
Related Skills
node-connect
349.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
