Golang gRPC practice a gRPC Introduction and serial

Introduction and gRPC

A high performance, open source, general RPC framework that puts mobile and HTTP/2 first. ——gRPC Website

gRPC  is a high-performance, open source, generic RPC framework introduced by Google, based on HTTP / 2 protocol standard design and development, using the default Protocol Buffers data serialization protocol, supports a variety of development languages. gRPC provides a simple way to define a precise services, and client and server reliable automatic generation libraries.

Key Features

  • Powerful IDL
    GRPC use ProtoBuf define the service, ProtoBuf was developed by Google A data serialization protocol (similar to XML, JSON, hessian). ProtoBuf possible to serialize the data, and widely used in data storage, communication protocol.

  • Multi-language support
    gRPC supports multiple languages, and can automatically generate client and server libraries based on language. Now provides a C version grpc, Java version grpc-java and Go version grpc-go, other language versions under active development, which, grpc support for C, C ++, Node.js, Python , Ruby, Objective-C, PHP C # and other languages, grpc-java already supports Android development.

  • The HTTP / 2
    GRPC the HTTP / 2 based on standard design, so compared to other frame RPC, GRPC brings more power, bidirectional flow, header compression, multiplexing multiple requests and the like. These features bring significant benefits to mobile devices, such as saving bandwidth and reduce the number of TCP link, saving CPU usage and extend battery life. Meanwhile, gRPC also able to improve the performance of cloud services and Web applications. gRPC both to the client application, the server side can also be applied to a transparent manner to build a communication system and simplify the communication client and server.

In gRPC client can directly call a remote procedure on different servers, using the posture looks like calling a local program, as it is easy to build distributed applications and services. And many RPC systems, the server is responsible for implementing defined interfaces and processes the request of the client, the client calls the service needs directly from the interface description. The client and server can use different languages ​​gRPC support implementation respectively.

image description

grpc library and protobuf

The default compiler requires a separate installation grpc and protobuf compiler, grpc / homebrew-grpc project provides a plug-in rapid installation scripts can be installed directly grpc, protobuf compilers and other language compilers need. go language plug-in requires a separate installation.

Ready to work

installation

Script Installation

curl -fsSL https://goo.gl/getgrpc bash ----with-plugins

Or directly mounted brew:

brew tap grpc/grpc
brew install --with-plugins grpc

Installation results:

/usr/local/bin

protoc
grpc_cpp_plugin
grpc_node_plugin
grpc_python_plugin
grpc_csharp_plugin
grpc_objective_c_plugin
grpc_ruby_plugin

This installation will be installed gRPc C / C ++ libraries and other supported languages proto-ins and protobuf compiler. If less than these languages, you can install only protobuf like, refer to the project: protobuf . java and go to support independent projects grpc-java and GRPC-go .

Golang protobuf plug

Project Address: golang / protobuf

installation:

  • Requirements golang version> 1.4

  • Prior to the installation requirements protocol buffer compiler

run:

go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

Installed after compiling protoc-gen-gothe $GOBINdirectory, by default  $GOPATH/bin. The directory must be in the system environment variables $PATH, so the compiler can find the plug-in protocol when compiling .proto file.

grpc-go

# 如果需要翻墙自己解决吧
go get -u google.golang.org/grpc

The compiler uses

Use protoccommand to compile .protofiles, support for different languages need to specify 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

Detailed here golang compile posture:

  • -I Parameters: Specify the import path, you can specify multiple -Iparameters, in order to find compile-time, find the current default directory is not specified

  • --go_out : Golang compilation support, supports the following parameters

    • plugins=plugin1+plugin2 - Specify the plug-in, currently only supports grpc, namely:plugins=grpc

    • M Parameters - golang package names corresponding to the specified file path is compiled .proto introduced (this parameter is not specified the default is the .protofile importpath statement)

    • import_prefix=xxx - all importadd the path prefix, is mainly used to compile multiple files in a subdirectory proto, this parameter is arguably useful, especially for some of the situations when alternative Margument, but there is a pain egg problem leads to practical use and can not achieve we desired effect, try it yourself to see it

    • import_path=foo/bar - Used to specify undeclared packageor go_packagepackage name of the file, rightmost characters before the slash will be ignored

    • end :编译文件路径 .proto文件路径(支持通配符)

Complete example:

protoc -I . --go_out=plugins=grpc,Mfoo/bar.proto=bar,import_prefix=foo/,import_path=foo/bar:. ./*.proto

reference

This series of sample code

Related documents:

Related projects

Guess you like

Origin www.cnblogs.com/ExMan/p/12163115.html