Skip to content

windows环境生成Protobuf消息和客户端服务存根 #53

Description

@huangshuwei

前言

windows环境生成Protobuf消息和客户端服务存根(Generate Protobuf Messages and Client Service Stub On Windows)

示例项目:https://github.com/huangshuwei/vue-grpc-demo

配置 protoc

protoc is the main protocol buffer code generator tool.

1、 下载 protoc 。 下载地址
根据你的 windows 系统的环境,下载对应的包,截止目前我用的是 “protoc-3.15.8-win64.zip”

2、将解压后的文件放到合适的位置,解压下载文件后,配置系统环境变量,定位到 bin 目录即可。如:

C:\Program Files\grpc-plugins\protoc-3.15.8-win64\bin

3、验证 protoc 是否配置成功
打开命令工具,输入后如果正常返回版本号即为正确:

protoc  --version

配置 protoc-gen-grpc-web

protoc-gen-grpc-web is a plugin, for protoc, that supplies additional capabilities to generate stub classes for grpc-web services.

1、 下载 protoc-gen-grpc-web。 下载地址
根据你的 windows 系统的环境,下载对应的包,截止目前我用的是 “protoc-gen-grpc-web-1.2.1-windows-x86_64.exe”

2、将此文件放到合适的位置,并且需要改名为 “protoc-gen-grpc-web.exe”,此处记得一定要改名,否则命令找不到。配置系统环境变量,定位到 protoc-gen-grpc-web.exe 的外层目录即可。如:

C:\Program Files\grpc-plugins\protoc-gen-grpc-web

3、验证 protoc-gen-grpc-web 是否配置成功。命令行输入 rotoc-gen-grpc-web 不报错,即为配置成功

protoc-gen-grpc-web

生成Protobuf消息和客户端服务存根

此处参考官方文档

protoc 以及 protoc-gen-grpc-web 都配置完成后,就可以生成Protobuf消息和客户端服务存根。命令格式如下:

protoc -I=$DIR echo.proto \
    --js_out=import_style=commonjs:$OUT_DIR \
    --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$OUT_DIR

-I 指定 .proto 文件目录 和 .proto 文件,多个用 .proto 文件用空格分割。如 -I=./src a.proto b.proto
--js_out 指定 *_pd.js 文件的输出信息
--grpc-web_out 指定 *_grpc_web_pd.js 文件的输出信息

像这个示例项目:https://github.com/huangshuwei/vue-grpc-demo ,我们的目录结构是:

├─router
│
├─service
│  │  node-server.js
│  │
│  ├─grpc-proto 
│  │      helloworld.proto
│  │
│  └─grpc-server
│          helloworld_grpc_web_pb.js
│          helloworld_pb.js
│
└─views

此时我们就可以执行如下命令,将 helloworld.proto 文件生成Protobuf消息和客户端服务存根(首先定位到该目录):

protoc -I=./service/grpc-proto helloworld.proto --js_out=import_style=commonjs:./service/grpc-server --grpc-web_out=import_style=commonjs,mode=grpcwebtext:./service/grpc-server

总结

下一篇将介绍 .proto 协议缓冲文件介绍

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions