A,protobufConfiguration of the environment

  • 1. Download the version for your computer

  • 2. Run the go env command to check your go path address and create a bin folder under the go path directory

  • 3, unzip the downloads from the first point and copy the protoc under bin to the bin folder under go path

  • 4. View the installed version

    protoc --version
    Copy the code
  • 5, Protoc-gen-Go installation (for generating micro code corresponding to GO language)

    go get -u github.com/golang/protobuf/proto
    go get -u github.com/golang/protobuf/protoc-gen-go
    Copy the code
  • 6. Check the following files in the bin directory under go Path

    ➜ bin tree.. ├── Protoc ├─ protoc-gen-g 0 directories, 2 files ➜ binCopy the code
  • 7, MAC operating system can use another installation method (recommended)

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    Copy the code
    brew install protobuf
    Copy the code
    protoc --version
    Copy the code

Second, write,protobuffile

  • 1. Simple code files

    syntax = "proto3";
    option go_package = ".; proto";
    service Greeter {
      rpc SayHello (HelloRequest) returns (HelloReply);
    }
    message HelloRequest {
      string name = 1;
    }
    
    message HelloReply {
      string message = 1;
    }
    Copy the code
  • 2. Run the script to generate the GO file

    # protoc -i. File name --go_out=plugins= GRPC:
    protoc -I . helloWorld.proto --go_out=plugins=grpc:.
    Copy the code

Third, the preparation of the service layer

  • 1. Install dependency packages

    go get -u google.golang.org/grpc
    Copy the code
  • 2, the service layer code server.go file

    package main
    
    import (
    	"context"
    	"gin_stuty/07.grpc/proto"
    	"google.golang.org/grpc"
    	"net"
    )
    
    type Server struct{}SayHello binds a method to a structure (similar to adding a method to a class)
    func (s *Server) SayHello(ctx context.Context, in *proto.HelloRequest) (*proto.HelloReply, error) {
    	return &proto.HelloReply{
    		Message: "hello" + in.Name,
    	}, nil
    }
    func main(a) {
     	g := grpc.NewServer()
     	proto.RegisterGreeterServer(g, &Server{})
     	listen, err := net.Listen("tcp"."0.0.0.0:8088")
    	iferr ! =nil {
    		panic("Service listening error :" + err.Error())
    	}
    	err = g.Serve(listen)
    	iferr ! =nil {
    		panic("Service startup failed :" + err.Error())
    	}
    }
    Copy the code

Third, the client method

  • 1. Create a client.go file

  • 2. The client.go file is similar to the dial-up server

    package main
    
    import (
    	"context"
    	"fmt"
    	"gin_stuty/07.grpc/proto"
    	"google.golang.org/grpc"
    )
    
    func main(a) {
      // Start dialing
    	conn, err := grpc.Dial("0.0.0.0:8088", grpc.WithInsecure())
    	iferr ! =nil {
    		panic("Dialing error :" + err.Error())
    	}
    	defer conn.Close()
    
    	c := proto.NewGreeterClient(conn)
    	r, err := c.SayHello(context.Background(), &proto.HelloRequest{Name: "Water mark"})
    	iferr ! =nil {
    		panic("Sending error" + err.Error())
    	}
    	fmt.Println("Receiving server-side data :", r.Message)
    }
    Copy the code