gRPC Golang / Python use

gRPC Golang / Python use

Previously developed sites are using the http protocol, learned the TCP / IP protocol knows, based on the transport layer TCP, HTTP application layer is filled with a certain rule text.

1.gRPC use and presentation

Work used to gRPC, in fact, is also a http request rpc variant, remote procedure calls .gRPC underlying protocol is HTTP2

gRPC beginning, developed by google, is a language-neutral, platform-neutral, open-source remote procedure call (RPC) systems for mobile and HTTP / 2 design. Currently provides C, Java, and Go language versions, namely:. Grpc, grpc-java, grpc-go where C version supports C, C ++, Node.js, Python, Ruby, Objective-C, PHP and C # support.

gRPC HTTP / 2 based on standard design, such as a bi-directional flow brings, flow control, header compression, multiplexing multiple single TCP connection requests on other bits. These characteristics make it a better performance on a mobile device, more power and space occupancy.

gRPC based on the following concepts: the definition of a service, specify its methods (including parameters and return types) can be invoked remotely. In the service side to implement this interface, and run a gRPC server to handle client calls. The client can have the server the same way as a stub that calls as if in the same machine.

You can use different language platform for development

grpc.png

gRPC default protocol buffers for message communications, in which Google is a mature open source data structure serialization mechanism

Reference: GRPC official document Chinese version | concept

Protocol

Protocol buffers are Google's language-neutral, platform-neutral, extensible 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.

Project Address: https://github.com/google/protobuf

Installation Reference: https://github.com/google/protobuf/blob/master/src/README.md

installationThe protocol compiler

sudo apt-get install autoconf automake libtool curl make g++ unzip
./autogen.sh
####
#执行./autogen.sh时,报error: configure.ac:1: file'gtest/m4/acx_pthread.m4' does not exist的错误。
#在gmock目录新建gtest文件夹,拷贝项目根目录下m4文件夹至gtest文件夹
####
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.

Use (go the following languages ​​need to install plug-ins)

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

Document Reference: Protocol-buffers documentation

Go gRPC

Go version of the library: https://github.com/grpc/grpc-go

Obtain:

go get google.golang.org/grpc

use

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

If you want to turn the gateway http, please

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

then

#!/usr/bin/env bash
protoc -I/usr/local/include -I. \
  -I$GOPATH/src \
  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  --go_out=plugins=grpc:. \
  msg_newest.proto

protoc -I/usr/local/include -I. \
  -I$GOPATH/src \
  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  --grpc-gateway_out=logtostderr=true:. \
  msg_newest.proto

Python gRPC

pip install grpcio==1.4.0
pip install grpcio-tools==1.4.0
pip install protobuf

python -m grpc.tools.protoc --python_out=pbdata --grpc_python_out=pbdata -I . msg.proto

Reproduced please specify: http://www.lenggirl.com/cap/grpc.html

Guess you like

Origin www.cnblogs.com/nima/p/11750938.html