
Swoft是一个PHP高性能微服务协程框架。 它已经发布了很多年,并已成为php的最佳选择。 它可以像Go,内置的协程Web服务器和通用协程客户端一样,并且驻留在内存中,独立于传统的PHP-FPM。 有类似的Go语言操作,类似于Spring Cloud框架的灵活注释。
Github
https://github.com/swoft-cloud/swoft
特色
完整的协程框架
Swoft是第一个驻留在内存注释框架中的PHP,其Spring Boot约定大于配置设计概念,并具有一组开发规范。
Aop
AOP是一种面向对象的编程,可以使业务代码解耦,提高代码质量和提高代码可重用性变得更加容易。
class CalcExecTimeAspect { protected $start; public function before() { $this->start = microtime(true); } 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")
批注定义服务。
class IndexController { public function index(): string { return "test"; } }
Websocket服务
Swoft为开发人员提供了完整的Websocket,以快速构建服务
class ChatModule { public function onOpen(Request $request, int $fd): void { server()->push($request->getFd(), "Opened, welcome!(FD: $fd)"); } }
RPC服务
Swoft RPC可以像本机函数一样称为Dubbo。
class RpcController { private $userService; 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 { public function echo(Request $request, Response $response): void {
连接池
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中轻松使用它。
微服务
Swoft提供了一组快速构建的微服务治理组件,开发人员易于使用。
class Test { public function func(): string {
Github
https://github.com/swoft-cloud/swoft
基准测试
