Grpc and API gateways - grpc

I was considering shifting my back
end rest api microservices to grpc servers. I use tyk as the api gateway to route http requests. How does an api gateway handle grpc requests?

With gRPC-Gateway, you can generate a reverse proxy that would translate REST into gRPC call via marshaling of the JSON request body into respective Go structures followed by the RPC endpoint call.
The gRPC-Gateway is a plugin of the Google protocol buffers compiler protoc. It reads protobuf service definitions and generates a reverse-proxy server which translates a RESTful HTTP API into gRPC. This server is generated according to the google.api.http annotations in your service definitions.

Related

Proxying gRPC calls to REST calls

I have a usecase in which i need to call an HTTP server, the client only supports gRPC traffic. So i need a proxy in between the server and client which can convert this gRPC traffic to HTTP.

How to collect and pass b3 propagation headers in grpc client request using opentracing api?

I was using https://github.com/opentracing-contrib/java-grpc with jaegar tracer for enabling tracing in my grpc client program. Now I would like to use istio service mesh to handle tracing in server side. https://istio.io/latest/docs/tasks/observability/distributed-tracing/overview/ .
So the grpc client now needs to send the appropriate tracing HTTP headers along with each grpc client request so that istio can send those metrics to Jaegar. Does anyone have a working example of fetching the trace span information in grpc client and include the corresponding b3 propagation headers in a grpc client request?
Following http headers need to be passed in a java/C# grpc client request :
x-request-id
x-b3-traceid
x-b3-spanid
x-b3-parentspanid
x-b3-sampled
x-b3-flags
x-ot-span-context
Thanks.
Have a look at the OpenTelemetry-Java-Instrumentation project. It can provide automated tracing of your application without needing to write custom code. It uses an instrumentation agent that runs in the JVM beside your application.
java -javaagent:path/to/opentelemetry-javaagent-all.jar \
-jar myapp.jar
It supports exporting metrics to Jaeger
It also supports propagating headers to downstream requests.
They have just added support for b3-multi headers so a new release should be available in the coming days

GRPC REST channel implementation

You can add REST "annotations" to a GRPC service: https://cloud.google.com/endpoints/docs/grpc/transcoding
I know there is a GRPC proxy service that you can run to proxy between GRPC <-> REST.
My question is, is there a GRPC channel implementation in Java that will allow me to achieve this without running a proxy service?
gRPC Transcoding is supported by Google APIs, Cloud Endpoint, gRPC Gateway and Envoy, but these annotations are not used by protoc-gen-grpc-java, so you have to either use a proxy service or implement support for these annotations yourself.

grpc - is TLS necessary if https enabled?

I'm newbie of grpc and have played with simple grpc clients of java, go, and python. I know basic http and https but not familiar with protocal details. So this question may be rediculous to you but I didn't find any explaination online.
I know grpc has insecure(go: grpc.WithInsecure(), python: grpc.insecure_channel, java: usePlaintext()) and secure mode(TLS). and grpc is based on httpv2, and http has security mode(https).
So what if use insecure grpc with https? Is the overall data transfer safe?
And what if use TLS grpc with https? Is there performance overhead(becuase I think the messages are encrypted twice)?
Thank you for any answer, any exsiting webpages explaining such topic that will be best!
Insecure implies http. And TLS implies https. So there's no way "to use insecure grpc with https", as at that point it is then http.
There is no double-encryption. The gRPC security mode is the same as the HTTP security mode.
Using gRPC over TLS is highly recommended if you gRPC server is serving requests coming from outside(external network). For example you're creating front end app in javascript serving user requests. Your javascript app make call to your gRPC server for APIs your server provide. Your javascript communicate to your gRPC server through stub created in javascript end. At the end of your gRPC server, you need to set tls mechanism to secure communication between your javascript app and your gRPC server(because requests coming from outside).
gRPC somehow mostly used for internal services communication inside internal network in microservice architecture. You don't need to set tls for internal network usage since requests coming from your own environment from within your watch.
If you want to apply something like "gRPC over HTTPS", then you need something like gateway to map your http call to your gRPC server. Check this out.
You need to compile your proto file as gateway service definitions as well using provided tools. Now you can create your normal http server with tls enabled through something like http.ListenAndServeTLS(...). Dont forget to register your grpc server to the http server using the service definitions compiled from the proto file. With this all your requests to are encrypted with tls to your http server like normal rest apis do, but get proxied to gRPC server you defined. There's no need to enable tls at your gRPC server since it has been enabled in your http server.

What is the difference between a JSON/XML RPC and a HTTP api or TCP API

What is special about a remote procedure call?
From what I have seen so far, they seem to have similar functionalities as a HTTP API or TCP API. Why not just use a http/tcp api?
As a developer that wishes to open up an API, why would I choose to use JSON RPC over a HTTP or TCP API (with a JSON encoded payload)
RPC is a different type of architecture that predates REST - it is more focused on procedures while REST focuses on exposing resources with a consistent API.
RPCs are most often found in legacy code and in cases where REST may not be appropriate.

Resources