1. protoc 编译器下载
protoc编译器负责将xxx.proto文件编译生成xxx.pb.go文件,其位github.com/protocolbuffers/protobuf库下。
到下面地址下载对应系统版本的protoc编译器。下载后解压文件,bin目录下添加protoc文件路径到系统环境变量。
https://github.com/protocolbuffers/protobuf/releases
2. protoc-gen-go
protoc-gen-go提供了protoc编译器生成go相关api的实现,其位github.com/golang/protobuf库下。
protobuf库是protocol buffers协议的golang语言版实现。protoc编译器编译时需依赖它。
go get github.com/golang/protobuf
3. protoc-gen-micro
protoc-gen-micro提供了protoc编译器生成go-micro相关api的实现,其位github.com/micro/micro库下。
地址:https://github.com/micro/micro/tree/master/cmd/protoc-gen-micro
注:go-micro v2版本需用如下方式下载protoc-gen-micro。
go get github.com/micro/micro/v2/cmd/protoc-gen-micro@master
注:protoc-gen-micro的v3版本其对应的go-micro v3版本官方内外测试中,尚未发布正式稳定版本(2020/8/24)。故现开发项目使用go-mirco v2版本(v2.9.1)
4. go-micro v2
go get github.com/micro/go-micro/v2
5. go-plugins 插件下载
go-micro v2版本:因为go-micro v2后,去除了对consul 的支持(github.com/micro/go-micro/registry里面没有consul目录),但支持etcd、mdns作为服务发现。需单独按需下载
//etcd v3版本下载
go get github.com/micro/go-plugins/registry/etcdv3/v2
//consul 插件下载
go get github.com/micro/go-plugins/registry/consul/v2
//mqtt 插件下载
go get github.com/micro/go-plugins/broker/mqtt/v2
//kubernetes
go get github.com/micro/go-plugins/registry/kubernetes/v2
//rabbitmq
go get github.com/micro/go-plugins/broker/rabbitmq/v2
//nats
go get github.com/micro/go-plugins/transport/nats/v2
...
6. 问题and代码
6.0 以上5步code归纳
protoc compiler:https://github.com/protocolbuffers/protobuf/releases
protoc-gen-go:go get github.com/golang/protobuf
protoc-gen-micro: go get github.com/micro/micro/v2/cmd/protoc-gen-micro@master
go get github.com/micro/go-micro/v2
//etcdv3 plugin
go get github.com/micro/go-plugins/registry/etcdv3/v2
...
6.1 protoc生成go文件
完成以上步骤,通过如下命令我们可以得到xxx.pb.go和xxx.pb.micro.go两个文件
protoc --proto_path=. --micro_out=. --go_out=. xxx.proto
6.2 go run server.go 启动服务报错
#github.com/coreos/etcd/clientv3/balancer/picker
#github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
#github.com/micro/go-micro/transport/quic
//替换为v1.26.0版本的gRPC库
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
//go1.13版本存在的问题,目前项目为go1.14版本
replace github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.14.1
go version: go1.14.7 windows/amd64
demo:https://gitee.com/ztind/golang-micro/tree/master/go-micro/demo_v2