quickstarts icon indicating copy to clipboard operation
quickstarts copied to clipboard

Can you show how to invoke gRPC service with http invoke?

Open hspaay opened this issue 2 years ago • 5 comments

Ask your question here

Following the helpful go quickstart service_invocation works like a charm. When I'm converting it to use gRPC with protobuf that works as well. However, invoking the gRPC using the dapr http API doesn't work and results into 503 Service Unavailable. Would it be possible to include this cross-protocol as an example in the quickstart?

Update: link to issue that suggests this should work: https://github.com/dapr/dapr/issues/2342

Although what is not clear if this requires the gRPC service to implement an onInvoke method as per (https://github.com/dapr/java-sdk/issues/535)

hspaay avatar Jun 22 '22 01:06 hspaay

I'm currently stuck on this as well.

safari137 avatar Jun 22 '22 13:06 safari137

@safari137 got it working by implementing the UnimplementedAppCallbackServer which defines an OnInvoke method. This is described in the integrations example here: https://docs.dapr.io/developing-applications/integrations/grpc-integration/. This allows a call into your service via HTTP. The onInvoke method should check the 'in.Method' property and pass the request to your service handler.

My current understanding after this early bit of experimentation with implementing a dapr gRPC service:

  1. The gRPC service can be invoked via dapr's gRPC port as shown in the docs (gRPC->gRPC). There is no dependency on dapr to get this to work.

  2. To invoke the service via dapr's HTTP API, the service must implement dapr's callback API, check the in.Method property to determine which service method to invoke, map the provided parameters to your service method parameters and do the type checking and validation. Now the service has become dependent on dapr. The service gRPC API is not used. You implement support for cross protocol yourself but at least you get the benefit of using dapr's HTTP middleware chain.

  3. When the service wants to use dapr's pub/sub feature it also needs to implement the same callback API, but I haven't tested this yet.

I'm going to work on a more elaborate sample service to test this out including pub/sub and http middleware. It would be nice if the documentation would explain this a bit better or if there is example code that does this. Hope this helps.

hspaay avatar Jun 23 '22 20:06 hspaay

@hspaay thanks for the explanation! This is very helpful.

safari137 avatar Jun 24 '22 02:06 safari137

In case anyone else wonders how to do this. This simple echo service demonstrates grpc->grpc and http->grpc invocation. Repo can be found here: https://github.com/wostzone/echo-service

hspaay avatar Jun 24 '22 18:06 hspaay

Thank you much for the contribution @hspaay.

The recommendation we have for Invoke is to use gPRC proxy, which is essentially setting the dapr-app-id header, and then just making the gRPC call as normal and/or using your proto. We believe this is better than using Dapr SDK's invoke API believe it or not.

I believe you nail this here: https://github.com/wostzone/echo-dapr/blob/main/pkg/plain-grpc/client/main.go#L70 Maybe it's worth just calling this out?

Note we have docs and how-to's for this approach here: https://docs.dapr.io/developing-applications/building-blocks/service-invocation/howto-invoke-services-grpc/#step-2-invoke-the-service

paulyuk avatar Jul 23 '22 16:07 paulyuk

@paulyuk - how would you like to resolve this issue?

msfussell avatar May 20 '23 00:05 msfussell

@yaron2 what do you recommend here? I feel it's cool there is an integration in the doc above. I wonder how much we feel this should be our guidance? For now I recommend we keep pointing to the doc, and not alter quickstarts.

paulyuk avatar Jun 02 '23 08:06 paulyuk

gRPC proxying is for gRPC to gRPC communication. The detailed reply by @hspaay above covers how you would achieve HTTP to gRPC, however since then we have deprecated the OnInvoke method and HTTP to gRPC isn't a recommended scenario, so shouldn't be in either docs nor quickstarts.

yaron2 avatar Jun 02 '23 13:06 yaron2

Ok thank you for clarifying. We will not doc or have samples for things we do not support. Thank you both.

paulyuk avatar Jun 02 '23 15:06 paulyuk