grpc 使用

1.第一步安装 protobuf

传送门:https://blog.csdn.net/u014534808/article/details/80203018

在此页面选择合适的版本,我选择的是最新的3.5.0,需要注意的mac下是不要下载protoc-3.5.1-xxx的包,因为这些包缺少相关命令,会导致后面安装失败。 此处我推荐安装protobuf-all-3.5.0.tar.gz

2. 解压

tar -zxf protobuf-all-3.5.0.tar.gz

3. 跳转到解压后的目录

cd  protobuf-3.5.0

4. 设置编译目录

./configure --prefix=/usr/local/protobuf
/usr/local/protobuf/ 为自己配置的编译安装目录

5. 安装

还是在解压的目录下进行

make
make install

6. 配置环境变量

sudo  vim .bash_profile

7. 添加配置文件

export PROTOBUF=/usr/local/protobuf
export PATH=$PROTOBUF/bin:$PATH
PS: 如果第七步数据保存不了可以先切换到root 用户进行保存
sudo -i
source .bash_profile

9. 测试安装结果

输入protoc --version
看到如下结果表示安装成功:

protoc --version
libprotoc 3.5.0

拉取grpc代码

## github 拉取代码错误
cd src/google.golang.org
git clone https://github.com/grpc/grpc-go grpc 报错
fatal: unable to access 'https://github.com/grpc/grpc-go/': Failed to connect to github.com port 443: Operation timed out

修改站点两个地址
打开ipaddress.com,查询如下两个域名,并分别记录下其对应的ip:
1、github.com
2、github.global.ssl.fastly.net

安装 Go 的 protoc 插件:

# go get -u github.com/golang/protobuf/protoc-gen-go 失败的话
cd src/google.golang.org
git clone https://github.com/google/go-genproto genproto

编写用于gRPC的pb文件

# cd $GOPATH/src/
# mkdir example
# cd example
# mkdir helloworld
# vim helloworld/helloworld.proto
syntax = "proto3";
option go_package = ".;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;
}

编译pb生成go代码

# protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

编写服务

编写服务执行就可以了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • python 1、使用前准备,安装这三个库 2、建立一个proto文件hello.proto 3、执行命令就会对应...
    hw_owen阅读 2,750评论 0 1
  • Fabric的节点通过grpc向内部或外部提供接口,在学习源码之前,需要对grpc的基本使用有所了解,并了解如何在...
    史圣杰阅读 4,432评论 0 0
  • RPC 远程过程调用 可以区别于IPC A想要调用B服务器上的提供的函数/方法 单一 RPC 无法实现 push,...
    转身是天涯阅读 11,115评论 3 5
  • 1.前言 gRPC是一个开源的高性能并且能在任何环境中运行的RPC框架,其采用protocol buffer: p...
    howie6879阅读 4,114评论 0 1
  • 夜莺2517阅读 127,787评论 1 9