简体   繁体   English

前端还是后端应该调用微服务?

[英]Should frontend or backend call microservices?

I am confused as to whether my frontend application should be the one calling microservices or the backend? 对于我的前端应用程序应该是调用微服务还是后端的应用程序,我感到困惑。

For example: let say I have an "App A" which uses "Auth" and "Products" services. 例如:假设我有一个使用“ Auth”和“ Products”服务的“ App A”。 Should my "App A" server call auth and products services? 我的“ App A”服务器是否应该调用身份验证和产品服务? Or should my frontend call these services directly? 还是我的前端应该直接致电这些服务?

I think in many cases, its more correct to call my services directly since I notice otherwise, my app server endpoints is purely a proxy to these services, which is quite useless. 我认为在许多情况下,直接调用我的服务更为正确,因为我注意到否则,我的应用程序服务器端点纯粹是这些服务的代理,这是毫无用处的。

As always... it depends. 一如既往...这取决于。 If your backend is purely proxying the calls, it is kind of useless. 如果您的后端纯粹是代理呼叫,那是没有用的。 But you could also utilize it for logging, load balancing etc. Javascript apps for example cannot run in parallel and utilize all your cores. 但是您也可以将其用于日志记录,负载平衡等。例如,Javascript应用程序无法并行运行并利用所有内核。 So this could be a reason to process requests in the backend as well. 因此,这也可能是在后端处理请求的原因。 There is really no one-fits-all solution. 确实没有万能的解决方案。

The approach you mentioned aligns well with the use case of API Gateway. 您提到的方法与API Gateway的用例非常吻合。 And it is not really just a proxy. 它实际上不仅仅是代理。 There are way too many things you can do around that, but perhaps the most important thing is security and abstraction. 您可以采取多种方式来解决此问题,但也许最重要的是安全性和抽象性。 By allowing frontend to access the services directly you are exposing all your services to the internet and you have to put in mechanism to tackle vulnerabilities in each one of them. 通过允许前端直接访问服务,您会将所有服务公开到Internet,并且您必须建立机制来解决其中每个漏洞。 It make sense to do it in one place. 在一处进行是有意义的。

Second is discovery and maintainability of endpoints which is a better with such a solution 其次是端点的发现和可维护性,这种解决方案更好

This may add one more hop but what you could do with this in place make it worth. 这可能会增加一跳,但是您可以适当执行此操作使其值得。

Logging, routing, downtime/error handling all can be managed well if you are not exposing services directly. 如果您不直接公开服务,则可以很好地管理日志,路由,停机/错误处理。

Another point with respect to Auth service, it may lie behind your proxy or could be the only other service exposed. 关于身份验证服务的另一点,它可能位于您的代理之后,或者可能是唯一公开的其他服务。 There are some benefits in either of those approaches but since its not what you asked I will trim it to this. 这两种方法中的任何一种都有一些好处,但是由于它不是您所要求的,因此我将对此进行了调整。

Having said that, in the end it all really depends on your needs and your use cases. 话虽如此,最终,这实际上取决于您的需求和用例。 If security is not a prime concern or if you are working more around POC. 如果安全不是主要问题,或者您正在围绕POC进行更多工作。 But just consider the pros and cons of having one 但只要考虑一下拥有一个的利弊

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

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