gRPC 和 Protobuf
目录
概念
- protobuf 简介
什么是 Google Protocol Buffer?
Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,他们用于 RPC 系统和持续数据存储系统。
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。
- grpc 简介 正如其他 RPC 系统,gRPC 基于如下思想:定义一个服务, 指定其可以被远程调用的方法及其参数和返回类型。gRPC 默认使用 protocol buffers 作为接口定义语言,来描述服务接口和有效载荷消息结构。如果有需要的话,可以使用其他替代方案。
链接
protobuf 使用方法
https://github.com/protocolbuffers/protobuf/releases
编译
https://github.com/grpc/grpc-java/blob/master/COMPILING.md
How to Build Code Generation Plugin
This section is only necessary if you are making changes to the code generation. Most users only need to use skipCodegen=true as discussed above.
Build Protobuf
The codegen plugin is C++ code and requires protobuf 3.0.0 or later.
For Linux, Mac and MinGW:
|
|
If you are comfortable with C++ compilation and autotools, you can specify a –prefix for Protobuf and use -I in CXXFLAGS, -L in LDFLAGS to reference it. The environment variables will be used when building grpc-java.
Protobuf installs to /usr/local by default.
For Visual C++, please refer to the Protobuf README for how to compile Protobuf. gRPC-java assumes a Release build.
Mac
Some versions of Mac OS X (e.g., 10.10) doesn’t have /usr/local in the default search paths for header files and libraries. It will fail the build of the codegen. To work around this, you will need to set environment variables:
|
|
Notes for Visual C++
When building on Windows and VC++, you need to specify project properties for Gradle to find protobuf:
|
|
Since specifying those properties every build is bothersome, you can instead create
|
|
By default, the build script will build the codegen for the same architecture as the Java runtime installed on your system. If you are using 64-bit JVM, the codegen will be compiled for 64-bit. Since Protobuf is only built for 32-bit by default, the targetArch=x86_32 is necessary.
Notes for MinGW on Windows
If you have both MinGW and VC++ installed on Windows, VC++ will be used by default. To override this default and use MinGW, add -PvcDisable=true to your Gradle command line or add vcDisable=true to your
Notes for Unsupported Operating Systems
The build script pulls pre-compiled protoc from Maven Central by default. We have built protoc binaries for popular systems, but they may not work for your system. If protoc cannot be downloaded or would not run, you can use the one that has been built by your own, by adding this property to
|
|
java : grpc
build
https://github.com/grpc/grpc-java/tree/master/compiler 编译方法
如果出错,参考 https://www.cnblogs.com/ghj1976/p/5454881.html
现成的 java 库
Normally you don’t need to compile the codegen by yourself, since pre-compiled binaries for common platforms are available on Maven Central:
Navigate to https://mvnrepository.com/artifact/io.grpc/protoc-gen-grpc-java
go lang : grpc
Install protobuf compiler
Install the protoc Go plugin
|
|
Rebuild the generated Go code
|
|
Or run protoc command (with the grpc plugin)
|
|
下载链接
|
|