官方文档地址: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目录下执行

  1. # wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-linux-x86_64.zip
  2. # unzip protoc-3.11.4-linux-x86_64.zip -d protoc
  3. # sudo mv protoc/bin/protoc /usr/bin/

安装protoc-gen-go,用于自动化生成go-protobuf代码文件,文件后缀是.pb.go

  1. # install protoc-gen-go
  2. $ 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

  1. # install protoc-gen-micro
  2. $go get github.com/micro/protoc-gen-micro
  3. go: downloading github.com/micro/protoc-gen-micro v1.0.0
  4. go: github.com/micro/protoc-gen-micro upgrade => v1.0.0
  5. go: downloading github.com/golang/protobuf v1.3.2

v2版本

  1. $go get github.com/micro/protoc-gen-micro/v2
  2. go: downloading github.com/micro/protoc-gen-micro/v2 v2.1.1
  3. go: github.com/micro/protoc-gen-micro/v2 upgrade => v2.1.1

HelloWorld

在proto目录下创建greeter.proto文件 内容如下

  1. syntax = "proto3";
  2. option go_package = "/proto";
  3. service Greeter {
  4. rpc Hello(Request) returns (Response) {}
  5. }
  6. message Request {
  7. string name = 1;
  8. }
  9. message Response {
  10. string msg = 1;
  11. }

执行下面的命令,分别自动会生成greeter.pb.go和greeter.micro.go文件

  1. $protoc --proto_path=. --micro_out=. --go_out=. proto/greeter.proto

go.mod 文件如下

  1. module github.com/baxiang/go-micro/hello
  2. go 1.14
  3. require (
  4. github.com/golang/protobuf v1.4.3
  5. github.com/micro/go-micro v1.18.0
  6. github.com/micro/go-plugins v1.5.1
  7. google.golang.org/protobuf v1.25.0
  8. )
  9. replace google.golang.org/grpc v1.27.0 => google.golang.org/grpc v1.26.0

在server文件夹下创建main.go 文件

  1. package main
  2. import (
  3. "context"
  4. "github.com/micro/go-micro"
  5. "github.com/micro/go-micro/registry"
  6. "github.com/micro/go-micro/util/log"
  7. "github.com/micro/go-plugins/registry/consul"
  8. "github.com/baxiang/go-micro/hello/proto"
  9. )
  10. type Greeter struct{}
  11. func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
  12. rsp.Msg = "Hello " + req.Name
  13. return nil
  14. }
  15. func main() {
  16. // consul resister
  17. reg := consul.NewRegistry(func(op *registry.Options) {
  18. op.Addrs = []string{
  19. "127.0.0.1:8500",
  20. }
  21. })
  22. // Create a new service. Optionally include some options here.
  23. service := micro.NewService(
  24. micro.Registry(reg),
  25. micro.Name("service.greeter"),
  26. )
  27. // Init will parse the command line flags.
  28. service.Init()
  29. // Register handler
  30. proto.RegisterGreeterHandler(service.Server(), new(Greeter))
  31. // Run the server
  32. log.Fatal(service.Run())
  33. }

执行server启动命令

  1. $ go run server/main.go
  2. 2021-02-18 16:58:57.952352 I | Transport [http] Listening on [::]:56136
  3. 2021-02-18 16:58:57.952418 I | Broker [http] Connected to [::]:56137
  4. 2021-02-18 16:58:57.952688 I | Registry [consul] Registering node: service.greeter-4a8e58b8-8c12-4d7c-a458-2342989dbc41

Service Discovery

服务发现与注册组件,服务发现使用的是consul

  1. consul agent -dev

浏览器打开本地

http://localhost:8500/ui
图片.png

在client文件下创建main.go 文件

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/micro/go-micro"
  6. "github.com/micro/go-micro/registry"
  7. "github.com/micro/go-plugins/registry/consul"
  8. "github.com/baxiang/go-micro/hello/proto"
  9. )
  10. func main() {
  11. reg := consul.NewRegistry(func(op *registry.Options) {
  12. op.Addrs = []string{
  13. "127.0.0.1:8500",
  14. }
  15. })
  16. service := micro.NewService(
  17. micro.Registry(reg),
  18. micro.Name("greeter.client"),
  19. )
  20. service.Init()
  21. // Create new greeter client
  22. g := proto.NewGreeterService("service.greeter", service.Client())
  23. // Call the greeter
  24. rsp, err := g.Hello(context.Background(), &proto.Request{Name: "micro"})
  25. if err != nil {
  26. fmt.Println(err)
  27. }
  28. // Print response
  29. fmt.Println(rsp.Msg)
  30. }

执行客户端

  1. $ go run client/main.go
  2. Hello micro

常见错误

  1. panic: qtls.ConnectionState not compatible with tls.ConnectionState
  2. goroutine 1 [running]:
  3. github.com/lucas-clemente/quic-go/internal/handshake.init.1()
  4. /Users/baxiang/go/pkg/mod/github.com/lucas-clemente/quic-go@v0.13.1/internal/handshake/unsafe.go:17 +0x12e
  5. exit status 2

官方提示https://github.com/micro/micro/issues/1574
图片.png


grpc 版本,参考https://github.com/micro/micro/issues/1206

  1. # github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
  2. ../../../../../pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:114:78: undefined: resolver.BuildOption
  3. ../../../../../pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:182:31: undefined: resolver.ResolveNowOption
  4. # github.com/coreos/etcd/clientv3/balancer/picker
  5. ../../../../../pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/picker/err.go:37:44: undefined: balancer.PickOptions
  6. ../../../../../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版本

  1. replace google.golang.org/grpc v1.27.0 => google.golang.org/grpc v1.26.0

micro脚手架

编译micro

进入这个文件夹github.com/micro/micro

  1. cd $GOPATH/src/github.com/micro/micro

执行下面命令

  1. go install

如果出现如下的类似错误

  1. verifying github.com/lucas-clemente/quic-go@v0.12.0: checksum mismatch
  2. downloaded: h1:TRbvZ6F++sofeGbh+Z2IIyIOhl8KyGnYuA06g2yrHdI=
  3. go.sum: h1:dYHUyB50gEQlK3KqytmNySzuyzAcaQ3iuI2ZReAfVrE=

删除掉go.sum 文件,执行下面的命令

  1. rm go.sum
  2. go mod vendor

然后再次执行go install

HelloWorld

创建项目的命令是micro new

  1. $ micro new --help
  2. NAME:
  3. micro new - Create a service template
  4. USAGE:
  5. micro new [command options] [arguments...]
  6. OPTIONS:
  7. --namespace value Namespace for the service e.g com.example (default: "go.micro")
  8. --type value Type of service e.g api, fnc, srv, web (default: "srv")
  9. --fqdn value FQDN of service e.g com.example.srv.service (defaults to namespace.type.alias)
  10. --alias value Alias is the short name used as part of combined name if specified
  11. --plugin value Specify plugins e.g --plugin=registry=etcd:broker=nats or use flag multiple times
  12. --gopath Create the service in the gopath. Defaults to true.
  1. $ $ micro new baxiang.com/micro/service_example
  2. Creating service go.micro.srv.service_example in /Users/baxiang/go/src/baxiang.com/micro/service_example
  3. .
  4. ├── main.go
  5. ├── plugin.go
  6. ├── handler
  7. └── service_example.go
  8. ├── subscriber
  9. └── service_example.go
  10. ├── proto/service_example
  11. └── service_example.proto
  12. ├── Dockerfile
  13. ├── Makefile
  14. ├── README.md
  15. └── go.mod
  16. download protobuf for micro:
  17. brew install protobuf
  18. go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
  19. go get -u github.com/micro/protoc-gen-micro
  20. compile the proto file service_example.proto:
  21. cd /Users/baxiang/go/src/baxiang.com/micro/service_example
  22. protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/service_example/service_example.proto

然后进入项目文件夹

  1. cd $GOPATH/src/baxiang.com/micro/example

运行下面的代码生成protobuf和micro代码文件

  1. protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/service_example/service_example.proto

确保已经运行consul agent -dev,最后启动服务

  1. go run main.go

创建web

  1. $ micro new --type="web" baxiang.com/micro/web_example
  2. Creating service go.micro.web.web_example in /Users/baxiang/go/src/baxiang.com/micro/web_example
  3. .
  4. ├── main.go
  5. ├── plugin.go
  6. ├── handler
  7. └── handler.go
  8. ├── html
  9. └── index.html
  10. ├── Dockerfile
  11. ├── Makefile
  12. ├── README.md
  13. └── 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