gdong42 / grpc-mate

A dynamic proxy server that translates JSON HTTP requests into gRPC calls.
https://grpcmate.io
Apache License 2.0
75 stars 9 forks source link
grpc grpc-gateway grpc-mate grpc-proxy rest-api

gRPC Mate

Go Report Card Build Status Docker Pulls MicroBadger Size (tag) Docker Image

gRPC Mate is a light weight reverse proxy server that translates JSON HTTP requests into gRPC calls without the need of code generation. It reads protobuf service definitions through accessing reflection API exposed by the gRPC service, and converts HTTP and JSON requests to gRPC invocations dynamically.

The purpose that gRPC Mate is created is to provide a way to serve HTTP and JSON from a gRPC server very easily, without protobuf definition files sharing, without protobuf directives, and without any Service Discovery system integration.

As its name gRPC Mate suggestes, the reverse proxy server is designed to be a sidecar, and is expected to run beside the proxied gRPC server. In a development or testing scenario, it can be dropped with mere configuration in front of the target gRPC service. In a production environment, instead of running a single gRPC Mate server in front all gRPC services, it is better to be deployed alongside each gRPC server instances, for example, as another container in the same pod if running within Kubernetes cluster.

Features

Installation

It's recommended to use pre-built docker image gdong/grpc-mate directly. You can also choose to build from source.

Build from source

  1. Clone this repo
    git clone git@github.com:gdong42/grpc-mate.git
  2. Install Go SDK if it's not installed yet, e.g.
    brew install go
    brew install dep
  3. Setup GOPATH if not yet, go to grpc-mate dir, install dependencies and build binary
    dep ensure
    go build -o grpc-mate

    Now grpc-mate command is built, following sections show how you configure and run it.

Quick Start

Prerequisites

Make sure Server Reflection is enabled on gRPC target server side.

For demonstration, we start the gRPC example server with reflection enabled provided at https://github.com/grpc/grpc-go/tree/master/examples/features/reflection

$ go run server/main.go
server listening at [::]:50051

Run gRPC Mate

It's really simple to run. Let's connect to the gRPC server started above as an example, using docker or command built from source.

Run gRPC Mate via Docker

$ docker run --name grpc-mate -e GRPC_MATE_PROXIED_HOST=<your grpc server local IP> -e GRPC_MATE_PROXIED_PORT=50051 -dp 6600:6600 gdong/grpc-mate

Note above GRPC_MATE_PROXIED_HOST has to be set to your IP address other than localhost, so that grpc-mate running inside docker can access it.

Run gRPC Mate directly

$ GRPC_MATE_PROXIED_PORT=50051 ./grpc-mate

This by default listens on 6600 as HTTP port, and connects to a local gRPC server running at localhost:50051

To connect to other gRPC server host and port, refer to the configuration section.

Introspecting Services

Now try get http://localhost:6600/actuator/services, you will see all services the server exposes, as well as their enclosing methods, input and output types, e.g. one element of services:

      {  
         "name":"helloworld.Greeter",
         "methods":[  
            {  
               "name":"SayHello",
               "input":"helloworld.HelloRequest",
               "output":"helloworld.HelloReply",
               "route":"/helloworld.Greeter/SayHello"
            }
         ]
      }

It also has request/response JSON templates, convenient for construcing HTTP and JSON requests, e.g. one element of types:

      {  
         "name":"helloworld.HelloRequest",
         "template":{  
            "name":""
         }
      }

Making Requests

Now let's try making gRPC requests using above inspected information

$ curl -X POST -d '{"name":"gdong42"}' "http://localhost:6600/v1/helloworld.Greeter/SayHello" 
{"message":"Hello gdong42"}

Above we invoked SayHello method of helloworld.Greeter service, with JSON message of helloworld.HelloRequest type, and got a JSON message of helloworld.HelloReply type.

Note the HTTP method is POST, the body is a JSON string, and the request path is of pattern /v1/{serviceName}/{methodName}.

Configuration

gRPC Mate is configured via a group of GRPC_MATE_ prefixed Environment variables. They are

Limitation

Currently, gRPC Mate works with Unary calls only. We are working on support Streaming as well.

Contributing

All kinds of contribution are welcome!

Credits

License

Apache License 2.0