目录

gRPC 和 Protobuf

目录

概念

  1. protobuf 简介

什么是 Google Protocol Buffer?

Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,他们用于 RPC 系统和持续数据存储系统。

Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。

  1. grpc 简介 正如其他 RPC 系统,gRPC 基于如下思想:定义一个服务, 指定其可以被远程调用的方法及其参数和返回类型。gRPC 默认使用 protocol buffers 作为接口定义语言,来描述服务接口和有效载荷消息结构。如果有需要的话,可以使用其他替代方案。

链接

https://protobuf.ked.pub

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:
1
2
3
4
5
6
7
$ PROTOBUF_VERSION=3.11.0
$ curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz
$ tar xzf protobuf-all-$PROTOBUF_VERSION.tar.gz
$ cd protobuf-$PROTOBUF_VERSION
$ ./configure --disable-shared #可能需要 --disable-dependency-tracking
$ make   # You may want to pass -j to make this run faster; see make --help
$ sudo make install

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:

1
$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
Notes for Visual C++

When building on Windows and VC++, you need to specify project properties for Gradle to find protobuf:

1
2
3
4
.\gradlew publishToMavenLocal ^
    -PvcProtobufInclude=C:\path\to\protobuf-3.11.0\src ^
    -PvcProtobufLibs=C:\path\to\protobuf-3.11.0\vsprojects\Release ^
    -PtargetArch=x86_32

Since specifying those properties every build is bothersome, you can instead create \gradle.properties with contents like:

1
2
3
vcProtobufInclude=C:\\path\\to\\protobuf-3.11.0\\src
vcProtobufLibs=C:\\path\\to\\protobuf-3.11.0\\vsprojects\\Release
targetArch=x86_32

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 \gradle.properties.

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 /gradle.properties:

1
protoc=/path/to/protoc

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

1
2
3
4
5
6
$ go get -u github.com/golang/protobuf/protoc-gen-go
$  
$ go build
$ git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
$ cd $GOPATH/src/google.golang.org/grpc
$ go insall

Rebuild the generated Go code

1
$ go generate google.golang.org/grpc/examples/helloworld/...

Or run protoc command (with the grpc plugin)

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

下载链接

https://protobuf.ked.pub/www

1
2
3
4
5
6
include/                                           19-Feb-2020 13:02                   -
linux/                                             19-Feb-2020 13:02                   -
macos/                                             18-Feb-2020 00:32                   -
windows/                                           22-Feb-2020 06:56                   -
for_download_urls.txt                              19-Feb-2020 15:24                  41
protobuf-all-3.11.4.tar.gz                         17-Feb-2020 08:02             7408292