Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microservices with gRPC and REST using Spring Boot

For a project I'd like to set up a small microservice scenario using Spring Boot with an API gateway exposing REST and GraphQL to the clients, a Eureka service registry and three services. I want all services behind the API gateway to talk gRPC because of performance reasons, but at the same time still expose an additional REST API. Is there a clean way to implement both types of interfaces on top of the same business logic? And how would the gateway proxy the clients' HTTP requests to gRPC ones?

like image 270
n1try Avatar asked Apr 14 '17 11:04

n1try


People also ask

Is gRPC good for microservices?

gRPC allows you to do client streaming, server streaming, and bi-directional streaming that allows you to send multiple requests or receive multiple responses in parallel. Stable Client–Server interaction in gRPC Microservices are very easy to accomplish because of automatic code generation.

How do I use gRPC in microservices?

gRPC is based on the idea of defining a service and specifying methods that can be called remotely with their parameters and return types. On the server-side, the server implements this interface and runs a gRPC server to handle client calls.

Should I replace REST with gRPC?

“gRPC is roughly 7 times faster than REST when receiving data & roughly 10 times faster than REST when sending data for this specific payload. This is mainly due to the tight packing of the Protocol Buffers and the use of HTTP/2 by gRPC.”

Is gRPC Faster Than REST API?

You just have to make calls to the server address, unlike gRPC, which requires the client setup. REST API still holds importance when the payload is small, and several clients simultaneously make a server call. As a result, gRPC is a lot faster than REST API in most cases.


1 Answers

You can look into LogNet grpc-spring-boot-starter to see how to integrate gRPC into Spring Boot, it also has a section about Eureka.

As per Eureka example, make sure you don't create new connection over gRPC for each call.

Depending on the implementation of the API gateway, it should also talk to Eureka, and access downstream services by logical names via gRPC.

As per second part, simply implement your business logic in Spring Services, and forward calls to them from the transport related abstractions (Controllers and gRPC services).

Optionally, you can go even further, and define all messages in Protobuf only. And then register Spring's Protobuf Converter for HTTP.

like image 89
Alex Avatar answered Sep 30 '22 18:09

Alex