PHP微服务框架-Swoft的“ Hello World”


总览


斯沃夫特是什么?


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


通过三年的积累和方向探索,Swoft使Swoft成为了PHP世界中的Spring Cloud,它是PHP高性能框架和微服务管理的最佳选择。


Github


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


文章


这是一套Swoft教程,将不断更新。 欢迎您一起讨论和学习。


讲解


安装


使用作曲家安装Swoft


swoft:/www# composer create-project swoft/swoft swoft Installing swoft/swoft (v2.0.5) - Installing swoft/swoft (v2.0.5): Loading from cache Created project in swoft > @php -r "file_exists('.env') || copy('.env.example', '.env');" Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 84 installs, 0 updates, 0 removals - Installing swoft/stdlib (v2.0.5): Loading from cache - Installing swoft/server (v2.0.5): Loading from cache - Installing nikic/php-parser (v4.2.4): Downloading (100%) ...... toolkit/cli-utils suggests installing inhere/php-validate (Very lightweight data validate tool) toolkit/cli-utils suggests installing inhere/console (a lightweight php console application library.) ...... Writing lock file Generating autoload files 

开始


安装后,转到Swoft项目根目录,然后如下所示启动Swoft。


 root@MyServer:/tmp/swoft# php bin/swoft http:start 2019/09/14-10:29:34 [INFO] Swoft\SwoftApplication:setSystemAlias(485) Set alias @base=/tmp/swoft 2019/09/14-10:29:34 [INFO] Swoft\SwoftApplication:setSystemAlias(486) Set alias @app=@base/app ...... 2019/09/14-10:29:35 [INFO] Swoft\Processor\ConsoleProcessor:handle(39) Console command route registered (group 14, command 42) Information Panel *********************************************************************** * HTTP | Listen: 0.0.0.0:18306, type: TCP, mode: Process, worker: 3 * RPC | Listen: 0.0.0.0:18307, type: TCP *********************************************************************** HTTP server start success ! 2019/09/14-10:29:35 [INFO] Swoft\Server\Server:startSwoole(492) Swoole\Runtime::enableCoroutine 2019/09/14-10:29:35 [INFO] ...... 

启动成功,您可以看到关闭了Http和Rpc的端口。 然后,您在浏览器中访问[ http://127.0.0.1:18306/](http://127.0.0.1:18306/ )地址。 将出现以下页面。



世界你好


像这样在Swoft控制器目录(app / Http / Controller)下创建一个新的HelloWorldController.php文件。


 <?php declare(strict_types=1); namespace App\Http\Controller; use Swoft\Http\Server\Annotation\Mapping\Controller; use Swoft\Http\Server\Annotation\Mapping\RequestMapping; /** * Class HelloWorldController * * @since 2.0 * * @Controller(prefix="hello-world") */ class HelloWorldController { /** * @RequestMapping() * * @return string */ public function index(): string { return 'Hello World !'; } } 

后续文章,重启服务,浏览器访问[ http://127.0.0.1:18306/hello-world/index](http://127.0.0.1:18306/您好 -世界/索引),您将获得以下页面。



板凳


只需在Apache Bench工具下测试Swoft,结果如下:



Github


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

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


All Articles