简体   繁体   English

Spring 启动微服务 - 从数据库中检索数据

[英]Spring boot microservices - retrieving data from DB

I'm new in Microservices architecture and I'm trying to build a simple projects with two spring boot microservices, eureka as service discovery and spring cloud for an API Gateway.我是微服务架构的新手,我正在尝试使用两个 spring 引导微服务构建一个简单的项目,eureka 作为服务发现和 spring 云用于 API 网关。 UserService and OrderService are connected to different MySQL schema and my question is, how to retrieve data from orders_table by calling rest method through the client and passing the user id? UserService 和 OrderService 连接到不同的 MySQL 模式,我的问题是,如何通过客户端调用 rest 方法并传递用户 ID 从 orders_table 检索数据? I have a dashboard where I have to show all the orders made by the logged user.我有一个仪表板,我必须在其中显示登录用户发出的所有订单。

The client project is a Spring Boot app with Web and RestTemplate dependencies.客户端项目是一个 Spring 启动应用程序,具有 Web 和 RestTemplate 依赖项。

Below the table struture:下表结构:

users_table: id, name, lastName users_table: id, name, lastName

orders_table: id, description, user_id orders_table: id, description, user_id

Thank you:)谢谢:)

You can use JPA repository to fetch the required data.您可以使用 JPA 存储库来获取所需的数据。 You can define Entity models for you tables.您可以为表定义实体模型。 JPA repository will give you CRUD operations out of the box which you can utilise to fetch data with your query. JPA 存储库将为您提供开箱即用的 CRUD 操作,您可以利用它通过查询获取数据。

Reference Link: https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa参考链接: https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa

Option 1: Call user service from the dashboard application to retrieve the user_id, then make a second call from the dashboard application to get the orders for the user_id.选项 1:从仪表板应用程序调用用户服务以检索 user_id,然后从仪表板应用程序进行第二次调用以获取 user_id 的订单。

Option 2: Have a third service which will act as an aggregator.选项 2:使用第三个服务作为聚合器。 From the dashboard application call the aggregator, which in turn will first fetch the user_id from the user service and then the associated orders from the orders service.从仪表板应用程序调用聚合器,聚合器将首先从用户服务中获取 user_id,然后从订单服务中获取关联的订单。

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

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