PHP微服务框架-Swoft


Swoft是一个PHP高性能微服务协程框架。 它已经发布了很多年,并已成为php的最佳选择。 它可以像Go,内置的协程Web服务器和通用协程客户端一样,并且驻留在内存中,独立于传统的PHP-FPM。 有类似的Go语言操作,类似于Spring Cloud框架的灵活注释。


Github


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


特色


完整的协程框架


Swoft是第一个驻留在内存注释框架中的PHP,其Spring Boot约定大于配置设计概念,并具有一组开发规范。


Aop


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服务


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

微服务


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]; } } 

Github


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


基准测试


Source: https://habr.com/ru/post/zh-CN463761/


All Articles