Introduction and Environmental Installation
Suppose we have a project requirement and hope to use it
APICommunication, but also want to provide external
Restful Api, write two sets of too cumbersome does not conform to
So we came up with
Grpcas well as
Grpc GatewayThis is what we need
At the official beginning of our
Grpc GatewayBefore practice, we need to configure our development environment
- Protoc Plugin
- Protocol Buffers
What is it?
Google is right
A high performance, open-source universal RPC framework
GrpcIs a high-performance, open source, general RPC framework with the following features:
Protocol BuffersAs a format for data exchange, supports
- Cross-language, cross-platform, that is
GrpcSupport for multiple platforms and languages
- HTTP2 support, two-way transmission, multiplexing, authentication, etc.
1. Official Recommendation (Scientific Internet Access Required)
go get -u google.golang.org/grpc
Enter the first $GOTPATH directory (because
go getWill be installed by default under the first), new
githubMirror 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/
google.golang.org/ └── grpc ...
grpcThere are many commonly used packages, such as:
grpcSupported metadata structures, methods in packages can
MDAcquiring and processing
grpcThe various authentication credentials supported encapsulate all the states that the client needs to authenticate the server and make various assertions.
grpcThe standard error code used is generic
What is it?
go get -u github.com/golang/protobuf/protoc-gen-go
Protoc 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.
v3We recommend using the version of
It is suggested that you can read the introduction of official documents. This series will briefly introduce the contents involved when using it.
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
If an error is reported
protoc: error while loading shared libraries: libprotobuf.so.15: cannot open shared object file: No such file or directory
ldconfigAfter that, it can be successfully run again.
Why do you want to implement
We can know from the information output from the console,
Protocol Buffers LibrariesThe default installation path for is at
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 is
ldconfigRole of orders
We follow the usual practice.
protoc --help(check the help document), we took out a few commonly used commands to explain
-IPATH, --proto_path=PATH: specify
importYou can specify more than one directory for searching. If you do not specify, the current working directory will be the default.
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 all
importThe beginning of the path
import_path=foo/bar: if the file is not declared
go_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)
.protoThe compiled package name of the file (
foo/bar.protoAfter compilation, the package name is
protoThe file specifies
protoc-gen-goCan be generated with
grpcCompatible code, we just need to
plugins=grpcParameter passed to
--go_out, can achieve this goal
protoc --go_out=plugins=grpc:. *.proto
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.
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 18.104.22.168:443: getsockopt: connection timed out)
There are two solutions,
1. Scientific Internet Access
The that entered the first $GOPATH directory
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 will
grpc-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.