官方文档地址:https://micro.mu/docs/index.html
Github地址:https://github.com/micro
中文文档https://micro.mu/docs/cn/index.html
实例源码https://github.com/micro/examples
安装依赖
protoc-gen-go
首选需要安装protoc,下载地址https://github.com/protocolbuffers/protobuf/releases,根据protoc-版本号-系统平台.zip选择下载和解压文件,在解压的bin目录下执行
# wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-linux-x86_64.zip
# unzip protoc-3.11.4-linux-x86_64.zip -d protoc
# sudo mv protoc/bin/protoc /usr/bin/
安装protoc-gen-go,用于自动化生成go-protobuf代码文件,文件后缀是.pb.go
# install protoc-gen-go
$ get google.golang.org/protobuf/cmd/protoc-gen-go
protoc-gen-micro
用于自动化生成go-micro需要的代码文件。
protoc-gen-micro的github 地址是:https://github.com/micro/protoc-gen-micro
# install protoc-gen-micro
$go get github.com/micro/protoc-gen-micro
go: downloading github.com/micro/protoc-gen-micro v1.0.0
go: github.com/micro/protoc-gen-micro upgrade => v1.0.0
go: downloading github.com/golang/protobuf v1.3.2
v2版本
$go get github.com/micro/protoc-gen-micro/v2
go: downloading github.com/micro/protoc-gen-micro/v2 v2.1.1
go: github.com/micro/protoc-gen-micro/v2 upgrade => v2.1.1
HelloWorld
在proto目录下创建greeter.proto文件 内容如下
syntax = "proto3";
option go_package = "/proto";
service Greeter {
rpc Hello(Request) returns (Response) {}
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
执行下面的命令,分别自动会生成greeter.pb.go和greeter.micro.go文件
$protoc --proto_path=. --micro_out=. --go_out=. proto/greeter.proto
go.mod 文件如下
module github.com/baxiang/go-micro/hello
go 1.14
require (
github.com/golang/protobuf v1.4.3
github.com/micro/go-micro v1.18.0
github.com/micro/go-plugins v1.5.1
google.golang.org/protobuf v1.25.0
)
replace google.golang.org/grpc v1.27.0 => google.golang.org/grpc v1.26.0
在server文件夹下创建main.go 文件
package main
import (
"context"
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/util/log"
"github.com/micro/go-plugins/registry/consul"
"github.com/baxiang/go-micro/hello/proto"
)
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
rsp.Msg = "Hello " + req.Name
return nil
}
func main() {
// consul resister
reg := consul.NewRegistry(func(op *registry.Options) {
op.Addrs = []string{
"127.0.0.1:8500",
}
})
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Registry(reg),
micro.Name("service.greeter"),
)
// Init will parse the command line flags.
service.Init()
// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the server
log.Fatal(service.Run())
}
执行server启动命令
$ go run server/main.go
2021-02-18 16:58:57.952352 I | Transport [http] Listening on [::]:56136
2021-02-18 16:58:57.952418 I | Broker [http] Connected to [::]:56137
2021-02-18 16:58:57.952688 I | Registry [consul] Registering node: service.greeter-4a8e58b8-8c12-4d7c-a458-2342989dbc41
Service Discovery
服务发现与注册组件,服务发现使用的是consul
consul agent -dev
浏览器打开本地
在client文件下创建main.go 文件
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry"
"github.com/micro/go-plugins/registry/consul"
"github.com/baxiang/go-micro/hello/proto"
)
func main() {
reg := consul.NewRegistry(func(op *registry.Options) {
op.Addrs = []string{
"127.0.0.1:8500",
}
})
service := micro.NewService(
micro.Registry(reg),
micro.Name("greeter.client"),
)
service.Init()
// Create new greeter client
g := proto.NewGreeterService("service.greeter", service.Client())
// Call the greeter
rsp, err := g.Hello(context.Background(), &proto.Request{Name: "micro"})
if err != nil {
fmt.Println(err)
}
// Print response
fmt.Println(rsp.Msg)
}
执行客户端
$ go run client/main.go
Hello micro
常见错误
panic: qtls.ConnectionState not compatible with tls.ConnectionState
goroutine 1 [running]:
github.com/lucas-clemente/quic-go/internal/handshake.init.1()
/Users/baxiang/go/pkg/mod/github.com/lucas-clemente/quic-go@v0.13.1/internal/handshake/unsafe.go:17 +0x12e
exit status 2
官方提示https://github.com/micro/micro/issues/1574
grpc 版本,参考https://github.com/micro/micro/issues/1206
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
../../../../../pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:114:78: undefined: resolver.BuildOption
../../../../../pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:182:31: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/picker
../../../../../pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/picker/err.go:37:44: undefined: balancer.PickOptions
../../../../../pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/picker/roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
虽然etcd的主分支已经兼容,但是release的版本仍然没有更新,因此只能降级使用,在go.mod 中加入, 代表强制使用v1.26.0版本
replace google.golang.org/grpc v1.27.0 => google.golang.org/grpc v1.26.0
micro脚手架
编译micro
进入这个文件夹github.com/micro/micro
cd $GOPATH/src/github.com/micro/micro
执行下面命令
go install
如果出现如下的类似错误
verifying github.com/lucas-clemente/quic-go@v0.12.0: checksum mismatch
downloaded: h1:TRbvZ6F++sofeGbh+Z2IIyIOhl8KyGnYuA06g2yrHdI=
go.sum: h1:dYHUyB50gEQlK3KqytmNySzuyzAcaQ3iuI2ZReAfVrE=
删除掉go.sum 文件,执行下面的命令
rm go.sum
go mod vendor
HelloWorld
创建项目的命令是micro new
$ micro new --help
NAME:
micro new - Create a service template
USAGE:
micro new [command options] [arguments...]
OPTIONS:
--namespace value Namespace for the service e.g com.example (default: "go.micro")
--type value Type of service e.g api, fnc, srv, web (default: "srv")
--fqdn value FQDN of service e.g com.example.srv.service (defaults to namespace.type.alias)
--alias value Alias is the short name used as part of combined name if specified
--plugin value Specify plugins e.g --plugin=registry=etcd:broker=nats or use flag multiple times
--gopath Create the service in the gopath. Defaults to true.
$ $ micro new baxiang.com/micro/service_example
Creating service go.micro.srv.service_example in /Users/baxiang/go/src/baxiang.com/micro/service_example
.
├── main.go
├── plugin.go
├── handler
│ └── service_example.go
├── subscriber
│ └── service_example.go
├── proto/service_example
│ └── service_example.proto
├── Dockerfile
├── Makefile
├── README.md
└── go.mod
download protobuf for micro:
brew install protobuf
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-micro
compile the proto file service_example.proto:
cd /Users/baxiang/go/src/baxiang.com/micro/service_example
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/service_example/service_example.proto
然后进入项目文件夹
cd $GOPATH/src/baxiang.com/micro/example
运行下面的代码生成protobuf和micro代码文件
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/service_example/service_example.proto
确保已经运行consul agent -dev,最后启动服务
go run main.go
创建web
$ micro new --type="web" baxiang.com/micro/web_example
Creating service go.micro.web.web_example in /Users/baxiang/go/src/baxiang.com/micro/web_example
.
├── main.go
├── plugin.go
├── handler
│ └── handler.go
├── html
│ └── index.html
├── Dockerfile
├── Makefile
├── README.md
└── go.mod
修改handle文件夹下handle.go 文件的web_example “path/to/service/proto/web_example”路径地址
参考
https://micro.mu/docs/go-helloworld.html
https://github.com/lpxxn/gomicrorpc
https://github.com/micro-in-cn/x-gateway
https://sdgmf.github.io/goproject/
https://blog.csdn.net/KingEasternSun/article/details/79085588
http://wen.topgoer.com/docs/mindoc/gaishu2