1 安装protobuf
1.1 进入https://github.com/google/protobuf/releases获取特定release版本的安装包
1.2 解压
tar xzvf protobuf-all-3.5.1.tar.gz
1.3 配置
cd protobuf-3.5.1 && ./configure
1.4 编译
make
如上图所以编译成功。
1.5 安装
make install
2 支持go语言
go get -a github.com/golang/protobuf/protoc-gen-go
3 安装gRPC
go get -u -v google.golang.org/grpc
4 例子
syntax = "proto3";
option go_package = "github.com/grpc/example/helloworld";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
运行编译命令
protoc helloworld.proto --go_out=plugins=grpc:output
得到文件hellopb.go,这是规定message的文件
下面是服务端(go):
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "./output/github.com/grpc/example/helloworld"
"google.golang.org/grpc/reflection"
)
const (
port = ":50051"
)
// server is used to implement helloworld.GreeterServer.
type server struct{}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
客户端(go):
package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "./output/github.com/grpc/example/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
9 问题
1)包太旧——由于更新了protoc,而go的包没有更新导致
hello/hello.pb.go:115: undefined: grpc.SupportPackageIsVersion4
hello/hello.pb.go:135: c.cc.Invoke undefined (type *grpc.ClientConn has no field or method Invoke)
方案:更新go包,并重新生成message文件hello.pb.go
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
2) grpc包太旧
hello/hello.pb.go:115: undefined: grpc.SupportPackageIsVersion4
hello/hello.pb.go:135: c.cc.Invoke undefined (type *grpc.ClientConn has no field or method Invoke)
方案:因为墙的原因,从github上的镜像获取包,再移动到包对应的位置
go get -u github.com/grpc/grpc-go
mv /opt/goHome/workspace/src/github.com/grpc/grpc-go /opt/goHome/src/google.golang.org/grpc
同样方式更新三个包
github.com/golang/text
github.com/golang/sys
github.com/golang/net