この記事では、Node.js のサーキット ブレーカー メカニズムについて説明します。お役に立てば幸いです。

従来の CS アーキテクチャを使用すると、障害などの理由でサーバーがリクエストをブロックし、お客様がクライアントのリクエストは応答を失い、その結果、ユーザーのグループは一定期間後にサービスを取得できなくなります。この状況によって考えられる影響は限定的であり、推定することができます。
ただし、マイクロサービス システムでは、サーバーが他のいくつかのマイクロサービスに依存している可能性があり、これらのマイクロサービスはさらに他のマイクロサービスに依存している可能性があります。この場合、特定のサービスがダウンストリームの輻輳を引き起こす可能性があり、致命的な結果を引き起こす可能性があります。瞬間的 (数秒以内) のカスケード的なリソース消費により、リンク全体が停止します。これを「サービス崩壊」と呼びます。 [推奨される学習: 「nodejs チュートリアル 」]
サーキット ブレーカーを使用しないリクエスト プロセスを見てみましょう: ユーザーは ServiceA に依存してサービスを提供し、ServiceA は ServiceB によって提供されるサービスに依存します。この時点で ServiceB が失敗すると仮定します。10 分以内、リクエストごとに、応答に 10 秒の遅延が発生します。
つまり、N 人のユーザーが ServiceA のサービスをリクエストしているとします。ServiceB へのリクエストが一時停止されるため、数秒以内に ServiceA のリソースが消費されます。ユーザーからのその後のリクエストを拒否します。ユーザーにとって、これは ServiceA と ServiceB の両方が同時に失敗し、サービス リンク全体が崩壊したことを意味します。
そして、ServiceA にサーキット ブレーカーを設置するとどうなるでしょうか?
状態になります。
ServiceB が復旧すると、サーキット ブレーカーは
サーキットブレーカーのステータス図は次のとおりです。 
次のことが考えられます。いくつかの核心点は次のとおりです: 
class CircuitBreaker {
constructor(timeout, failureThreshold, retryTimePeriod) {
// We start in a closed state hoping that everything is fine
this.state = 'CLOSED';
// Number of failures we receive from the depended service before we change the state to 'OPEN'
this.failureThreshold = failureThreshold;
// Timeout for the API request.
this.timeout = timeout;
// Time period after which a fresh request be made to the dependent
// service to check if service is up.
this.retryTimePeriod = retryTimePeriod;
this.lastFailureTime = null;
this.failureCount = 0;
}
}async call(urlToCall) {
// Determine the current state of the circuit.
this.setState();
switch (this.state) {
case 'OPEN':
// return cached response if no the circuit is in OPEN state
return { data: 'this is stale response' };
// Make the API request if the circuit is not OPEN
case 'HALF-OPEN':
case 'CLOSED':
try {
const response = await axios({
url: urlToCall,
timeout: this.timeout,
method: 'get',
});
// Yay!! the API responded fine. Lets reset everything.
this.reset();
return response;
} catch (err) {
// Uh-oh!! the call still failed. Lets update that in our records.
this.recordFailure();
throw new Error(err);
}
default:
console.log('This state should never be reached');
return 'unexpected state in the state machine';
}
}// reset all the parameters to the initial state when circuit is initialized
reset() {
this.failureCount = 0;
this.lastFailureTime = null;
this.state = 'CLOSED';
}
// Set the current state of our circuit breaker.
setState() {
if (this.failureCount > this.failureThreshold) {
if ((Date.now() - this.lastFailureTime) > this.retryTimePeriod) {
this.state = 'HALF-OPEN';
} else {
this.state = 'OPEN';
}
} else {
this.state = 'CLOSED';
}
}
recordFailure() {
this.failureCount += 1;
this.lastFailureTime = Date.now();
}... const circuitBreaker = new CircuitBreaker(3000, 5, 2000); const response = await circuitBreaker.call('http://0.0.0.0:8000/flakycall');
Opossum## と呼ばれる昔に作成された # 成熟した Node.js サーキット ブレーカー実装。リンクはここにあります: Opossum。分散システムの場合、このライブラリを使用すると、サービスの耐障害性が大幅に向上し、サービス障害の問題を根本的に解決できます。
元のアドレス: https://juejin.cn/post/7019217344601948173プログラミングビデオ著者: ES2049 / Singularity を探しています
プログラミング関連の知識をさらに増やす 、
以上がNode.js のサーキット ブレーカー メカニズムの詳細な分析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。