使用Spring Boot的带有gRPC和REST的微服务 [英] Microservices with gRPC and REST using Spring Boot

查看:112
本文介绍了使用Spring Boot的带有gRPC和REST的微服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我想使用Spring Boot设置一个小型微服务场景,该API网关向客户端公开REST和GraphQL,并提供Eureka服务注册表和三个服务.由于性能原因,我希望API网关后面的所有服务都使用gRPC,但同时仍要公开其他REST API.有没有一种干净的方法可以在相同的业务逻辑上实现这两种类型的接口?网关如何将客户端的HTTP请求代理到gRPC的请求?

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?

推荐答案

您可以查看 LogNet grpc-spring-boot-starter ,了解如何将gRPC集成到Spring Boot中,其中还有关于Eureka的部分.

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.

按照Eureka的示例,请确保您不要为每个调用通过gRPC创建新连接.

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

根据API网关的实现,它还应该与Eureka进行通信,并通过gRPC通过逻辑名称访问下游服务.

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

在第二部分中,只需在Spring Services中实现您的业务逻辑,然后从与传输相关的抽象(控制器和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).

(可选)您可以走得更远,并且仅在Protobuf中定义所有消息.然后注册Spring的Protobuf Converter for HTTP.

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

这篇关于使用Spring Boot的带有gRPC和REST的微服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆