简体   繁体   English

Spring Boot - Zuul - 微服务 - 优化对网关的 API 调用以获取用户详细信息

[英]Spring Boot - Zuul - Microservice - optimise API calls made to gateway to fetch user details

I am implementing a micro-service architecture in Spring-Boot with Zuul as gateway.我正在以 Zuul 作为网关在 Spring-Boot 中实现微服务架构。 I have a zuul gateway service that has authentication and authorisation logic.我有一个具有身份验证和授权逻辑的 zuul 网关服务。 Also, it has API exposed that responds with user details like userid-username mapping, current loggedin user etc. So, my other services make API call to gateway for any user information.此外,它还公开了 API,可响应用户详细信息,如用户 ID 用户名映射、当前登录用户等。因此,我的其他服务通过 API 调用网关获取任何用户信息。 I have marked zuul.sensitiveHeaders=Cookie,Set-Cookie to allow bearer token to be passed to non-gateway microservices.我已标记 zuul.sensitiveHeaders=Cookie,Set-Cookie 以允许将承载令牌传递给非网关微服务。

Problem Statement - In my other service, I am just storing userid where ever required.问题陈述 - 在我的其他服务中,我只是在需要的地方存储用户 ID。 Then, while returning data to front-end, I make API call to gateway service to fetch username for the userid.然后,在向前端返回数据时,我对网关服务进行 API 调用以获取用户 ID 的用户名。 This works fine when I am modifying single data.当我修改单个数据时,这很好用。

However, when I have to return bulk data (say 1000 records), I am making 1000 API calls to fetch username based on userid of each record.但是,当我必须返回批量数据(比如 1000 条记录)时,我将进行 1000 次 API 调用以根据每条记录的用户 ID 获取用户名。 This is compromising the speed.这会影响速度。

Example -例子 - 在此处输入图片说明

To display above data on UI, I am making 8 API calls to gateway to fetch username for each userid (created_by).为了在 UI 上显示以上数据,我对网关进行了 8 次 API 调用,以获取每个用户 ID(created_by)的用户名。

Can someone please help me with architecture I should be using in such case.有人可以帮我解决在这种情况下应该使用的架构吗? Some of the solutions I thought of are below.我想到的一些解决方案如下。 However I am not very sure if any of these is best possible one但是我不太确定这些是否是最好的

  1. Fetch hashmap of userid-username mapping after login and store in session cache.登录后获取 userid-username 映射的 hashmap 并存储在会话缓存中。 but this will slowdown login call但这会减慢登录调用
  2. Add OncePerPrequestFilter such that on every API call, hashmap of userid-username mapping will be fetched and passed to controller.添加OncePerPrequestFilter 以便在每次API 调用时,将获取userid-username 映射的hashmap 并将其传递给控制器​​。 This is feasible but in APIs where this mapping is not required, still a API call will be made which will be over load.这是可行的,但在不需要此映射的 API 中,仍然会进行 API 调用,这将导致过载。

any other suggestions are welcome.欢迎任何其他建议。

It's not quite clear what you're trying to do - the question title does not seem to have to do much with the description.不太清楚您要做什么 - 问题标题似乎与描述没有太大关系。 Nonetheless, it seems you have two separate facets to this issue:尽管如此,您似乎对这个问题有两个不同的方面:

  1. In your backend, you require user information passed via the token (through your Gateway) that is used in your services.在您的后端,您需要通过服务中使用的令牌(通过您的网关)传递用户信息。
  2. In you frontend, you need some (minimal?) subset of that information for display purposes (I say display, but usually this information would be used for some other process - eg "send this user an E-Mail").在您的前端,您需要该信息的一些(最小?)子集用于显示目的(我说的是显示,但通常此信息将用于其他一些过程 - 例如“向该用户发送电子邮件”)。

Moreover, you mention that:此外,您提到:

However, when I have to return bulk data (say 1000 records), I am making 1000 API calls to fetch username based on userid of each record.但是,当我必须返回批量数据(比如 1000 条记录)时,我将进行 1000 次 API 调用以根据每条记录的用户 ID 获取用户名。 This is compromising the speed.这会影响速度。

How you wish to handle this situation depends what you want to do and how you wish to represent your requests and results.您希望如何处理这种情况取决于您想要做什么以及您希望如何表示您的请求和结果。

In short, you need to give us more details behind the actual use case behind this specific request and the nature of requests that will be happening in the system.简而言之,您需要在此特定请求背后的实际用例以及系统中将发生的请求的性质背后向我们提供更多详细信息。 For instance, do you really need each individual user?例如,您真的需要每个单独的用户吗? Do you instead perhaps need a list of all active/inactive/ users in the system?您是否可能需要系统中所有活动/非活动/用户的列表? In the latter case, it would suffice to query a read-optimized model which is updated whenever a new user is created.在后一种情况下,查询读取优化模型就足够了,该模型在创建新用户时更新。 In this case, the response would not only be fast, but you could easily add nice features like paging and sorting since chances are you are already providing such functionality for collection-based responses.在这种情况下,响应不仅会很快,而且您可以轻松添加分页和排序等不错的功能,因为您可能已经为基于集合的响应提供了此类功能。

Your backend services can still populate a security context based on the token information alone: what the user's role in the system is, the provided scopes, etc. For changing the state of the system, you would still (at some point in the process) have a check that checks the current system state, via eg a database call.您的后端服务仍然可以仅根据令牌信息填充安全上下文:用户在系统中的角色是什么,提供的范围等。对于更改系统状态,您仍然(在过程中的某个时刻)检查当前系统状态,例如通过数据库调用。

If you only need some small subset of details for the currently logged in user to be displayed (which rarely changes), and this data is not highly confidential, you may consider adding it as a custom Claim inside your bearer token.如果您只需要显示当前登录用户的一小部分详细信息(很少更改),并且此数据不是高度机密的,您可以考虑将其添加为不记名令牌中的自定义Claim For example, if you use JWTs as your bearer tokens, you can easily decode the token and read the information.例如,如果您使用 JWT 作为不记名令牌,则可以轻松解码令牌并读取信息。

So, it depends on what you exactly want to do.所以,这取决于你到底想做什么。 Do you really need to query >1,000 individual users at once for 1 field?您是否真的需要为 1 个字段同时查询 >1,000 个个人用户? Perhaps I've misunderstood the question, but it sounds to me like you want a very specific subset of information on a large number of entities (in this case - users) in your system, and a read-optimized model (or even a database view which is directly queried by a user with sufficient privileges) would do the job.也许我误解了这个问题,但在我看来,您想要关于系统中大量实体(在这种情况下 - 用户)的非常具体的信息子集,以及读取优化模型(甚至是数据库)具有足够权限的用户直接查询的视图)将完成这项工作。

As a side remark, Spring Cloud Zuul has been in maintenance mode since the Greenwich release train, as of 22 Jan. 2019. You should consider upgrading to the Spring Cloud Gateway , which supersedes it and offers (among many other improvements) a non-blocking request model (as opposed to the Zuul blocking model). Zuul说一句,自Greenwich发布系列以来,Spring Cloud Zuul一直处于维护模式,截至 2019 年 1 月 22 日。您应该考虑升级到Spring Cloud Gateway ,它取代了它并提供(在许多其他改进中)一个非阻塞请求模型(与Zuul阻塞模型相反)。

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

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