go grpc安装与使用

1.安装
1. 安装protoc编译器

它可以把我们编写的 .proto文件编译成不同语言的代码
【go grpc安装与使用】下载
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-linux-x86_64.zip
解压
unzip protoc-3.11.4-linux-x86_64.zip
拷贝
protoc-3.11.4-linux-x86_64/bin下的protoc文件拷贝到 GOPATH
protoc-3.11.4-linux-x86_64/include下的google文件夹拷贝到/usr/local/include目录
2.安装grpc
直接go get安装会报错,被墙了,所以换成以下方式
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

安装上面最后一条时可能会报错,cannot find package "golang.org/x/sys/unix" in any of:golang.org也需要,可以用下面这种方式解决(如果没报错则跳过下面这一步就好了)
cd $GOPATH/src/golang.org/x git clone https://github.com/golang/sys.git

安装剩下的
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

没输出什么的话就安装成功

2.编写proto文件,编译成需要的语言代码(golang)
1.创建一个hello.proto
其实就是定义一个服务,然后后面需要实现它的接口
syntax = "proto3"package hello; service HelloService { rpc SayHello (HelloRequest) returns (HelloReply) {} }message HelloRequest { string name = 1; int age = 2; }message HelloReply { stringtime = 1; }

2.编译
protoc --go_out=plugins=grpc:. *.proto

会在当前文件夹生成hello.pb.go

3.目录树
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.实现并启动定义的服务
1. 在server/handler/handler.go文件中定义一个结构体,然后实现刚刚的SayHello方法
package handlerimport ( "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. 在server/service.go文件中编写启动服务
package mainimport ( "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. 启动服务
go run service.go

5.客户端调用服务
1. 需要将上面编译生成的hello.pb.go文件copy过来先,接着在client/main.go中调用服务
package mainimport ( 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. 测试一下
go run main.go
client输出:
2020-03-31 16:46:04.018755957 +0800 CST m=+5.300707152

server输出:
姓名: lubenwei 年龄: 21

    推荐阅读