This is a creation in Article, where the information may have evolved or changed.
GRPC Introduction and Installation
A High performance, open source, general RPC framework that puts mobile and HTTP/2 first. --grpc Website
GRPC is a high-performance, open-source, common RPC framework, launched by Google, designed and developed based on the HTTP/2 protocol Standard, which uses the protocol buffers data serialization protocol to support multiple development languages by default. GRPC provides an easy way to define services precisely, and automatically generates a reliable library of functions for both the client and the server.
Key Features
The powerful IDL
GRPC uses PROTOBUF to define services, PROTOBUF is a data serialization protocol developed by Google (similar to XML, JSON, Hessian). Protobuf is capable of serializing data and is widely used in data storage, communication protocols, and so on.
Multi-lingual support
GRPC supports multiple languages and is able to automatically generate client and server function libraries based on language. Version c Grpc, Java version Grpc-java and go version grpc-go are currently available, and other languages are actively under development, GRPC support C, C + +, node. js, Python, Ruby, Objective-c, PHP, and C # and other languages, Grpc-java has supported Android development.
Http/2
GRPC is designed based on the HTTP/2 standard, so GRPC brings more powerful features than other RPC frameworks, such as bidirectional streaming, head compression, multiplexing requests, and more. These features provide significant benefits to mobile devices such as bandwidth savings, reduced TCP links, CPU usage, and battery life. At the same time, GRPC can improve the performance of cloud services and Web applications. The GRPC can be applied both on the client and the server side, thus transparently enabling client and server-side communication and simplifying the construction of communication systems.
The GRPC client can directly invoke remote programs on different servers, using gestures that look like a local program, and easily build distributed applications and services. Like many RPC systems, the server is responsible for implementing a well-defined interface and processing client requests, and the client invokes the required services directly based on the interface description. The client and server can be implemented separately using the different languages supported by GRPC.
GRPC Library and Protobuf
By default, the GRPC and protobuf compilers need to be compiled separately, and the GRPC/HOMEBREW-GRPC project provides a quick install script that installs the plug-ins required for GRPC, PROTOBUF compilers, and other language compilers directly. The Go language plugin needs to be installed independently.
Preparatory work
Installation
Script Installation
curl -fsSL https://goo.gl/getgrpc | bash -s -- --with-plugins
Or use the Brew installation directly:
brew tap grpc/grpcbrew install --with-plugins grpc
Installation Result:
/usr/local/binprotocgrpc_cpp_plugingrpc_node_plugingrpc_python_plugingrpc_csharp_plugingrpc_objective_c_plugingrpc_ruby_plugin
This installation installs the GRPC/C + + libraries and other language-enabled proto plugins and Protobuf compilers. If you do not use these languages, you can just install PROTOBUF, reference project: Protobuf. Java and go support have standalone projects Grpc-java and grpc-go.
Golang Protobuf Plug-in
Project Address: Golang/protobuf
Installation:
Run:
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
It will be installed to the directory when it is compiled protoc-gen-go
$GOBIN
, by default $GOPATH/bin
. The directory must be in the environment variable of the system so that the $PATH
protocol compiler can find the plug-in when compiling the. proto file.
Grpc-go
# 如果需要翻墙自己解决吧go get -u google.golang.org/grpc
Compiler uses
Using protoc
the command to compile .proto
the file, different language support needs to specify the output parameters, such as:
protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR --go_out=DST_DIR --ruby_out=DST_DIR --javanano_out=DST_DIR --objc_out=DST_DIR --csharp_out=DST_DIR path/to/file.proto
Here is a detailed description of Golang's compilation posture:
-I
Parameters: Specify the import path, you can specify multiple -I
parameters, compile-time lookup in order, do not specify the default to find the current directory
--go_out
: Golang compile support, support the following parameters
plugins=plugin1+plugin2
-Specifies the plugin, currently only supports GRPC, i.e.:plugins=grpc
M
Parameters-Specifies the Golang package name for the imported. Proto file path after compilation (this parameter is not specified by default is .proto
import
the path to the statement in the file)
import_prefix=xxx
-Prefixes all import
paths, mainly used to compile multiple proto files within subdirectories, which is arguably useful, especially when it comes to replacing some cases M
, but the actual use of an egg-ache problem is not as effective as we expected, try it out for yourself.
import_path=foo/bar
-Used to specify the package name of the file that is not declared package
or go_package
, and the character before the rightmost slash is ignored
End:编译文件路径 .proto文件路径(支持通配符)
Complete Example:
protoc -I . --go_out=plugins=grpc,Mfoo/bar.proto=bar,import_prefix=foo/,import_path=foo/bar:. ./*.proto
Reference
Sample code for this series
Related documents:
Related projects
Grpc/grpc
Grpc-go
Grpc-java
Google/protobuf
Golang/protobuf
Grpc-middleware
Grpc-gateway