我有两个发送电子邮件的服务,Postmark和SendGrid。我使用工厂模式是为了具有可扩展性和灵活性。
EmailProviderInterface
namespace App\Services\Notifications;
interface EmailProviderInterface
{
public function sendEmail(string $to, string $templateName, array $data);
}
PostMarkEmailService
<?php
namespace App\Services\Notifications;
use App\Traits\SendEmailTrait;
class PostmarkEmailService implements EmailProviderInterface
{
use SendEmailTrait;
/**
* send email using postmark
*
* @param string $to
* @param string $templateName
* @param array $data
* @return array
*/
public function sendEmail(string $to, string $templateName, array $data): array
{
$templateInfo = $this->getHtmlRender($templateName, $data);
if (!$templateInfo['success']) {
return $templateInfo;
}
try {
// implement logic calling external api and passing the html
return $this->successHandler();
} catch (\Exception $e) {
return $this->handleErrors($e);
}
}
}
SendGridEmailService
<?php
namespace App\Services\Notifications;
use App\Traits\SendEmailTrait;
class SendGridEmailService implements EmailProviderInterface
{
use SendEmailTrait;
/**
* Send Email Using SendGrid
*
* @param string $to
* @param string $templateName
* @param array $data
* @return array
*/
public function sendEmail(string $to, string $templateName, array $data): array
{
$templateInfo = $this->getHtmlRender($templateName, $data);
if (!$templateInfo['success']) {
return $templateInfo;
}
try {
// implement logic calling external api and passing the html
return $this->successHandler();
} catch (\Exception $e) {
return $this->handleErrors($e);
}
}
}
这是EmailServiceProvider将使用它:
<?php
namespace App\Services\Notifications;
class EmailServiceProvider
{
private EmailProviderInterface $emailProvider;
public function __construct(bool $highPriority = true)
{
if ($highPriority) {
$this->emailProvider = new PostmarkEmailService();
} else {
$this->emailProvider = new SendGridEmailService();
}
}
/**
* Send email using the email provider
*
* @param string $to
* @param string $templateName
* @param array $data
* @return array
*/
public function sendMail(string $to, string $templateName, array $data): array
{
return $this->emailProvider->sendEmail($to, $templateName, $data);
}
}
我需要实现一个带有重试机制的负载均衡器(使用循环策略)。这将在EmailService提供程序上操作,以便在PostMark和SendGrid服务之间进行交换。
如果要实现一个队列,如何应用这样的负载均衡器?
1条答案
按热度按时间56lgkhnf1#
要在EmailServiceProvider中实现带重试的负载平衡,可以更新
constructor
和sendMail
方法。您还可以通过利用消息代理(如
RabbitMQ
或Redis
)在队列级别实现负载平衡。这些消息代理支持开箱即用的负载平衡机制,允许您将电子邮件作业的处理分布在多个工作进程或计算机上。