安装(前提是本地已安装protobuf和protof的go扩展)
go get github.com/micro/micro
go get github.com/micro/go-micro
go get github.com/micro/protoc-gen-micro
go get github.com/micro/go-plugins/registry/consul
支持consul需要单独安装这个包
go get github.com/micro/go-plugins/registry/consul
micro生成新项目
micro new --gopath=false --type=srv micpro
进入到 proto文件的同级目录,然后执行以下命令生成go文件
protoc --micro_out=. --go_out=. *.proto
package main
import (
"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"
"micpro/handler"
micpro "micpro/proto/micpro"
"micpro/subscriber"
)
func main() {
consulRegis := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"127.0.0.1:8500",
}
})
// New Service
service := micro.NewService(
micro.Name("go.micro.srv.micpro"),
micro.Registry(consulRegis),
micro.Version("latest"),
)
// Initialise service
service.Init()
// Register Handler
micpro.RegisterMicproHandler(service.Server(), new(handler.Micpro))
// Register Struct as Subscriber
micro.RegisterSubscriber("go.micro.srv.micpro", service.Server(), new(subscriber.Micpro))
// Register Function as Subscriber
micro.RegisterSubscriber("go.micro.srv.micpro", service.Server(), subscriber.Handler)
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
原文:https://www.cnblogs.com/itsuibi/p/14730237.html