Nez
Nez

Reputation: 141

Is it possible to have an API working with both REST and gRPC?

My goal is to try to put both to work as part of the incremental migration from a REST API to gRPC. We are going to start using gRPC as the way of communication between our services in our microservice architecture.

The problem is that whenever I put the gRPC service in my middleware and/or I try to force http2 protocol for the gRPC to work my REST API stops to work. Even though my Swagger documentation stops to work with both implementations the endpoints via POSTMAN are still reachable when I add the middleware, but not when I add the http2 protocol. For reference we are already using .NET Core 3.

So my question is, is it possible to have both REST and gRPC working at the same time on the same application? If so, how?

Upvotes: 14

Views: 5720

Answers (3)

GiveMeSomething
GiveMeSomething

Reputation: 1

I think you can enable support for both HTTP1.1/2.0 for a single endpoint with Http1AndHttp2 option

My config in appsettings.json

{
"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http1AndHttp2"
    }
  }
}

Then your applicationUrl inside launchSettings.json will now support both HTTP 1.1 and HTTP 2.0

P/s: I'm using .NET Core 6.0

Upvotes: 0

Arnon Rotem-Gal-Oz
Arnon Rotem-Gal-Oz

Reputation: 25919

An alternative (esp. if is a transition period) is to expose just gRPC endpoints and use transcode from HTTP to gRPC in the gateway e.g. https://apisix.apache.org/docs/apisix/plugins/grpc-transcode/

Upvotes: 0

Majid Shahabfar
Majid Shahabfar

Reputation: 4829

Yes, it is. All you need is just to configure the Kestrel endpoints parameters in your appsettings.json. Set Endpoints with WebApi and gRPC with your custom name as follow:

  "Kestrel": {
    "Endpoints": {
      "Grpc": {
        "Protocols": "Http2",
        "Url": "https://localhost:5104"
      },
      "webApi": {
        "Protocols": "Http1",
        "Url": "https://localhost:5105"
      }
    }
  }

Now you can do both access to WebApi endpoints and call the gRPC service from a client.

Upvotes: 2

Related Questions