🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> ### 服务端 ~~~ package main import ( "log" "github.com/micro/go-micro" proto "exchange/service-grpc/greeter/proto" "golang.org/x/net/context" ) type Greeter struct{} func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error { rsp.Greeting = "Hello " + req.Name return nil } func main() { service := micro.NewService( micro.Name("greeter"), ) service.Init() proto.RegisterGreeterHandler(service.Server(), new(Greeter)) if err := service.Run(); err != nil { log.Fatal(err) } } ~~~ 复制 > ### 客户端 ~~~ package main import ( "github.com/micro/go-micro" proto "exchange/service-grpc/greeter/proto" "fmt" "golang.org/x/net/context" ) func main() { service := micro.NewService( micro.Name("greeter"), ) service.Init() greeter := proto.NewGreeterClient("greeter", service.Client()) // 在Greeter handler上请求调用Hello方法 rsp, err := greeter.Hello(context.TODO(), &proto.HelloRequest{ Name: "John", }) if err != nil { fmt.Println(err) return } fmt.Println(rsp.GetGreeting()) } ~~~ > ### 相关阅读 * [使用Go Micro编写微服务](http://btfak.com/%E5%BE%AE%E6%9C%8D%E5%8A%A1/2016/03/28/go-micro/)