Estrutura de microsserviço PHP - `Hello world` do Swoft


Visão geral


O que é o Swoft?


O Swoft é um framework de alto desempenho em microsserviços em PHP. Foi publicado por muitos anos e se tornou a melhor escolha para php. Pode ser como Go, servidor da web incorporado de corotina e cliente comum de corotina e é residente na memória, independentemente do PHP-FPM tradicional. Existem operações de idioma Go semelhantes, semelhantes às anotações flexíveis da estrutura Spring Cloud.


Através de três anos de acumulação e exploração de direção, a Swoft transformou a Swoft na Spring Cloud no mundo PHP, que é a melhor escolha para a estrutura de alto desempenho e gerenciamento de microsserviços do PHP.


Github


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


Artigo


Este é um conjunto de tutoriais do Swoft, que serão atualizados continuamente. Você pode discutir e aprender juntos.


Tutorial


Instale


Use o compositor para instalar o 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 

Iniciar


Após a instalação, vá para o diretório raiz do projeto Swoft e inicie o Swoft como abaixo.


 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] ...... 

Comece com sucesso. Você pode ver a porta em HTTP e Rpc. Em seguida, você acessa o endereço [ http://127.0.0.1:18306/ ] ( http://127.0.0.1:18306/ ) no navegador. A página a seguir será exibida.



Olá mundo


Crie um novo arquivo HelloWorldController.php no diretório do controlador Swoft (app / Http / Controller) como este.


 <?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 !'; } } 

O controlador e o roteador usados ​​aqui serão descritos em detalhes no artigo subsequente, reinicie o serviço, acesse o navegador [ http://127.0.0.1:18306/hello-world/index ] ( http://127.0.0.1:18306/ olá - Mundo / índice), você verá a página a seguir.



Banco


Simplesmente teste o Swoft sob a ferramenta Apache Bench e os resultados são os seguintes:



Github


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

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


All Articles