简体   繁体   English

使用 Spring Boot 的具有 gRPC 和 REST 的微服务

[英]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.对于一个项目,我想使用 Spring Boot 设置一个小型微服务场景,其中有一个 API 网关,将 REST 和 GraphQL 暴露给客户端、一个 Eureka 服务注册表和三个服务。 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.由于性能原因,我希望 API 网关背后的所有服务都可以使用 gRPC,但同时仍然公开一个额外的 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?网关如何将客户端的 HTTP 请求代理到 gRPC 请求?

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.您可以查看LogNet grpc-spring-boot-starter以了解如何将 gRPC 集成到 Spring Boot 中,它还有一个关于 Eureka 的部分。

As per Eureka example, make sure you don't create new connection over gRPC for each call.根据 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.根据 API 网关的实现,它还应该与 Eureka 对话,并通过 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).作为第二部分,只需在 Spring 服务中实现您的业务逻辑,并从与传输相关的抽象(控制器和 gRPC 服务)转发对它们的调用。

Optionally, you can go even further, and define all messages in Protobuf only.或者,您可以更进一步,仅在 Protobuf 中定义所有消息。 And then register Spring's Protobuf Converter for HTTP.然后注册 Spring 的 Protobuf Converter for HTTP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM