bugloos/fault-tolerance-bundle

容错组件

安装量: 1,150

依赖关系: 0

建议者: 0

安全: 0

星标: 11

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.0-BETA2 2022-05-30 17:36 UTC

This package is auto-updated.

Last update: 2024-09-17 16:11:59 UTC


README

容错组件

Scrutinizer Code Quality Build Status

它做什么? :)

  • 断路器模式
  • 缓存每个请求并在Redis中保存指定时间
  • 在断路器不允许执行请求且请求不存在缓存数据时,确定静态回退数据

安装

composer require bugloos/fault-tolerance-bundle

兼容性

  • PHP v7.4或更高版本
  • Symfony v4.4或更高版本

什么是断路器模式

在微服务架构中,一个服务通常调用其他服务来获取数据,下游服务可能宕机。这可能是由于慢速网络连接、超时或暂时不可用。因此,重试调用可以解决问题。然而,如果某个微服务存在严重问题,则它将长时间不可用。在这种情况下,请求将继续发送到该服务,因为客户端没有特定服务宕机的知识。结果,网络资源将因低性能和糟糕的用户体验而耗尽。此外,一个服务的故障可能导致整个应用程序的级联故障。

因此,您可以使用断路器设计模式来克服这个问题。借助这种模式,客户端将通过代理调用远程服务。这个代理将基本上表现为一个电气断路器。所以,当失败次数超过阈值时,断路器将在特定时间段内跳闸。然后,在此超时期间,所有尝试调用远程服务的尝试都将失败。超时后,断路器允许有限数量的测试请求通过。如果这些请求成功,断路器将恢复到正常操作。否则,如果有故障,超时期将重新开始。

使用方法

为了保护远程服务访问点,我们使用命令模式。这里是一个可能的简化实现

namespace App\Proxy;

use Bugloos\FaultToleranceBundle\Contract\Command;

/**
* All commands must extend Fault Tolerance Bundle's Command
  */
class GetOrderProxyCommand extends Command
{
  private $param1;
  
  private $param2;

  public function __construct($param1, $param2)
  { 
      $this->param1 = $param1;
      $this->param2 = $param2;
  }

  /**
    * This function is called internally by Fault Tolerance Bundle, only if the request is allowed
    *
    * @return mixed
    */
  protected function run()
  {
      # Make an HTTP call
  }
}

此命令可以这样使用

use Bugloos\FaultToleranceBundle\Factory\CommandFactoryInterface;
use App\Proxy\GetOrderProxyCommand;

class Service
{
  private CommandFactoryInterface $commandFactory;

  public function __construct(CommandFactoryInterface $commandFactory)
  {
      $this->commandFactory = $commandFactory;
  }

  public function getAvatarUrl()
  {
      $getOrderProxyCommand = $this->commandFactory->getCommand(
          GetOrderProxyCommand::class,
          'param1',
          'param2'
      );
      
      $result = $getOrderProxyCommand->execute();
  }
}

注意:您传递给工厂getCommand方法的额外参数将转发到命令的构造函数。

命令特定的配置在实例化时与默认配置合并。在这种情况下,“GetOrderProxyCommand”是命令键。默认情况下,它与命令的类相同,但您可以通过重写getCommandKey受保护方法来自定义它。

    /**
     * @return string
     */
    protected function getCommandKey()
    {
        return 'CustomCommandKey';
    }

容错组件仅与命令键一起工作。如果您有两个具有相同命令键的不同命令 - 容错将像单个实体一样启用和禁用请求。这可以用于分组命令。

要管理每个命令的配置,您可以在命令类的config受保护方法中使用Config对象。

    /**
     * @return ?Bugloos\FaultToleranceBundle\Config\Config
     */
    protected function config()
    {
        return (new Config())
            ->intervalToHalfOpen(15)
            ->failureRateThreshold(20)
            ->timeWindow(60);
    }

注意:您设置的配置将合并到默认配置中。

请求缓存

请求缓存,当启用时,将在单个HTTP请求内缓存命令执行结果,因此您不必担心在不需要的情况下加载网络数据。

结果按命令键和缓存键分别缓存。要定义缓存键生成逻辑,请实现getCacheKey受保护方法

    protected function getCacheKey()
    {
        return 'cache_' . $user;
    }

回退

对于一条命令,您可以指定回退逻辑,该逻辑将在失败或远程服务被阻止时执行。

namespace App\Proxy;

use Bugloos\FaultToleranceBundle\Contract\Command;

class GetAvatarUrlProxyCommand extends Command
{

    protected function run()
    {
        # Make an HTTP call
    }
  
    /**
     * When __run__ fails for some reason, or when Fault Tolerant doesn't allow the request in the first place,
     * this function result will be returned instead
     *
     * @return string
     */
    protected function getFallback()
    {
        // we failed getting user's picture, so showing a generic no-photo placeholder instead.
        return 'https://example.com/avatars/fallback-image.jpg';
    }
}

注意:如果您想在回退逻辑中使用需要网络的操作,请确保将其“包装”成一个具有容错性的独立命令。