当前位置:   article > 正文

mac下安装grpc protobuf_proto mac编码器下载

proto mac编码器下载

一、前言

这两天想研究一下grpc的Protobuf,于是在自己的mac电脑上进行尝试。把过程记录下来,给有需要的同学参考下,大家共同进步。

参考grpc官网:https://grpc.io

二、下载grpc代码包

使用dep包管理工具直接下载grpc代码包,命令为:

dep ensure -add google.golang.org/grpc

下载完成后,在Gopkg.toml文件里会新增以下内容:

  1. [[constraint]]
  2. name = "google.golang.org/grpc"
  3. version = "1.20.1"

三、安装protoc工具

安装protoc编译器,以生成grpc服务代码。

3.1 下载protoc源代码

下载protoc编译器源码地址为:https://github.com/protocolbuffers/protobuf/releases

笔者下载版本为protobuf-all-3.7.1.tar.gz

3.2 编译所需的依赖工具

编译protoc源代码需要的依赖工具为:autoconf、automake、libtool、curl、make、g++、unzip。

3.21.查看autoconf是否已安装,如果没有,使用brew安装:

$ brew install autoconf

3.2.2查看automake是否已安装,如果没有,使用brew安装:

$ brew install automake

3.2.3查看libtool是否已安装,如果没有,使用brew安装:

$ brew install libtool

3.2.4 查看curl是否已安装,如果没有,使用brew安装:

$ brew install curl

3.2.5 查看make是否已安装,如果没有,使用brew安装:

$ brew install make

3.2.6查看g++是否已安装,如果没有,使用brew安装:

$ brew install g++

3.2.7查看unzip是否已安装,如果没有,使用brew安装:

$ brew install unzip

3.3执行配置脚本

解压缩protobuf-all-3.7.1.tar.gz,进入解压后的目录,执行配置脚本:

  1. $ cd ~/Downloads/protobuf-3.7.1
  2. $ ./autogen.sh

3.4 编译与安装

  1. $ ./configure --prefix=/usr/local/protobuf
  2. $ make
  3. $ make check
  4. $ make install

3.5 设置环境变量

编辑.bash_profile配置文件,设置环境变量,重新加载配置文件:

  1. $ vi ~/.bash_profile
  2. #### protoc
  3. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
  4. export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/lib
  5. export PATH=$PATH:/usr/local/protobuf/bin
  6. $ source ~/.bash_profile

3.6 检查protoc编译工具是否安装成功

  1. $ protoc --version
  2. libprotoc 3.7.1

四、安装protoc-gen-go插件工具

安装protoc-gen-go插件工具,以支持生成go语言版本的服务代码。

4.1 下载protoc-gen-go插件工具的源代码包(注意:GOPATH路径为~/Go

$ go get -u github.com/golang/protobuf/protoc-gen-go

4.2 安装生成protoc-gen-go插件工具

  1. $ cd ~/Go/src/github.com/golang/protobuf/protoc-gen-go
  2. $ go install

安装完成后,就会发现:在GOPATH/bin目录下,已生成protoc-gen-go文件。

五、示例

5.1 protobuf示例

该示例源自gRPC-go的examples的helloworld,代码路径为:

vendor/google.golang.org/grpc/examples/helloworld/helloworld/helloworld.proto

先看下pb文件的内容:

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. option java_package = "io.grpc.examples.helloworld";
  4. option java_outer_classname = "HelloWorldProto";
  5. package helloworld;
  6. // The greeting service definition.
  7. service Greeter {
  8. // Sends a greeting
  9. rpc SayHello (HelloRequest) returns (HelloReply) {}
  10. }
  11. // The request message containing the user's name.
  12. message HelloRequest {
  13. string name = 1;
  14. }
  15. // The response message containing the greetings
  16. message HelloReply {
  17. string message = 1;
  18. }

该文件定义了一个服务Greeter,其中定义了一个API SayHello。该API接受参数为HelloRequest类型,返回值为HelloReply类型,这里HelloRequestHelloReply都是普通的PB定义。

使用protoc命令生成相关文件,命令为:

  1. $ cd grpc/examples/helloworld
  2. $ protoc -I helloworld/ helloworld.proto --go_out=plugins=grpc:helloworld

可以看到,在helloworld.proto同级目录下会生成对应的pb.go文件helloworld.pb.go。注意:在命令行中使用了plugins选项,提供对grpc的支持,否则不会生成Service的接口。

5.2  服务端程序

服务端程序示例如下:

  1. package main
  2. import (
  3. "log"
  4. "net"
  5. pb "your_path_to_gen_pb_dir/helloworld"
  6. "golang.org/x/net/context"
  7. "google.golang.org/grpc"
  8. )
  9. const (
  10. port = ":50051"
  11. )
  12. // server is used to implement helloworld.GreeterServer.
  13. type server struct{}
  14. // SayHello implements helloworld.GreeterServer
  15. func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
  16. return &pb.HelloReply{Message: "Hello " + in.Name}, nil
  17. }
  18. func main() {
  19. lis, err := net.Listen("tcp", port)
  20. if err != nil {
  21. log.Fatalf("failed to listen: %v", err)
  22. }
  23. s := grpc.NewServer()
  24. pb.RegisterGreeterServer(s, &server{})
  25. s.Serve(lis)
  26. }

可以看到,首先定义一个server结构,实现SayHello的接口,该接口定义在“your_path_to_gen_pb_dir/helloworld.proto”中。

然后,在main方法中调用grpc.NewServer() 方法创建一个server s。

接着,调用 pb.RegisterGreeterServer(s, &server{})方法将自定义的server注册到这个server s中。

最后,将创建的net.Listener传给s.Serve()

这样,就可以开始监听,并提供服务了,类似HTTP的ListenAndServe。

5.3 客户端程序

客户端程序示例如下:

  1. package main
  2. import (
  3. "log"
  4. "os"
  5. pb "your_path_to_gen_pb_dir/helloworld"
  6. "golang.org/x/net/context"
  7. "google.golang.org/grpc"
  8. )
  9. const (
  10. address = "localhost:50051"
  11. defaultName = "world"
  12. )
  13. func main() {
  14. // Set up a connection to the server.
  15. conn, err := grpc.Dial(address, grpc.WithInsecure())
  16. if err != nil {
  17. log.Fatalf("did not connect: %v", err)
  18. }
  19. defer conn.Close()
  20. c := pb.NewGreeterClient(conn)
  21. // Contact the server and print out its response.
  22. name := defaultName
  23. if len(os.Args) > 1 {
  24. name = os.Args[1]
  25. }
  26. r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
  27. if err != nil {
  28. log.Fatalf("could not greet: %v", err)
  29. }
  30. log.Printf("Greeting: %s", r.Message)
  31. }

可以看到,首先调用grpc.Dial方法产生一个grpc的ClientConn;然后,以这个ClientConn为参数,调用pb.NewGreeterClient()创建一个client;最后,通过这个client,直接调用对应服务器端的接口。

注意:在编译执行helloworld/greeter_server和helloworld/greeter_client时,如果提示proto.ProtoPackageIsVersion3不存在,那么修改下Gopkg.toml文件中关于grpc的部分:

  1. [[constraint]]
  2. name = "google.golang.org/grpc"
  3. branch = "master"

然后,用dep包管理工具重新加载下,就ok了。命令为:

$ dep ensure -update

原因为dep ensure默认加载的是proto2版本,但示例中使用的时proto3版本,升级下代码包版本就可以了。

六、结束语

初次接触grpc和protobuf,要学的还有很多。本文仅把mac下学习安装protoc工具和protoc-gen-go插件工具的过程记录下来,提供给有需要的同学参考下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/807622
推荐阅读
相关标签
  

闽ICP备14008679号