GRPC-GATEWAY:GRPC convert to HTTP protocol for external service

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

My company's projects are based on a RESTful micro-service architecture, with the micro-service communication between more and more frequent, we hope to make use of RPC to do internal communication, still use restful. So I thought about Google's grpc.

The advantages of using GRPC are many, the binary data can speed up the transfer speed, HTTP2-based multiplexing can reduce the number of connections between services, and the function of the same call method is also effective to improve the development efficiency.

However, the use of GRPC will also face a problem, our micro-service must be to provide a restful interface, if the internal call using GRPC, in some cases to provide a function of the two sets of API interface, which not only reduces the development efficiency, but also increases the complexity of debugging. So I think there is a conversion mechanism, so that restful and GPRC can transform each other.

See a solution on the net, Https://github.com/grpc-ecosystem/grpc-gateway, simply say that there is a gateway server responsible for conversion and proxy forwarding.

Such as:

Installation

First, install Protocolbuffers 3.0 and later.

mkdir tmpcd tmpgit clone https://github.com/google/protobufcd protobuf./autogen.sh./configuremakemake checksudo make install

Then use go get to get grpc-gateway.

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gatewaygo get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swaggergo get -u github.com/golang/protobuf/protoc-gen-go

It is best to put the compiled binary files directory in $PATH , you can put $GOPATH/bin into $PATH .

Example

This example is based on an example from my previous blog, Google's use of grpc in Glang, and if necessary, learn about the previous blog.

Sample code get address: Https://github.com/andyidea/go-example.

The code file structure is as follows

└── src    └── grpc-helloworld-gateway        ├── gateway        │   └── main.go        ├── greeter_server        │   └── main.go        └── helloworld            ├── helloworld.pb.go            ├── helloworld.pb.gw.go            └── helloworld.proto

Let's take a look at the protocol file first. Helloworld.proto has some changes, introducing Google's official API-related extensions, which provide support for the GRPC HTTP conversion.

The specific changes are as follows:

syntax = "proto3";option java_multiple_files = true;option java_package = "io.grpc.examples.helloworld";option java_outer_classname = "HelloWorldProto";package helloworld;import "google/api/annotations.proto";// The greeting service definition.service Greeter {  // Sends a greeting  rpc SayHello (HelloRequest) returns (HelloReply) {        option (google.api.http) = {        post: "/v1/example/echo"        body: "*"    };  }}// The request message containing the user's name.message HelloRequest {  string name = 1;}// The response message containing the greetingsmessage HelloReply {  string message = 1;}

Compared to the previous proto file, the new file has been added

import "google/api/annotations.proto";

And

option (google.api.http) = {        post: "/v1/example/echo"        body: "*"

An extended configuration of HTTP is added here.

Then compile the proto file and generate the corresponding go file

The Helloworld/helloworld.pb.go file is generated here.

Helloworld.pb.go is required by the Server service, and next we need to use PROTOC to generate the go file required by the gateway.

cd src/grpc-helloworld-gatewayprotoc -I/usr/local/include -I. \-I$GOPATH/src  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \--swagger_out=logtostderr=true:. \helloworld/helloworld.proto

The Helloworld/helloworld.pb.gw.go file is generated here. This file is the protocol file that gateway uses to make GRPC and HTTP protocol conversions.

Once the protocol file has been processed, the gateway code needs to be written.

The gateway code is as follows:

 package mainimport ("Flag" "Net/http" "Github.com/golang/glog" "Github.com/grpc-ecosyst Em/grpc-gateway/runtime "" Golang.org/x/net/context "" Google.golang.org/grpc "GW" grpc-helloworld-gateway/hellowor LD ") var (echoendpoint = flag. String ("Echo_endpoint", "localhost:50051", "Endpoint of Yourservice")) func run () error {CTX: = context. Background () ctx, Cancel: = context. Withcancel (CTX) defer cancel () Mux: = Runtime. Newservemux () OPTs: = []grpc. Dialoption{grpc. Withinsecure ()} ERR: = GW. Registergreeterhandlerfromendpoint (CTX, Mux, *echoendpoint, opts) if err! = Nil {return ERR} return HTTP . Listenandserve (": 8080", MUX)}func main () {flag. Parse () defer glog. Flush () If Err: = Run (); Err! = Nil {glog. Fatal (Err)}}  

First, Echoendpoint stores the server information that needs to be connected. This information and the newly created server are then registered and bound with the Registergreeterhandlerfromendpoint in Gw.go, and the low-level connects to the remote server address provided by Echoendpoint so that gateway acts as a client and remote The server establishes the connection, and then launches the newly created Server,gateway with HTTP as a service to the server side to provide HTTP.

The code is done here, let's test it.

First start the Greeter_server service, and then start the gateway, then Gatway connected to the Greeter_server, the external establishment of HTTP monitoring.

Then we send the HTTP request with Curl

curl -X POST -k http://localhost:8080/v1/example/echo -d '{"name": " world"}{"message":"Hello  world"}

The process is as follows: Curl uses post to send a request to the gateway, and the gateway acts as a proxy to convert the request to Greeter_server,greeter_server via GRPC to return the results by GRPC, and after the gateway receives the results, Converted to JSON to return to the front end.

In this way, the conversion process from HTTP JSON to internal GRPC is accomplished through Grpc-gateway.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.