Grpc+Grpc Gateway Practice I Introduction and Environmental Installation

  golang, grpc, protobuf, rpc

Introduction and Environmental Installation

Suppose we have a project requirement and hope to use itRpcAs internalAPICommunication, but also want to provide externalRestful Api, write two sets of too cumbersome does not conform to

So we came up withGrpcas well asGrpc GatewayThis is what we need

grpc-gateway

Preparation link

At the official beginning of ourGrpc+Grpc GatewayBefore practice, we need to configure our development environment

  • Grpc
  • Protoc Plugin
  • Protocol Buffers
  • Grpc-gateway

Grpc

What is it?

Google is rightGrpcDefinition of:

A high performance, open-source universal RPC framework

That is,GrpcIs a high-performance, open source, general RPC framework with the following features:

  • PowerfulIDL, useProtocol BuffersAs a format for data exchange, supportsv2v3(recommendedv3)
  • Cross-language, cross-platform, that isGrpcSupport for multiple platforms and languages
  • HTTP2 support, two-way transmission, multiplexing, authentication, etc.

Installation

1. Official Recommendation (Scientific Internet Access Required)

go get -u google.golang.org/grpc

2. Adoptiongithub.com

Enter the first $GOTPATH directory (becausego getWill be installed by default under the first), newgoogle.golang.orgDirectory, pullgolangIngithubMirror library on:

cd /usr/local/go/path/src   

mkdir google.golang.org

cd google.golang.org/

git clone https://github.com/grpc/grpc-go

mv grpc-go/ grpc/

Directory structure:

google.golang.org/
└── grpc
    ...

And ingrpcThere are many commonly used packages, such as:

  • metadata: definedgrpcSupported metadata structures, methods in packages canMDAcquiring and processing
  • credentials: ImplementedgrpcThe various authentication credentials supported encapsulate all the states that the client needs to authenticate the server and make various assertions.
  • codes: definedgrpcThe standard error code used is generic

Protoc Plugin

What is it?

Compiler plug-in

Installation

go get -u github.com/golang/protobuf/protoc-gen-go

willProtoc PluginThe executable of moved from $GOPATH to under $GOBIN

mv /usr/local/go/path/bin/protoc-gen-go /usr/local/go/bin/

Protocol Buffers v3

What is it?

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the “old” format.

Protocol BuffersYesGoogleIntroduced a data description language that supports multiple languages and platforms. It is a binary format. Generally speaking, it is smaller, faster, simpler and more flexible. Currently, there arev2v3We recommend using the version ofv3

It is suggested that you can read the introduction of official documents. This series will briefly introduce the contents involved when using it.

Installation

wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-all-3.5.1.zip
unzip protobuf-all-3.5.1.zip
cd protobuf-3.5.1/
./configure
make
make install

Check whether the installation was successful

protoc --version

If an error is reported

protoc: error while loading shared libraries: libprotobuf.so.15: cannot open shared object file: No such file or directory

Then executeldconfigAfter that, it can be successfully run again.

Why do you want to implementldconfig

We can know from the information output from the console,Protocol Buffers LibrariesThe default installation path for is at/usr/local/lib

Libraries have been installed in:
   /usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.

We installed a new dynamic link library.ldconfigIt usually runs at system startup, so this will not be found now.libSo we have to do it manually.ldconfig,Let the dynamic link library be shared by the system. It is a dynamic link library management command.This isldconfigRole of orders

Protoc usage

We follow the usual practice.protoc --help(check the help document), we took out a few commonly used commands to explain

1、-IPATH, --proto_path=PATH: specifyimportYou can specify more than one directory for searching. If you do not specify, the current working directory will be the default.

2、--go_out: generategolangSource file

Parameter

To pass additional parameters to the plug-in, use a comma-separated list of parameters separated from the output directory:

protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
  • import_prefix=xxx: Adds the specified prefix to allimportThe beginning of the path
  • import_path=foo/bar: if the file is not declaredgo_packageIs used as a package. If it contains a slash, the rightmost slash will be ignored.
  • plugins=plugin1+plugin2: Specify the list of sub-plug-ins to load (the only plug-in in the repo we downloaded is grpc)
  • Mfoo/bar.proto=quux/shmeMParameters, specifying.protoThe compiled package name of the file (foo/bar.protoAfter compilation, the package name isquux/shme)

Grpc support

IfprotoThe file specifiesRPCServices,protoc-gen-goCan be generated withgrpcCompatible code, we just need toplugins=grpcParameter passed to--go_out, can achieve this goal

protoc --go_out=plugins=grpc:. *.proto

Grpc-gateway

What is it?

grpc-gateway is a plugin of protoc. It reads gRPC service definition, and generates a reverse-proxy server which translates a RESTful JSON API into gRPC. This server is generated according to custom options in your gRPC definition.

grpc-gatewayIt is a plug-in of protoc. It reads the gRPC service definition and generates a reverse proxy server that converts the RESTful JSON API to gRPC. This server is generated according to the custom options in the gRPC definition.

Installation

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

If the following error is reported, we can know by analyzing the error prompt that the connection timed out (probably by a wall)

package google.golang.org/genproto/googleapis/api/annotations: unrecognized import path "google.golang.org/genproto/googleapis/api/annotations" (https fetch: Get https://google.golang.org/genproto/googleapis/api/annotations?go-get=1: dial tcp 216.239.37.1:443: getsockopt: connection timed out)

There are two solutions,

1. Scientific Internet Access

2. Adoptiongithub.com

The that entered the first $GOPATH directorygoogle.golang.orgDirectory, pullgenprotoIngithubsuperiorgo-genprotoMirror library:

cd /usr/local/go/path/src/google.golang.org

git clone https://github.com/google/go-genproto.git

mv go-genproto/ genproto/

After the installation, we willgrpc-gatewayThe executable of moved from $GOPATH to $GOBIN

mv /usr/local/go/path/bin/protoc-gen-grpc-gateway /usr/local/go/bin/

Here we have basically finished this section. It is suggested to read it again and again to deepen our understanding of each component.

References

Sample code