简体   繁体   English

微服务架构中的API调用

[英]API calls in Microservices Architecture

not sure if my question is explainable enough but I will try to explain it here as much as I can. 我不确定我的问题是否可以解释,但我会尽可能地在这里解释一下。 I am currently exploring and playing with a microservices architecture, to see how it works and learn more. 我目前正在探索和使用微服务架构,以了解它是如何工作和了解更多。 Mostly I understand how things work, what is the role of API Gateway in this architecture, etc... 大多数情况下,我了解事情的运作方式,API Gateway在此架构中的作用等等......

So I have one more theoretical question. 所以我还有一个理论问题。 For example, imagine there are 2 services, ie. 例如,假设有2个服务,即。 event (which manage possible events) and service ticket which manages tickets related to a specific event (there could be many tickets). 事件(管理可能的事件)和管理与特定事件相关的票证的服务票证(可能有许多票证)。 So these 2 services really depend on each other but they have a separated database, are completely isolated and loosely coupled, just as it should be in "ideal" microservices environment. 所以这两个服务实际上是相互依赖的,但它们有一个独立的数据库,完全是隔离的,松散耦合的,就像应该在“理想的”微服务环境中一样。

Now imagine I want to fetch event and all tickets related to that event and display it in a mobile application or web spa application or whatever. 现在想象一下,我想获取与该事件相关的事件和所有票证,并将其显示在移动应用程序或Web应用程序或其他任何内容中。 Is calling multiple services / URLs to fetch data and output to UI completely okay in this scenario? 在这种情况下,调用多个服务/ URL来获取数据并输出到UI完全没问题吗? Or is there a better way to fetch and aggregate this data. 或者有更好的方法来获取和聚合这些数据。

From my reading different sources calling one service from another service is adding latency, making services depend on each other, future changes in one service will break another one, etc so it's not a great idea at all. 从我的阅读中,从另一个服务调用一个服务的不同来源是增加延迟,使服务相互依赖,一个服务中的未来变化将破坏另一个服务等,所以这根本不是一个好主意。

I'm sorry if I am repeating a question and it was already asked somewhere (althought I could not find it), but I need an opinion from someone that met this question earlier and can explain the flow here in a better way. 我很抱歉,如果我重复一个问题并且已经在某个地方被问过(虽然我找不到它),但是我需要早些时候遇到这个问题的人的意见,并且可以更好地解释这里的流程。

Is calling multiple services / URLs to fetch data and output to UI completely okay in this scenario? 在这种情况下,调用多个服务/ URL来获取数据并输出到UI完全没问题吗? Or is there a better way to fetch and aggregate this data. 或者有更好的方法来获取和聚合这些数据。

  1. Yes it is ok to call multiple services from your UI and aggregate the data in your Fronted code for your needs. 是的,可以从您的UI调用多个服务,并根据您的需要聚合您的前端代码中的数据。 Actually in this case you would call 2 Rest API's to get the data from ticket micro-service and event micro-service. 实际上,在这种情况下,您可以调用2 Rest API来从票务微服务和事件微服务中获取数据。

  2. Another option is that you have some Views/Read optimized micro-service which would aggregate data from both micro-services and serve as a Read-only micro-service. 另一个选择是你有一些Views / Read优化的微服务,它将聚合来自两个微服务的数据,并作为只读微服务。 This of course involves some latency considerations and other things. 这当然涉及一些延迟考虑和其他事情。 For example this approach can be used if you have requirement like a View which consists of multiple of models(something like a Denormalized view) which will be accessed a lot and/or have some complex filter options as well. 例如,如果您有像View这样的需求,可以使用这种方法,该View包含多个模型(类似于非规范化视图),这些模型将被大量访问和/或具有一些复杂的过滤器选项。 In this approach you would have a Third micro-service which would be aggregated from the data of your 2 micro-services(tickets and events). 在这种方法中,您将拥有第三个微服务,该服务将根据您的2个微服务(票证和事件)的数据进行汇总。 This micro-services would be optimized for reading and could also if needed use a different storage type(Document db or similar). 这种微服务将针对阅读进行优化,并且如果需要还可以使用不同的存储类型(文档db或类似)。 For your case if you would decide to do this you could have only one API call to this micro-service which will provide you all your data. 对于您的情况,如果您决定这样做,您只能对此微服务进行一次API调用,它将为您提供所有数据。

  3. Calling One micro-service from another. 从另一个调用一个微服务。 In some cases you can not really avoid this. 在某些情况下,你无法避免这种情况。 Even though there are some sources online which would tell you not to do it sometimes it is inevitable. 尽管网上有一些消息来告诉你不要这样做但有时这是不可避免的。 For your example I would not recommend this approach as it would produce coupling and unnecessary latency which can be avoided with other approaches. 对于您的示例,我不建议使用此方法,因为它会产生耦合和不必要的延迟,这可以通过其他方法避免。

Background info: 背景资料:

You can read this answer where the topic is about if it is ok to call one micro-service from another micro-service. 您可以阅读答案,其中主题是关于是否可以从另一个微服务调用一个微服务。 For your particular case it is not a good option but for some cases it might be. 对于您的特定情况,它不是一个好的选择,但在某些情况下可能是。 So read it for some clarification. 所以请阅读它以获得一些澄清。

Summary: 摘要:

I have worked with system where we where doing all those 3 things. 我曾经在系统中工作,我们在那里做所有这三件事。 It really depends on your business scenario and needs of your application. 这实际上取决于您的业务场景和应用程序的需求。 Which approach to pick will depend on a couple of criteria like: usability from UI, scaling(if you have high demand on the micro-services you could consider adding a Third micro-service which could aggregate data from tickets and events micro-service), domain coupling. 选择哪种方法取决于以下几个标准:来自UI的可用性,扩展(如果您对微服务有很高的需求,您可以考虑添加第三个微服务,它可以汇集门票和事件微服务的数据) ,域耦合。 For your case I would suggest option 1 or option 2 (if you have a high demanding UI) from user prospective. 对于您的情况,我建议用户预期选项1或选项2(如果您有高要求的UI)。 For some cases option 1 is enough and having a third micro-service would be an overkill, but sometimes this is an option as well. 对于某些情况,选项1就足够了,并且拥有第三个微服务将是一种过度杀伤,但有时这也是一个选项。

In my experience with cloud based services, primarily Microsoft Azure, the latency of one service calling another does indeed exist, but can be relied upon to be minimal. 根据我对基于云的服务(主要是Microsoft Azure)的体验,一个服务调用另一个服务的延迟确实存在,但可以依赖于最小化。 This is especially true when compared to the unknown latency involved with the users device making the call over whichever internet plan they happen to have. 与用户设备通过他们碰巧具有的任何互联网计划进行呼叫所涉及的未知延迟相比,尤其如此。

There will always be a consuming client that is dependent on a service and its defined interface, whether it is the SPA app or a another service. 总是有一个消费客户端依赖于服务及其定义的接口,无论是SPA应用程序还是其他服务。 So in the scenario which you described, something has to aggregate the payloads from both services. 因此,在您描述的场景中,必须聚合两个服务的有效负载。

Based on this I have seen improved performance by using a service which handles client requests, aggregates results from n services and responds accordingly. 基于此,我已经看到通过使用处理客户端请求的服务,聚合来自n个服务的结果并相应地响应来改进性能。 This does indeed result in dependencies, but as your services evolve, it is possible to have multiple versions of your services active simultaneously allowing you to depreciate older versions at a time that is appropriate. 这确实会导致依赖性,但随着服务的发展,可以同时激活多个版本的服务,从而允许您在适当的时候折旧旧版本。

I hope that helps. 我希望有所帮助。

Optional Advice 可选建议

You can maintain the read table (denormalize ) inside any of the services , which suited best. 您可以在任何最适合的服务中维护读取表(非规范化)。 Why? 为什么? - Because the CQRS apply where needed , CQRS best suited for the big and complex application. - 因为CQRS适用于需要的地方,CQRS最适合大型和复杂的应用。 It introduce the complexity in your system , and you gain less profit and more headache. 它介绍了系统的复杂性,并且您获得的利润更少,更令人头痛。

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

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