下载最新版本golang 1.14.2,
配置镜像,
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
安装runtime
go get -v github.com/micro/micro/v2
启动服务
micro server
查看以后服务
> micro list services
go.micro
go.micro.api
go.micro.auth
go.micro.bot
go.micro.broker
go.micro.config
go.micro.debug
go.micro.network
go.micro.proxy
go.micro.registry
go.micro.router
go.micro.runtime
go.micro.server
go.micro.store
go.micro.web
micro.http.broker
创建第一个微服务 Hello World
准备工作
下载安装protoc, 更多查看:
curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-osx-x86_64.zip
unzip protoc-3.11.4-osx-x86_64.zip
cd protoc-3.11.4-osx-x86_64
sudo mv bin/protoc /usr/local/bin/
sudo mv bin/google /usr/local/include/
下载后, proptoc无法直接打开, 进入 System Preferences -> Security & Privacy, 修改配置

安装依赖
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
创建微服务
❯ micro new --gopath --namespace=local test.local/helloworld
test.local/helloworld
执行结果一下:
❯ micro new --gopath --namespace=local test.local/helloworld
Creating service local.service.helloworld in /Users/work/go/src/test.local/helloworld
.
├── main.go
├── generate.go
├── plugin.go
├── handler
│ └── helloworld.go
├── subscriber
│ └── helloworld.go
├── proto/helloworld
│ └── helloworld.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 helloworld.proto:
cd /Users/work/go/src/test.local/helloworld
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/helloworld/helloworld.proto
编译proto文件helloworld.proto
cd /Users/work/go/src/test.local/helloworld
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/helloworld/helloworld.proto
proto/helloworld/helloworld.proto
package local.service.helloworld;
option go_package = "proto/helloworld;local_service_helloworld"; // 添加此行代码
再重新执行
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/helloworld/helloworld.proto
proto/helloworld/
▾ proto/helloworld/
helloworld.pb.go
helloworld.pb.micro.go
go build
go build -o helloworld-service *.go
然后执行
❯ ./helloworld-service
2020-05-14 23:07:46 file=service.go:206 level=info Starting [service] local.service.helloworld
2020-05-14 23:07:46 file=grpc.go:845 level=info Server [grpc] Listening on [::]:63090
2020-05-14 23:07:46 file=grpc.go:862 level=info Broker [http] Connected to 127.0.0.1:63091
2020-05-14 23:07:46 file=grpc.go:676 level=info Registry [mdns] Registering node: local.service.helloworld-c8ea4286-8814-4606-a400-8ca4498f502e
2020-05-14 23:07:46 file=grpc.go:711 level=info Subscribing to topic: local.service.helloworld
验证和使用
micro list services
❯ micro list services
...
local.service.helloworld
...
micro stats
❯ micro stats local.service.helloworld
service local.service.helloworld
version latest
node address:port started uptime memory threads gc
local.service.helloworld-c8ea4286-8814-4606-a400-8ca4498f502e 192.168.3.21:63090 May 14 23:07:46 3m42s 3.44mb 31 511.124µs
micro call
❯ micro call local.service.helloworld Helloworld.Call '{"name": "John"}'
{
"msg": "Hello John"
}