PHP Microservice Framework - "Hallo Welt" von Swoft


Übersicht


Was ist Swoft?


Swoft ist ein PHP-Hochleistungs-Microservice-Coroutine-Framework. Es wurde seit vielen Jahren veröffentlicht und ist die beste Wahl für PHP geworden. Es kann wie Go, ein integrierter Coroutine-Webserver und ein allgemeiner Coroutine-Client sein und befindet sich unabhängig vom herkömmlichen PHP-FPM im Speicher. Es gibt ähnliche Go-Sprachoperationen, ähnlich den flexiblen Annotationen des Spring Cloud-Frameworks.


Durch drei Jahre Akkumulation und Erforschung der Richtung hat Swoft Swoft zur Spring Cloud in der PHP-Welt gemacht, die die beste Wahl für das Hochleistungs-Framework und das Microservices-Management von PHP ist.


Github


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


Artikel


Dies ist eine Reihe von Swoft-Tutorials, die kontinuierlich aktualisiert werden. Sie können gerne gemeinsam diskutieren und lernen.


Tutorial


Installieren


Verwenden Sie Composer, um Swoft zu installieren


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 

Starten Sie


Wechseln Sie nach der Installation in das Stammverzeichnis des Swoft-Projekts und starten Sie Swoft wie unten beschrieben.


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

Starten Sie Erfolg, Sie können den Port von HTTP und RPC sehen. Anschließend greifen Sie im Browser auf die Adresse [ http://127.0.0.1:18306/ ] ( http://127.0.0.1:18306/ ) zu. Die folgende Seite wird angezeigt.



Hallo Welt


Erstellen Sie eine neue HelloWorldController.php Datei im Swoft-Controller-Verzeichnis (app / Http / Controller) wie folgt.


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

Der hier verwendete Controller und Router wird im folgenden Artikel ausführlich beschrieben. Starten Sie den Dienst neu und greifen Sie auf den Browser zu [ http://127.0.0.1:18306/hello-world/index ] ( http://127.0.0.1:18306/). Hallo - Welt / Index), erhalten Sie die folgende Seite.



Bank


Testen Sie Swoft einfach mit dem Apache Bench-Tool. Die Ergebnisse lauten wie folgt:



Github


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

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


All Articles