PHP microservices Framework - تم نشر Swoft 2.0.3

صورة


ما هو Swoft؟


Swoft عبارة عن إطار كوروتي لخدمات PHP يعتمد على امتداد Swoole . مثل Go ، يحتوي Swoft على خادم ويب coroutine مدمج وعميل coroutine مشترك ويقيم في الذاكرة ، مستقل عن PHP-FPM التقليدي. هناك عمليات Go Go مماثلة ، مثل الشروح المرنة لإطار Spring Cloud ، وحاوية حقن التبعية العالمية القوية ، وحوكمة الخدمة الشاملة ، AOP المرنة والقوية ، وتنفيذ مواصفات PSR القياسية وما إلى ذلك.


من خلال ثلاث سنوات من التراكم واستكشاف الاتجاهات ، جعلت Swoft Swoft the Spring Cloud في عالم PHP ، وهو الخيار الأفضل لإطار عمل PHP عالي الأداء وإدارة الخدمات المصغرة.


حوكمة خدمة أنيقة


توصي Swoft رسميًا بأن يستخدم المطورون أنماط شبكة الخدمة ، مثل إطار عمل Istio / Envoy ، لفصل حوكمة الأعمال والخدمات ، ولكن يوفر Swoft أيضًا مجموعة من مكونات الخدمات المصغرة للشركات الصغيرة والمتوسطة الحجم لإنشاء خدمات ميكروية بسرعة.



تسجيل الخدمة واكتشافها


لتسجيل الخدمة واكتشافها ، يكون مكون القنصل في swoft الذي توفره Swoft مطلوبًا ، إذا كانت أطراف ثالثة أخرى متشابهة.


خدمات التسجيل والإلغاء


استمع إلى حدث SwooleEvent::START ، سجّل الخدمة


 /** * Class RegisterServiceListener * * @since 2.0 * * @Listener(event=SwooleEvent::START) */ class RegisterServiceListener implements EventHandlerInterface { /** * @Inject() * * @var Agent */ private $agent; /** * @param EventInterface $event */ public function handle(EventInterface $event): void { /* @var HttpServer $httpServer */ $httpServer = $event->getTarget(); $service = [ // .... ]; $scheduler = Swoole\Coroutine\Scheduler(); $scheduler->add(function () use ($service) { // Register $this->agent->registerService($service); CLog::info('Swoft http register service success by consul!'); }); $scheduler->start(); } } 

استمع إلى حدث SwooleEvent::SHUTDOWN ، SwooleEvent::SHUTDOWN بإلغاء الخدمة


 /** * Class DeregisterServiceListener * * @since 2.0 * * @Listener(SwooleEvent::SHUTDOWN) */ class DeregisterServiceListener implements EventHandlerInterface { /** * @Inject() * * @var Agent */ private $agent; /** * @param EventInterface $event */ public function handle(EventInterface $event): void { /* @var HttpServer $httpServer */ $httpServer = $event->getTarget(); $scheduler = Swoole\Coroutine\Scheduler(); $scheduler->add(function () use ($httpServer) { $this->agent->deregisterService('swoft'); }); $scheduler->start(); } } 

اكتشاف الخدمة


تحديد مزود الخدمة


 /** * Class RpcProvider * * @since 2.0 * * @Bean() */ class RpcProvider implements ProviderInterface { /** * @Inject() * * @var Agent */ private $agent; /** * @param Client $client * * @return array * @example * [ * 'host:port' * ] */ public function getList(Client $client): array { // Get health service from consul $services = $this->agent->services(); $services = [ ]; return $services; } } 

مزود خدمة التكوين


 return [ 'user' => [ 'class' => ServiceClient::class, 'provider' => bean(RpcProvider::class) // ... ] ]; 

خدمة الكسارة


يستخدم @Breaker التعليق التوضيحي @Breaker لتحقيق ضربة ، والتي يمكن تفجيرها على أي طريقة.


 /** * Class BreakerLogic * * @since 2.0 * * @Bean() */ class BreakerLogic { /** * @Breaker(fallback="funcFallback") * * @return string * @throws Exception */ public function func(): string { // Do something throw new Exception('Breaker exception'); } /** * @return string */ public function funcFallback(): string { return 'funcFallback'; } } 

حد الخدمة


يستخدم @RateLimiter التعليق التوضيحي @RateLimiter لتطبيق التحكم في الخدمة ، والذي يمكن التحكم به بأي طريقة ، وليس فقط وحدة التحكم ، كما يدعم KEY لغة تعبير symfony / تعبير لغة التعبير.


 /** * Class LimiterController * * @since 2.0 * * @Controller(prefix="limiter") */ class LimiterController { /** * @RequestMapping() * @RateLimiter(key="request.getUriPath()", fallback="limiterFallback") * * @param Request $request * * @return array */ public function requestLimiter(Request $request): array { $uri = $request->getUriPath(); return ['requestLimiter', $uri]; } /** * @param Request $request * * @return array */ public function limiterFallback(Request $request): array { $uri = $request->getUriPath(); return ['limiterFallback', $uri]; } } 

مركز التكوين


يحتاج مركز التكوين إلى استخدام مكون Swoft-apollo المقدم من Swoft رسميًا ، إذا كانت الأطراف الثالثة الأخرى متشابهة.


أعلن وكيل


 /** * Class AgentCommand * * @since 2.0 * * @Command("agent") */ class AgentCommand { /** * @Inject() * * @var Config */ private $config; /** * @CommandMapping(name="index") */ public function index(): void { $namespaces = [ 'application' ]; while (true) { try { $this->config->listen($namespaces, [$this, 'updateConfigFile']); } catch (Throwable $e) { CLog::error('Config agent fail(%s %s %d)!', $e->getMessage(), $e->getFile(), $e->getLine()); } } } /** * @param array $data * * @throws ContainerException * @throws ReflectionException */ public function updateConfigFile(array $data): void { foreach ($data as $namespace => $namespaceData) { $configFile = sprintf('@config/%s.php', $namespace); $configKVs = $namespaceData['configurations'] ?? ''; $content = '<?php return ' . var_export($configKVs, true) . ';'; Co::writeFile(alias($configFile), $content, FILE_NO_DEFAULT_CONTEXT); CLog::info('Apollo update success!'); /** @var HttpServer $server */ $server = bean('httpServer'); $server->restart(); } } } 

بدء وكيل


يحتاج الوكيل فقط إلى التشغيل قبل بدء تشغيل الخدمة (Http / RPC / Websocket).


 php bin/swoft agent:index 

مورد


Source: https://habr.com/ru/post/ar460147/


All Articles