go-micro
1 安装
- 安装golang version>=1.13
- micro-gen: go get github.com/micro/protoc-gen-micro/v2
protoc --proto_path=. --micro_out=. --go_out=. proto/greeter.proto
- import: "github.com/micro/go-micro/v2"
- go get github.com/micro/micro@v1.18 //v2编译失败
- demo
package main
import (
"context"
"fmt"
proto "micro_test/proto"
"github.com/micro/go-micro/v2"
)
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Name("greeter"),
)
// Init will parse the command line flags.
service.Init()
// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the server
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
来源:oschina
链接:https://my.oschina.net/u/4215839/blog/3167158