go grpc Installation and Use

1. Install

1. Install compiler protoc

It can we write the code .proto documents translated into different languages

download

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-linux-x86_64.zip

Decompression
unzip protoc-3.11.4-linux-x86_64.zip

Copies
will be protoc-3.11.4-linux-x86_64/binunder the protocfiles are copied to GOPATHthe next
will be protoc-3.11.4-linux-x86_64/includein the googlefolder are copied to /usr/local/includea directory

2. Install grpc
directly go get the installation error, by a wall, so replaced by the following way

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text 

Installation above error may be the last time, cannot find package "golang.org/x/sys/unix" in any of:golang.orgalso need over the wall, it can be solved by following this way (if not the following error skip this step just fine)

cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/sys.git

Install the remaining

go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
cd $GOPATH/src/
go install google.golang.org/grpc

What did the installation is successful, then the output


2. Write proto file, compiled into the required language code (golang)

1. Create a hello.proto
is actually the definition of a service, and then behind it need to implement an interface

syntax = "proto3"

package hello;

service HelloService {
	rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
	string name = 1;
	int age = 2;
}

message HelloReply {
	string  time = 1;
}

2. Compile

protoc --go_out=plugins=grpc:. *.proto

Will be generated in the current folder filehello.pb.go


3. tree

grpctest/
├── client
│   ├── go.mod
│   ├── go.sum
│   ├── main.go
│   └── proto
│       └── hello
│           ├── hello.pb.go
│           └── hello.proto
└── server
    ├── go.mod
    ├── go.sum
    ├── handler
    │   └── handler.go
    ├── proto
    │   └── hello
    │       ├── hello.pb.go
    │       └── hello.proto
    └── service.go

4. Implement and start the defined service

1. In server/handler/handler.gothe definition of a file structure, and to achieve just the SayHellomethod of

package handler

import (
	"context"
	"fmt"
	hellopb "server/proto/hello"
	"time"
)

type hellosrv struct {}

func NewHelloSrv() hellopb.HelloServiceServer {
	return &hellosrv{}
}

func (*hellosrv)SayHello(ctx context.Context, in *hellopb.HelloRequest) (*hellopb.HelloReply, error) {
	fmt.Println("姓名:", in.Name, "年龄:", in.Age)
	var time string = time.Now().String()
	return &hellopb.HelloReply{Time: time}, nil
}

2. In the server/service.gopreparation of starting the service file

package main

import (
	"fmt"
	"net"
	hellopb "server/proto/hello"
	"server/handler"
	"google.golang.org/grpc"
)

const (
	PORT = ":7778"
)

func main() {
	lis, err := net.Listen("tcp", PORT)
	if err != nil {
		fmt.Println(err)
	}

	grpcService := grpc.NewServer()
	
	 //注册服务
	hellopb.RegisterHelloServiceServer(grpcService, handler.NewHelloSrv())
	if err := grpcService.Serve(lis); err != nil {
		fmt.Println(err)
	}
}

3. Start Service
go run service.go


The client calls service

1. The need to compile the above-generated hello.pb.gofile copy came first, followed by the client/main.gocall to service

package main

import (
	hellopb "client/proto/hello"
	"google.golang.org/grpc"
	"context"
	"fmt"
	"time"
)

const (
	ADDRESS = "127.0.0.1:7778"
)

func main() {
	conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure())
	if err != nil {
		fmt.Println(err)
	}
	defer conn.Close()

	client := hellopb.NewFirstServiceClient(conn)
	
	resp, err := client.SayHello(context.Background(), &hellopb.HelloRequest{Name: "lubenwei", Age: "21"})
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(resp.Time)
}

2. test
go run main.go

client output:

2020-03-31 16:46:04.018755957 +0800 CST m=+5.300707152

serverOutput:

姓名: lubenwei 年龄: 21
Published 48 original articles · won praise 56 · views 20000 +

Guess you like

Origin blog.csdn.net/zhetmdoubeizhanyong/article/details/105223689