Introduction and Environmental Installation
Suppose we have a project requirement and hope to use itRpc
As internalAPI
Communication, but also want to provide externalRestful Api
, write two sets of too cumbersome does not conform to
So we came up withGrpc
as well asGrpc Gateway
This is what we need
Preparation link
At the official beginning of ourGrpc
+Grpc Gateway
Before practice, we need to configure our development environment
- Grpc
- Protoc Plugin
- Protocol Buffers
- Grpc-gateway
Grpc
What is it?
Google is rightGrpc
Definition of:
A high performance, open-source universal RPC framework
That is,Grpc
Is a high-performance, open source, general RPC framework with the following features:
- Powerful
IDL
, useProtocol Buffers
As a format for data exchange, supportsv2
、v3
(recommendedv3
) - Cross-language, cross-platform, that is
Grpc
Support 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 get
Will be installed by default under the first), newgoogle.golang.org
Directory, pullgolang
Ingithub
Mirror 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 ingrpc
There are many commonly used packages, such as:
-
metadata: defined
grpc
Supported metadata structures, methods in packages canMD
Acquiring and processing -
credentials: Implemented
grpc
The various authentication credentials supported encapsulate all the states that the client needs to authenticate the server and make various assertions. -
codes: defined
grpc
The 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 Plugin
The 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 Buffers
YesGoogle
Introduced 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 arev2
、v3
We 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 executeldconfig
After 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 Libraries
The 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.ldconfig
It usually runs at system startup, so this will not be found now.lib
So 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 isldconfig
Role 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
: specifyimport
You can specify more than one directory for searching. If you do not specify, the current working directory will be the default.
2、--go_out
: generategolang
Source 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 allimport
The beginning of the path -
import_path=foo/bar
: if the file is not declaredgo_package
Is 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/shme
:M
Parameters, specifying.proto
The compiled package name of the file (foo/bar.proto
After compilation, the package name isquux/shme
)
Grpc support
Ifproto
The file specifiesRPC
Services,protoc-gen-go
Can be generated withgrpc
Compatible code, we just need toplugins=grpc
Parameter 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.org
Directory, pullgenproto
Ingithub
superiorgo-genproto
Mirror 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-gateway
The 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.