Quick Start

Overview

This paper will describe how to quickly use gRPC customers to connect to rpc.

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 client.go

Reference configuration and content

  • etc/greet-client.yaml
  • client.go
  1. Target: 127.0.0.1:8080
  1. func main() {
  2. var clientConf zrpc.RpcClientConf
  3. conf.MustLoad("etc/client.yaml", &clientConf)
  4. conn := zrpc.MustNewClient(clientConf)
  5. resp, err := client.Ping(context.Background(), &greet.Request{})
  6. if err != nil {
  7. log.Fatal(err)
  8. return
  9. }
  10. log.Println(resp)
  11. }
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 connections (service register, service discovery, cluster direct, intermediate use etc.) can be consulted rpc service connection

References