PHP إطار microservice - Swoft


Swoft هو إطار PHOTO microservice عالي الأداء. تم نشره لسنوات عديدة وأصبح الخيار الأفضل لـ php. يمكن أن يكون مثل Go ، خادم الويب coroutine المدمج والعميل coroutine المشترك ويقيم في الذاكرة ، مستقلة عن PHP-FPM التقليدية. هناك عمليات مماثلة للغة Go ، مشابهة للشروح المرنة لإطار Spring Cloud.


جيثب


https://github.com/swoft-cloud/swoft


ميزة


إطار coroutine الكامل


Swoft هو أول مقيم في PHP في إطار التعليق التوضيحي للذاكرة ، مع اصطلاحات Spring Boot أكبر من مفهوم تصميم التهيئة ، مع مجموعة من مواصفات التطوير.


اوب


AOP عبارة عن برمجة موجهة للكائنات تسهل فك شفرة العمل ، وتحسين جودة الشفرة ، وزيادة إمكانية إعادة استخدام الشفرة.


/** * @Aspect(order=1) * @PointBean(include={"App\Http\Controller\TestExecTimeController"}) */ class CalcExecTimeAspect { protected $start; /** * @Before() */ public function before() { $this->start = microtime(true); } /** * @After() */ public function after(JoinPoint $joinPoint) { $method = $joinPoint->getMethod(); $after = microtime(true); $runtime = ($after - $this->start) * 1000; echo "{$method} cost: {$runtime}ms\n"; } } 

خدمة المتشعب


خدمة Http بسيطة ومرنة ، فقط لاستخدام @Controller() و @RequestMapping(route="index") لتحديد الخدمة。


 /** * @Controller() */ class IndexController { /** * @RequestMapping(route="index") */ public function index(): string { return "test"; } } 

خدمة websocket


يوفر Swoft Websocket كاملة للمطورين لبناء الخدمة بسرعة


 /** * @WsModule( * "/chat", * messageParser=TokenTextParser::class, * controllers={HomeController::class} * ) */ class ChatModule { /** * @OnOpen() * @param Request $request * @param int $fd */ public function onOpen(Request $request, int $fd): void { server()->push($request->getFd(), "Opened, welcome!(FD: $fd)"); } } 

خدمة RPC


يمكن استدعاء Swoft RPC مثل وظيفة أصلية مثل Dubbo.


 /** * @Controller() */ class RpcController { /** * @Reference(pool="user.pool", version="1.0") * * @var UserInterface */ private $userService; /** * @RequestMapping("getList") * * @return array */ public function getList(): array { $result = $this->userService->getList(12, 'type'); return [$result]; } } 

خدمة TCP


يوفر Swoft أيضًا خدمة دعم TCP غنية بالميزات.


 <?php declare(strict_types=1); namespace App\Tcp\Controller; use Swoft\Tcp\Server\Annotation\Mapping\TcpController; use Swoft\Tcp\Server\Annotation\Mapping\TcpMapping; use Swoft\Tcp\Server\Request; use Swoft\Tcp\Server\Response; /** * Class DemoController * * @TcpController() */ class DemoController { /** * @TcpMapping("echo", root=true) * @param Request $request * @param Response $response */ public function echo(Request $request, Response $response): void { // $str = $request->getRawData(); $str = $request->getPackage()->getDataString(); $response->setData('[echo]hi, we received your message: ' . $str); } } 

تجمع اتصال


Swoft بسيط لتعريف تجمع اتصال عالي الأداء بمثل هذا:


 return [ 'xxx.pool' => [ 'class' => \Swoft\xxx\Pool::class, 'minActive' => 10, 'maxActive' => 20, 'maxWait' => 0, 'maxWaitTime' => 0, 'maxIdleTime' => 60, ] ]; 

متوافق مع Laravel ORM


قاعدة بيانات Swoft متوافقة للغاية مع Laravel ORM ، من السهل على مطور PHP استخدامها في Swoft.


 // Model $user = User::new(); $user->setName('name'); $user->setSex(1); $user->setDesc('this my desc'); $user->setAge(mt_rand(1, 100)); $user->save(); $id = $user->getId(); // Query $users = DB::table('user')->get(); foreach ($users as $user) { echo $user->name; } // Transaction B::beginTransaction(); $user = User::find($id); $user->update(['name' => $id]); DB::beginTransaction(); User::find($id)->update(['name'=>'sakuraovq']); DB::rollBack(); DB::commit(); 

Microservice


يوفر Swoft مجموعة من مكونات حوكمة الخدمات المصغرة سريعة الإنشاء ، ومن السهل على المطورين استخدامها.


  • تسجيل الخدمة واكتشافها
  • وسيط الخدمة
  • التكوين المركزي
  • خدمة اختناق القدرة

 /** * @Bean() */ class Test { /** * @Breaker(fallback="funcFallback") * * @return string * @throws Exception */ public function func(): string { // Do something throw new Exception('Breaker exception'); } /** * @RequestMapping() * @RateLimiter(key="request.getUriPath()") * * @param Request $request * * @return array */ public function requestLimiter(Request $request): array { $uri = $request->getUriPath(); return ['requestLimiter', $uri]; } } 

جيثب


https://github.com/swoft-cloud/swoft


المعيار


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


All Articles