API体系结构中断路器设计模式的优势是什么? [英] What is advantage of circuit breaker design pattern in API architecture?

查看:119
本文介绍了API体系结构中断路器设计模式的优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这个问题不适合.

So sorry if this question is not fit for SO.

但是我试图寻找答案.

我正在研究断路器的设计模式,据我了解它用于使您的API容错.现在我感到困惑的是,

I was studying Circuit Breaker design pattern, As I understand its used for making you API fault tolerance. Now what I am confuse is,

假设我有调用支付api的API,并且可以说我将电路配置为在5次调用连续失败的情况下打开.

Let say I have API which calls payment api, and lets say I configured my circuit to open if 5 calls fail continuously.

现在按照断路器设计,在断开电路后我将路由后续调用以回退方法.让我们说下5个呼叫,如果是api在线,则在第6个呼叫中我将对支付API进行呼叫.

Now as per circuit breaker design, I will route subsequent calls after opening circuit to fall back method. lets say next 5 calls, and on 6th call I will make a call to payment API if api is online I will close circuit.

但是我没有发现这种模式的任何优势,例如捕获块和断路器之间的区别是什么.

But I dont find any advantage of this pattern, Like what is difference between catch block and circuit breaker.

我们可以使用后退方法做什么?这有什么帮助?

And what can we do in fall back method? How can this help?

推荐答案

我们的同事已经展示了断路器的优点,因此我将集中在实际示例上.

Our colleagues have already shown the advantages of a circuit breaker, so I'll concentrate on practical examples.

因此,在您的示例中,您有一个必须调用支付API的流程>让我们假设其为外部"组件.如果没有这个电话,整个流程可能不会被视为已成功完成"(我了解您有一些将付款作为其基本步骤之一的在线流程).

So, looking at your example, you have a flow that has to call a payment API> Let's assume its an "external" component. Without this call, the whole flow probably can't be considered as "successfully completed" (I understand you have some online process that has a payment as one of its essential steps).

在这种情况下,除非作为备用,您将付款命令存储在某种中间存储器中,然后重新应用"付款逻辑,否则断路器实际上可能不会那么有用.

In this case Circuit Breaker indeed probably won't be that useful unless as a fallback you store the payment command in some kind of intermediate storage and then "re-apply" the payment logic.

断路器的全部目的是提供一个合理的后备功能,这样,如果应用后备逻辑,该流程就不会被认为是失败的.

The whole point of a circuit breaker is to provide a reasonable fallback so that the flow won't be considered as failed if the fallback logic gets applied.

这是断路器更具意义的另一个例子.

Here is another example where Circuit breaker has much more sense.

如果您构建类似于netflix的"门户,并且在UI中存在显示推荐"电影的小部件.推荐引擎会考虑您之前看过/喜欢的电影.从技术上讲,这是一个外部系统"/微服务.

If you build a "netflix-like" portal and in UI there is a widget that shows "recommended" movies. The recommendation engine takes into consideration the movies you've seen / liked before. Technically this is an "external system"/microservice.

现在,如果填充UI数据的流程无法获取建议(例如,如果建议服务关闭),您将使整个流程失败"吗? 可能不是,最好显示一些推荐电影的一般列表",并让用户继续使用该应用程序.

Now, what if the flow that populates the data for the UI, is not able to get the recommendations (for example, if the recommendation service is down), will you "fail" the whole flow? Probably not, maybe its better to show some "generic list" of recommended movies and let the user proceed to work with the application.

在这种情况下,断路器是实现对外部推荐服务的呼叫的不错选择.

In this case, the Circuit breaker can be a good choice for implementing the call to external recommendation service.

现在,此流程与异常处理之间有什么区别?

Now, what's the difference between this flow and exception handling?

如果该推荐系统失败的原因是网络临时中断/数据库运行缓慢,可能最好的办法是花一些时间而不是一遍又一遍地调用它,我们应该给它一个机会再度". 当我们使用断路器时,在断路"期间,我们的代码甚至不会尝试调用服务器,而是直接路由到后备方法.

If the reason for the failure of that recommendation system is some temp network outage / Database slowness, probably its the best to give this service some time and not to try to call it over and over again, we should give it a chance to 'recuperate'. When we apply a circuit breaker, during the "open-circuit" period our code won't even try to call the server, directly routing to the fallback method.

另一方面,常规的try/catch块会始终呼叫推荐服务.

A regular try/catch block, on the other hand, will always call the recommendation service.

因此,总结起来,断路器是问题中所述的容错模式.它是一种可以在某些情况下适用,而在其他情况下无关紧要的工具.

So to wrap up, a circuit breaker is a pattern for fault tolerance as was stated in the question; its a tool which can be applicable in some situations, and irrelevant in other cases.

这篇关于API体系结构中断路器设计模式的优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆