Quick Start

Overview

This paper will describe how to quickly develop a rpc server

Sample

Preparation

We do the following execution to generate a proto file and pb file.

  1. # Create demo service directory
  2. $ mkdir demo && cd demo
  3. # go mod initializing
  4. $ go mod init demo
  5. # Generate greet. roto file
  6. $ goctl rpc -o greet.proto
  7. # pun pb.go file
  8. $ protoc greet.proto --go_out=. --go-grpc_out=.
  9. # Create client directory
  10. $ mkdir client && cd client
  11. # Add profile
  12. $ mkdir etc && cd etc
  13. $ touch gret-client. aml
  14. # Add client.go file
  15. $ touch server.go

yaml configuration content and server.go code below

  • etc/greet-server.yaml
  • server.go
  1. Name: greet.rpc
  2. ListenOn: 127.0.0.1:8080
  1. func main() {
  2. func main() {
  3. var serverConf zrpc.RpcServerConf
  4. conf.MustLoad("etc/greet-server.yaml", &serverConf)
  5. s := zrpc.MustNewServer(serverConf, func(server *grpc.Server) {
  6. greet.RegisterGreetServer(server, &exampleServer{})
  7. })
  8. defer s.Stop()
  9. s.Start()
  10. }
  11. }
  12. type exampleServer struct {
  13. greet.UnimplementedGreetServer
  14. }
  15. func (e *exampleServer) Ping(ctx context.Context, request *greet.Request) (*greet.Response, error) {
  16. // fill your logic here
  17. return &greet.Response{}, nil
  18. }
Quick Start - 图1small trick

If you also find it troublesome to write the code, please try goctl scroll code generated, refer to goctl rpc

More gRPC usage (service register, service discovery, intermediate use etc.) can be consulted rpc service registration

References