Node.js中断路器机制是怎么样的

发布时间:2021-10-18 10:02:55 作者:小新
来源:亿速云 阅读:118

这篇文章将为大家详细讲解有关Node.js中断路器机制是怎么样的,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

架构演变带来的问题

当我们使用传统的 CS 架构时,服务端由于故障等原因将请求堵塞,可能会导致客户端的请求失去响应,进而在一段时间后导致一批用户无法获得服务。而这种情况可能影响范围是有限,可以预估的。

然而,在微服务体系下,您的服务器可能依赖了若干其他微服务,而这些微服务又依赖其它更多的微服务,这种情况下,某个服务对于下游的堵塞,可能会瞬间(数秒内)因为级联的资源消耗造成整条链路上灾难性的后果,我们称之为“服务血崩”。【推荐学习:《nodejs 教程》】

Node.js中断路器机制是怎么样的

Node.js中断路器机制是怎么样的

解决问题的几种方式

断路器的机制和实现

断路器的存在,相当于给了我们一层保障,在调用稳定性欠佳,或者说很可能会调用失败的服务和资源时,断路器可以监视这些错误并且在达到一定阈值之后让请求失败,防止过度消耗资源。并且,断路器还拥有自动识别服务状态并恢复的功能,当上游服务恢复正常时,断路器可以自动判断并恢复正常请求。

让我们看一下一个没有断路器的请求过程: 用户依赖 ServiceA 来提供服务,ServiceA 又依赖 ServiceB 提供的服务,假设 ServiceB 此时出现了故障,在 10 分钟内,对于每个请求都会延迟 10 秒响应。

Node.js中断路器机制是怎么样的

那么假设我们有 N 个 User 在请求 ServiceA 的服务时,几秒钟内,ServiceA 的资源就会因为对 ServiceB 发起的请求被挂起而消耗一空,从而拒绝 User 之后的任何请求。对于用户来说,这就等于 ServiceA 和 ServiceB 同时都出现了故障,引起了整条服务链路的崩溃。

而当我们在 ServiceA 上装上一个断路器后会怎么样呢?

Node.js中断路器机制是怎么样的

断路器的状态图如下:

Node.js中断路器机制是怎么样的

由此可见,断路器的几个核心要点如下:

有了这些知识,我们可以尝试创建一个断路器:

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();
  }

使用断路器时,只需要将请求包裹在断路器实例中的 Call 方法里调用即可:

...
const circuitBreaker = new CircuitBreaker(3000, 5, 2000);

const response = await circuitBreaker.call('http://0.0.0.0:8000/flakycall');

成熟的 Node.js 断路器库

Red Hat 很早就创建了一个名叫 Opossum 的成熟 Node.js 断路器实现,链接在此:Opossum 。对于分布式系统来说,使用这个库可以极大提升你的服务的容错能力,从根本上解决服务血崩的问题。

关于“Node.js中断路器机制是怎么样的”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. node.js的事件轮询机制
  2. java中是如何实现多态机制的?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

node.js

上一篇:怎么设置php.ini不显示错误

下一篇:Vue 3.0进阶之什么是响应式Refs API

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》