go micro 实践笔记1

243 阅读1分钟
  1. 构建项目:hello

进入要存放项目的目录 $micro new --gopath=false hello

├── main.go
├── generate.go
├── plugin.go
├── handler
│   └── hello.go
├── subscriber
│   └── hello.go
├── proto
│   └── hello
│       └── hello.proto
├── Dockerfile
├── Makefile
├── README.md
├── .gitignore
└── go.mod

download protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:
visit https://github.com/protocolbuffers/protobuf/releases

download protobuf for micro:

go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v2/cmd/protoc-gen-micro

compile the proto file hello.proto:

cd hello
make proto
  1. 生成protobuf文件

protoc --proto_path=proto --go_out=proto --micro_out=proto proto/hello.proto

image.png

  1. 启动服务

$ micro server //启动相关服务等
$ go run main.go //启动hello

 go.micro.service.hello
2021-04-23 15:11:16  file=grpc/grpc.go:864 level=info Server [grpc] Listening on
 [::]:60735
2021-04-23 15:11:16  file=grpc/grpc.go:881 level=info Broker [http] Connected to
 127.0.0.1:60736
2021-04-23 15:11:16  file=grpc/grpc.go:697 level=info Registry [mdns] Registerin
g node: go.micro.service.hello-190bc41b-94a3-4903-b892-01c3d67ac307

2021-04-23 15:11:17  file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.hello
2021-04-23 15:18:11  file=handler/hello.go:15 level=info Received Hello.Call request
  1. 服务调用测试

$ go run cmd/cli/main.go

2021-04-23 15:18:11.870405 I | call func success! Hello xiao xie
2021-04-23 15:18:11.972361 I | send msg success!

笔记与文件