Enable GRPC towards your HTTP/1.1 API
Few months back I had a requirement to integrate a modern application with legacy Rest API built in HTTP/1.1. Requests will be mostly PUT and sometimes REST. Due to various constraints the modern application cannot connect to Rest API via HTTP/1.1, it should be or can be via GRPC or Event broker.
However it was determined to be GRPC to benefit both sides, But how? Legacy application cannot expose GRPC server API.
The answer is Envoy Proxy!!
Envoy Proxy is a modern, high performance, small footprint edge and service proxy. Envoy adds resilience and observability to your services, and it does so in a way that’s transparent to your service implementation.
Envoy explicitly supports L7 protocol parsing and routing for HTTP/1, HTTP2, gRPC, Redis, MongoDB, and DynamoDB. Refer documentation for more details.
It also supports Network(L3/L4) filters for Read, Write & Read/Write.
Now the design to integrate the two applications would be to use Envoy GRPC Reverse Bridge proxy.
Below is the sample proto file definition for this example.
<script src=”https://gist.github.com/MeiyappanKannappa/684b8ee2d3197e4c6e7b7eb9b22aa8fc.js"></script>
Steps
- Expose the Rest API as it is, including the authentication passed as bearer token or JWT token validated on the REST API. The API should be POST with path /<PACKAGE_NAME>.<SERVICE_NAME>/<METHOD_NAME> in correspondence to the proto file.
<script src=”https://gist.github.com/MeiyappanKannappa/a829a5319bd27b0402aa68d6a030099b.js"></script>
2. Configure envoy reverse proxy with below yaml. GRPC server will be exposed in port 8050, and will be routed to HTTP node express server running on port 80 as GRPC unary request.
Note: Envoy GRPC reverse bridge will be supported only for unary requests, streaming support is not available in GRPC reverse bridge.
<script src=”https://gist.github.com/MeiyappanKannappa/cce57ad5da74f5eee54a5526406ba958.js"></script>
3. Create a GRPC client to connect to envoy proxy, and pass a custom GRPC header which will be matched by the proxy and routed to upstream cluster. Header used in this example is content-type with value application/grpc+proto.
The HTTP/1.1 server should also provide response with the same content type header. Else GRPC requests would fail.
<script src=”https://gist.github.com/MeiyappanKannappa/47977dd808755e1c6f3d1b0630485eec.js"></script>
Now that you are all set, start the envoy proxy
envoy — config-path proxy.yaml
Start the HTTP/1.1 server
node app.js
Run the sample GRPC client
node greeter_client.js
You are all set with below response
Greeting: { message: ‘Hello Bill’ }
Envoy supports various TCP and UDP proxies as well.
References: